Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback for environment variables with docker-compose

Given the following configuration:

mysql:
    environment:
        MY_MYSQL_PORT:
    image: mysql
    ports:
        - "${MY_MYSQL_PORT}:3306"

There's a way to provide a fallback value for MY_MYSQL_PORT without relying on wrapper scripts? I already tested like bash ${MY_MYSQL_PORT-3306} but it doesn't work.

like image 953
Jean Carlo Machado Avatar asked Nov 18 '15 16:11

Jean Carlo Machado


People also ask

Does Docker compose pass environment variables?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

How do I override Docker compose environment variables?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.

Does Docker compose down delete containers?

Stops containers and removes containers, networks, volumes, and images created by up .

How can I see docker environment variables?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.


1 Answers

They implemented that feature with compose 1.9 release:

Added support for shell-style inline defaults in variable interpolation.

The supported forms are ${FOO-default} (fall back if FOO is unset) and ${FOO:-default} (fall back if FOO is unset or empty).

Release Notes Docker Compose 1.9

like image 143
Alexander Heimbuch Avatar answered Sep 18 '22 15:09

Alexander Heimbuch