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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With