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.
Related Docker Compose job interview questions
How do you display a Docker Compose version that is currently installed?
Docker Compose JuniorHow do you pass environment variables to Docker Compose containers when you are starting services?
Docker Compose JuniorHow do you create a custom network and configure a Docker Compose service to use this network?
Docker Compose JuniorHow do you pass an environment variable to a Docker Compose service container?
Docker Compose JuniorHow do you display images of all running containers that were created based on running Docker Compose?
Docker Compose Junior