compose icon indicating copy to clipboard operation
compose copied to clipboard

Variable substitution in service name

Open lmesz opened this issue 10 years ago • 8 comments

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,

lmesz avatar Aug 19 '16 08:08 lmesz

Hi,

This is not currently supported, sorry. No substitution is done for keys, only values.

shin- avatar Aug 31 '16 22:08 shin-

Sad to hear that. May I contribute with that function ? Or is there any specific reason why is it not supported?

lmesz avatar Sep 01 '16 08:09 lmesz

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?

dnephin avatar Sep 01 '16 18:09 dnephin

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.

lmesz avatar Sep 02 '16 11:09 lmesz

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.

lmesz avatar Nov 02 '16 15:11 lmesz

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 :)

tchernobyl avatar Dec 16 '16 17:12 tchernobyl

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.

dnephin avatar Dec 16 '16 21:12 dnephin

@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.

ErumS avatar Sep 28 '22 15:09 ErumS