Andrius
Andrius

Reputation: 21098

docker-compose: remove default suffix on container name?

my.yml:

version: '3'
services:
  myservice:
    image: myimage

When I run docker-compose -p myprefix -f my.yml up

It creates container named myprefix_myservice_1.

Is it possible to generate name, so it would use prefix (project name) and service name only without suffix?

In this case it should be: myprefix_myservice

Whats the point of _1 suffix anyway? Looks weird and does not seem to do anything important, like incrementing _2 for new container etc? If I run same prefix/service name, it just gonna start existing container anyway. So really don't see any reason to have all containers contain same _1 suffix.

I could use container_name, but then it won't be possible to reuse same compose file for multiple containers.

P.S. I read this question: docker-compose image named: "prefix_%s_1" instead of "%s"

But it does not give any answer about suffix.

Upvotes: 10

Views: 8993

Answers (3)

Chris Becke
Chris Becke

Reputation: 36016

Docker compose supports scaled services:

services:
  test:
    image: nginx:latest
    scale: 2
docker compose up -d
❯ docker compose up -d
[+] Running 2/2
 ✔ Container tmp-test-2  Started                                                                                                                                                                                                                                                    0.5s 
 ✔ Container tmp-test-1  Started    

You can manually set the number of instances for a service:

❯ docker compose up --scale test=3 --detach
[+] Running 3/3
 ✔ Container tmp-test-1  Started                                                                                                                                                                                                                                                    0.4s 
 ✔ Container tmp-test-2  Started                                                                                                                                                                                                                                                    0.6s 
 ✔ Container tmp-test-3  Started       

Interestingly docker compose vs docker-compose separates with dashes rather than underscores.

When using other compose commands, there is an optional --index parameter that can target the replica you want. e.g.

docker compose exec --index 2 test hostname

Anyway. Hope this helps.

Upvotes: -1

user22655254
user22655254

Reputation: 21

Now I can use this:

version: '3'
name: myprefix # this is project name instead of `-p` option
services:
  myservice:
    image: myimage
    container_name: '${COMPOSE_PROJECT_NAME}_myservice'

I searched but there's no way to get service name in yaml from my understand. We may have to use env variable or type myservice twice for name prop.

Upvotes: 2

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13616

Here's one way to do it that works in non-Swarm mode using variable substitution and container_name:

# my.yml
version: '3'
services:
  myservice:
    image: redis:alpine
    container_name: ${MYPREFIX}_myservice_${MYSUFFIX}
$ MYPREFIX=prefix MYSUFFIX=test docker-compose -f my.yml up
...
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
def6d2925819        redis:alpine        "docker-entrypoint.s…"   5 seconds ago       Up 5 seconds        6379/tcp            prefix_myservice_test

Upvotes: 6

Related Questions