Archive · Docker

Docker Compose for Multi-Container Apps

Describe a web app and its database in one file and start both with a single command.

1 min read

  • Docker Compose 1.26
  • Docker Engine 19.03.12
  • Redis 6

Compose describes a whole application in docker-compose.yml.

version: "3.8"
services:
  web:
    build: .
    ports: ["8080:3000"]
    environment:
      REDIS_HOST: cache
    depends_on: [cache]
  cache:
    image: redis:6-alpine
    volumes: [redisdata:/data]
volumes:
  redisdata:
docker-compose up -d --build
docker-compose ps
docker-compose logs -f web
docker-compose down          # add -v to delete volumes too

Compose creates a network for the project, so web reaches Redis at the hostname cache. Note that depends_on only controls start order, not whether the dependency is ready; make your app retry its connection.

Install Compose 1.26 from the GitHub release binary or pip. Keep secrets out of the file: use an .env file that is not committed.