How can i use variable to assigne a service name


Say I have a .env file that contains the variables I want to pass to the docker-compose.yml file.




version: '3.7'

How can I use the variable for this service? The above code block show an error:

services Additional property ${SERVICE_NAME} is not allowed

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

1 Like

Thanks Metin

It is really helpful.

Just to give you the full picture:

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.

Update: this is the somewhere

1 Like

It same as the old reply “Little complex to understand” LOL. Still will give it a try.

If you want, you can quote the parts that are unclear to you, write what you understood or assume, then I can try to rephrase the quoted part.

1 Like