Use the environment section to set an environment variable in a container via docker-compose:


After docker-compose up the value of DB_HOST inside the container will be empty. To set the default value if the environment variable is empty or not set, use the following syntax:

  - DB_HOST=${DB_HOST:-localhost}

To provide values for DB_HOST, either set it in the .env file or pass it on the command line:

DB_HOST=staging docker-compose up

For more details see the Variable substitution section of the compose file documentation.