Variable substitution in service name
Hy,
I set SERVICE_NAME to "test".
Tried docker-compose up -d with the following compose file:
jenkins_${SERVICE_NAME}:
image: jenkins
ports:
- "8080:8080"
Following error appears:
ERROR: The Compose file './docker-compose.yml' is invalid because: Invalid service name 'rbp_${USER}' - only [a-zA-Z0-9._-] characters are allowed
Is it possible to somehow create service dynamically, pass env_var to service name?
docker-compose version: docker-compose version 1.7.1, build 6c29830
Thanks in advance,
Hi,
This is not currently supported, sorry. No substitution is done for keys, only values.
Sad to hear that. May I contribute with that function ? Or is there any specific reason why is it not supported?
It is not supported by design. There shouldn't be any need to have variables in the keys (service names), and it makes the code a lot more complex.
In almost every case you should be able to vary the names by using the $COMPOSE_PROJECT_NAME environment variable, or docker-compose -p.
Can you tell us more about your use case?
We have an infrastructure where we wanted to deploy compose apps with the same service name but different container name. Obviously it wasn't possible so I wanted to use az environment variable e.g.: ${PORT} and add it to the name of the service so the same compose file could be used without sed or any custom script. I didn't know about the "-p" parameter, but it sounds promising. Thanks the advice I give it a try.
Sorry I know you answered earlier. I tried the --project as you adviced and it works like a charm and solves my problem perfectly. Thanks.
Hi , I had the some issue , I need to set the name dynamically , I am working in automated tests ,several containers must be executed in each pull request , there's no problem in the case of a single request sent , but if there are many request It will generate a lot of problems. I resolve this issue by generate a docker-compose file from template for each request , but I wonder if there is a better solution, thx :)
Hello, yes, the better solution is to use COMPOSE_PROJECT_NAME or -p to set a unique project name. That will ensure that all containers, networks and volumes have unique names without the need for variables.
@tchernobyl Hi! Can you please specify how you created docker-compose file from template for each request? I am trying to build several containers with services such as mysql within the container scope. For this I am trying to create dynamic services but unable to.