How do you define a default value in Docker Compose interpolation?
Experience Level: Senior
Tags: Docker Compose
Answer
Use the following syntax with default value after dash:
version: "3.9"
services:
my-service:
image: "postgres:${POSTGRES_VERSION:-1.0.0}"
If the environment variable POSTGRES_VERSION is not defined, the image version will fall back to postgres:1.0.0.
Related Docker Compose job interview questions
How do you comment out lines that you don't want to be processed by Docker Compose?
Docker Compose SeniorHow do tell Docker Compose to use custom .env2 file without passing it in command-line argument?
Docker Compose SeniorHow does Docker Compose work with multiple .yml files?
Docker Compose SeniorHow do you pull all images associated with services defined in your docker-compose.yml file?
Docker Compose SeniorUsing Docker Compose, how do you define that a service should be restarted after a failure?
Docker Compose Senior