MediumConcept

What is Docker Compose used for?

Technology:
Docker
Experience:
Mid-level,
Senior

Quick answer

Docker Compose defines a multi-container application (services, networks and volumes) in one YAML file so you can start, stop and rebuild the whole stack with a single command, typically for local development and testing.

Detailed explanation

Most applications need more than one container: an API, a database, a cache, perhaps a worker. Starting each with long docker run commands is error-prone. Compose describes them declaratively in compose.yaml.

Services on the same Compose network reach each other by service name, so the API connects to postgres://db:5432 rather than an IP address. depends_on with health checks controls start-up order, and environment variables can come from an .env file.

Compose is ideal for development, CI and small single-host deployments. For production across many machines, teams move to an orchestrator such as Kubernetes or a managed container service, which adds scheduling, self-healing and rolling updates.

Example

yaml
services:
  api:
    build: .
    ports: ["3000:3000"]
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/app
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
    volumes: [pgdata:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app"]
      interval: 5s
volumes:
  pgdata:

Key points

  • One file describes services, networks and volumes
  • Services find each other by name
  • Great for development, CI and single-host setups
  • Kubernetes or managed services for multi-host production

Follow-up questions

  • DockerEasy

    What are Docker volumes and why do you need them?

    Volumes are storage managed by Docker that lives outside a container鈥檚 writable layer, so data such as database files survives when the container is removed or replaced.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    How do you write a good Dockerfile? What are multi-stage builds?

    Use a small, pinned base image, order instructions so dependencies are cached before source code, use multi-stage builds to leave build tools out of the final image, run as a non-root user and keep secrets out of the image.

    Junior 路 Mid-level 路 DevOps
  • DockerEasy

    What is the difference between a Docker container and a virtual machine?

    A virtual machine runs a full guest operating system on virtualised hardware, while a container shares the host鈥檚 kernel and isolates only the process, its filesystem and resources, which makes containers much lighter and faster to start.

    Intern 路 Junior 路 DevOps
  • DockerEasy

    What is the difference between a Docker image and a container?

    An image is a read-only template made of layers that contains the application and its dependencies; a container is a running (or stopped) instance of an image with its own writable layer on top.

    Intern 路 Junior 路 DevOps
  • Node.jsEasy

    What is middleware in Express and how does it work?

    Middleware are functions that receive the request, response and a next callback; they run in the order they are registered and can modify the request, end the response or pass control on with next().

    Junior 路 Mid-level 路 REST API
  • Software TestingEasy

    What is the test pyramid?

    The test pyramid is a guideline to have many fast unit tests at the base, fewer integration tests in the middle and only a small number of slow end-to-end tests at the top, so the suite stays fast and reliable.

    Junior 路 Mid-level 路 Testing