docker --version
docker compose version
Write a production-grade docker-compose.yml for a web app, database, and cache with proper networking.
1 services: 2 api: 3 build: 4 context: . 5 dockerfile: Dockerfile 6 target: runtime 7 ports: 8 - "8080:8080" 9 environment: 10 DATABASE_URL: postgresql://app:secret@postgres:5432/appdb 11 REDIS_URL: redis://redis:6379/0 12 env_file: 13 - .env.local 14 depends_on: 15 postgres: 16 condition: service_healthy 17 redis: 18 condition: service_healthy 19 volumes: 20 - ./src:/app/src:ro # Hot-reload in dev; remove in prod 21 restart: unless-stopped 22 networks: 23 - backend 24 25 postgres: 26 image: postgres:16-alpine 27 environment: 28 POSTGRES_DB: appdb 29 POSTGRES_USER: app 30 POSTGRES_PASSWORD: secret 31 volumes: 32 - postgres_data:/var/lib/postgresql/data 33 - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql:ro 34 healthcheck: 35 test: ["CMD-SHELL", "pg_isready -U app -d appdb"] 36 interval: 10s 37 timeout: 5s 38 retries: 5 39 start_period: 30s 40 networks: 41 - backend 42 43 redis: 44 image: redis:7-alpine 45 command: redis-server --requirepass secret --maxmemory 256mb --maxmemory-policy allkeys-lru 46 volumes: 47 - redis_data:/data 48 healthcheck: 49 test: ["CMD", "redis-cli", "--pass", "secret", "ping"] 50 interval: 10s 51 timeout: 5s 52 retries: 5 53 networks: 54 - backend 55 56 # Dev-only service 57 pgadmin: 58 image: dpage/pgadmin4:latest 59 profiles: ["dev"] 60 environment: 61 PGADMIN_DEFAULT_EMAIL: admin@local.dev 62 PGADMIN_DEFAULT_PASSWORD: admin 63 ports: 64 - "5050:80" 65 depends_on: 66 - postgres 67 networks: 68 - backend 69 70 volumes: 71 postgres_data: 72 redis_data: 73 74 networks: 75 backend: 76 driver: bridge 77
Sign in to share your feedback and join the discussion.