2

I have a docker-compose.yml

php-fpm:
    build: ./php
    container_name: php-fpm-symfony
    links:
        - db
    ports:
        - 9000:9000
        - 8000:8000
    working_dir: /var/www/html/
    volumes:
        - ../:/var/www/html
    volumes_from:
        - data
    tty: true
    env_file:
        - ./docker.env
    entrypoint: /entrypoint.sh

When I'm executing my docker-compose up -d I would like to pass an argument, which is my container name, to name dynamically my container.

Is it possible with docker-compose?

Like for example:

docker-compose up -d "-variable=symfony" // ???

And:

php-fpm:
    build: ./php
    container_name: php-fpm-$(variable)

Something which works like that...

5
  • Can't you just use normal environment variables? docs.docker.com/compose/environment-variables Commented Aug 22, 2016 at 17:57
  • @StephenHarris But I don't necessarily want my variable to be in my container. I just want to build different container name regarding the argument. Commented Aug 22, 2016 at 18:04
  • Can't you do container_name: php-fpm-${FOOBAR} and then FOOBAR=symfony docker-compose ... ? Commented Aug 22, 2016 at 18:06
  • Apparently not. I have this message WARNING: The FOOBAR variable is not set. Defaulting to a blank string. Commented Aug 22, 2016 at 18:16
  • Did you type it on one line, 'cos it worked for me. % grep FOO docker-compose.yml container_name: foo-${FOOBAR} % FOOBAR=hellotest docker-compose up Recreating foo- Attaching to foo-hellotest foo-hellotest | Hello, World foo-hellotest exited with code 0 . We can see the 'hellotest' coming from the environment. Commented Aug 22, 2016 at 18:42

1 Answer 1

1

You can define all of your possible choices in YAML and use YAML references to keep the verbosity down.

default-container: &default-container
    build: ./php
    container_name: default
    links:
        - db
    ports:
        - 9000:9000
        - 8000:8000
    working_dir: /var/www/html/
    volumes:
        - ../:/var/www/html
    volumes_from:
        - data
    tty: true
    env_file:
        - ./docker.env
    entrypoint: /entrypoint.sh

php-fpm-symfony:
    <<: *default-container
    container_name: php-fpm-symfony

php-fpm-laravel:
    <<: *default-container
    container_name: php-fpm-laravel

References:

https://en.wikipedia.org/wiki/YAML#References

https://stackoverflow.com/questions/4150782/using-yaml-with-variables

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.