How do you create a Docker Compose file that is parametrized using environment variables?

Experience Level: Junior
Tags: Docker Compose

Answer

To write an environment variable value to a Docker Compose file, use the ${ENVIRONMENT_VARIABLE_NAME} syntax.
If the environment variable exists, the placeholder will be replaced by the value of the environment variable when the Docker Compose file is used.

version: "3.4"
services:
  my-service:
    image: "webapp:${TAG}"

Note that the environment variables must reside in a file .env or other file that is passed to docker using --env-file parameter.

Comments

No Comments Yet.
Be the first to tell us what you think.
Docker Compose
Docker Compose

Are you learning Docker Compose ? Try our test we designed to help you progress faster.

Test yourself