Write a docker-compose.yml for an app, Postgres and Redis, where the app waits for a healthy database.
IntermediateHands-onWrite-a-Dockerfile tasks
Answer
services:
app:
build: .
ports: ["8000:8000"]
environment:
DATABASE_URL: postgresql://app:app@db:5432/app
REDIS_URL: redis://cache:6379/0
depends_on:
db: { condition: service_healthy }
cache: { condition: service_started }
db:
image: postgres:16
environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: app, POSTGRES_DB: app }
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
retries: 10
cache:
image: redis:7-alpine
volumes:
pgdata:
Key trick → Plain depends_on only orders startup. condition: service_healthy actually waits for the healthcheck to pass. Services reach each other by service name (db) on the Compose network.
More on Write-a-Dockerfile tasks
Write a production Dockerfile for a Node.js API using a multi-stage build.IntermediateWrite a Dockerfile for a Python Flask app that runs with gunicorn as a non-root user.BeginnerWrite a Dockerfile for a Go service that produces a tiny image (under 20 MB).IntermediateWrite a multi-stage Dockerfile for a Java Spring Boot app built with Maven.IntermediateWrite a Dockerfile for a static React site served by Nginx.BeginnerThis Dockerfile rebuilds dependencies on every code change. Fix it.Beginner