docker-compose: remove default suffix on container name?

Viewed 2990

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.

1 Answers

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
Related