No, you canât. Service names are supposed to be literal strings.
You can use --project-name (short version: -p ) to specify the project name, which will be prefixed to the container, volume, and network names for a particular compose deployment to prevent naming conflicts with other deployments using the same docker-compose.yml
You could render your compose files through templating of some sort (you would need to solve this yourself) and pass it to docker compose, like this: {command to render template} | docker compose -f - up -d
The -f - uses the compose file it receives via stdin.
Somewhere in the forum, I gave an example that uses envsubst for simple templating.
Hey, just wanted to make sure there is still no way to use environment variables in service names nowadays. I would need to specify different service names for several instances of a multi container app running on the same network.
No, it is not supported. And will not if nobody asks for the feature
You can do that here:
My opinion is that when I need a more complex compose file where even service names can be changed, I use a template system. I wouldnât complicate the compose file parser more. Even now there are some issues with handling variables i compose files when using docker compose config