Docker Compose Explained: Run Multi-Container Apps Locally

Most apps are not one process. A web app needs a database, a cache, and maybe a worker that sends mail in the background. Docker Compose explains how to run that whole stack with one file and one command, instead of a chain of long docker run commands you have to remember and type in the right order. This guide covers docker compose multi-container apps from the first file to the commands you will use every day.

Compose reads a YAML file, usually called compose.yaml, that lists each service, its image, its ports, its environment, and its volumes. This docker compose yaml example uses three services, but the shape stays the same with two or with ten. One docker compose up starts every service, wires them onto a shared network, and keeps their logs in one stream. One docker compose down stops them all. That single-file habit is what makes local development match production closely enough to catch integration bugs early.

Docker Compose multi-container apps in one file

Take a typical Node app with a Postgres database and a Redis cache. The Compose file below starts all three, gives each a name, and connects them on a network Compose creates for you:

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:app@db:5432/appdb
      REDIS_URL: redis://cache:6379
    depends_on:
      - db
      - cache
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app
      POSTGRES_DB: appdb
    volumes:
      - pgdata:/var/lib/postgresql/data
  cache:
    image: redis:7-alpine
volumes:
  pgdata:

Notice the hostnames. The app reaches the database at db and the cache at cache, because Compose gives each service a DNS name on the shared network. There is no need to look up IP addresses or publish the database port to your machine. Keep the database port unpublished in local development unless you need to inspect it with a client, because an open database port is an easy mistake to carry into a shared environment.

Start, stop, and rebuild without surprises

The daily commands are few. The docker compose up down commands are the ones you will use every day, and they are worth learning before anything else. docker compose up -d starts the stack in the background. docker compose ps shows what is running. docker compose logs -f web follows the app logs. docker compose down stops everything and removes the network, while docker compose down -v also deletes the named volumes, which wipes the database. Use the plain down for normal stops and the -v form only when you mean to reset the data.

Rebuilds need one extra flag. After you change the Dockerfile or the app code, run docker compose up -d --build so Compose rebuilds the image instead of reusing the old one. A plain up after a code change can leave you testing yesterday’s image, which is a common source of confusion. If a service keeps crashing, run it in the foreground with docker compose up web and read the first error before you change anything. The guide to isolating multiple sites on one VPS with Docker uses the same service and network ideas on a server, so the mental model carries over when you deploy.

Environment variables and overrides

Keep secrets out of the Compose file. A .env file in the same folder fills in ${VAR} references, and Compose reads it automatically. Put real values in .env, keep a .env.example with empty keys in version control, and never commit the real file. For differences between machines, use an override file. Compose merges compose.override.yaml automatically, so your local file can mount your code folder for live reload while the shared file stays clean:

services:
  web:
    volumes:
      - .:/app
      - /app/node_modules
    command: npm run dev

The second volume line is the trick that makes the mount work: it keeps the container’s own node_modules in place while your code folder is mounted over the app directory. Without it, your host node_modules would replace the container’s and break the app. If your stack grows past local development, the comparison in Docker Compose vs Kubernetes, when to use each will help you decide when a single host stops being enough.

Volumes, networks, and health checks

Compose gives you three tools that keep a stack stable. Volumes hold data that must survive a container restart, such as the database files in the example above. Named volumes are the default pick, because Compose manages their location and they work the same on every machine. Networks connect services by name, and Compose creates one per project automatically, so two projects on the same host cannot see each other’s services by accident.

Health checks fix the startup race. The web service starts at once by default, even if the database is still booting, and the first requests fail. Add a condition to depends_on so the app waits for the database to report healthy:

depends_on:
  db:
    condition: service_healthy
  cache:
    condition: service_started

Pair that with a healthcheck on the database service that runs pg_isready. A docker compose depends_on healthcheck pair is what stops the app from starting before the database is ready. The same pattern applies to MySQL and Postgres alike, and our guide to MySQL vs PostgreSQL tuning on a VPS with limited RAM covers which database fits a small host. For image size, keep the app image slim with the techniques in multi-stage Dockerfiles that shrink images, because a small image makes every rebuild and every fresh checkout faster.

When Compose is the wrong tool

Compose runs on one host. That is a strength for local work and small servers, and a limit when you need several machines, automatic restarts across hosts, or rolling updates with no downtime. Kubernetes and Swarm solve those problems, but they add real operational work: clusters, networking, and storage that you have to run and watch. A single VPS with Compose can carry a surprising amount of traffic before that work pays for itself.

The rule is simple. If your stack fits on one machine and your team can restart it by hand, Compose is enough. When deploys need zero downtime across several machines or the traffic outgrows one host, move the parts that need scaling and keep Compose for local development, where it stays the fastest way to run the whole app with one command.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.