Using Docker Compose, how do you define that one service is dependent on another service?
Experience Level: Senior
Tags: Docker Compose
Answer
To define a dependency (or more dependencies), add the depends_on: property under you service:
version: "3.9"
services:
web:
image: busybox
depends_on:
- db
db:
image: busybox
This tells Docker Compose that the service web is dependent on the service db, so it will be started after the db service starts.
Related Docker Compose job interview questions
How 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 SeniorHow do you use definitions that are stored in multiple Docker Compose files?
Docker Compose JuniorHow do you mount a volume to your Docker Compose service?
Docker Compose JuniorHow do you mount a volume to your Docker Compose service?
Docker Compose Junior