Docker Compose
Docker Compose lets you start and manage the containers that make up an application together. A YAML file records what runs, how the containers connect, and where their data is stored.
What Docker Compose does
A web app might need a database and a cache before you can run it. Starting each one with docker run works, but everyone on the team has to get the same ports, settings, and storage options right. With Docker Compose, those settings live in a compose.yaml file. Run docker compose up -d to start the stack in the background, and use docker compose ps to see what's running.
How the file maps to containers
Each entry under services is a recipe for running one or more containers. A service can use a prebuilt image or a build configuration that tells Docker how to build one. That is the difference between a Dockerfile and a Compose file: a Dockerfile describes how to build an image; Compose describes how to run the parts of an application together.
By default, services share a project network and can look each other up by service name. A container connecting to PostgreSQL uses db:5432 if the database service is called db. It does not use localhost, which points back to the container making the connection. The ports setting publishes a container port on the Docker host; it isn't needed for communication between services on the same network.
Example: a database and a browser UI
services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: demo-only
volumes:
- pgdata:/var/lib/postgresql/data
adminer:
image: adminer:4
ports:
- "127.0.0.1:8080:8080"
volumes:
pgdata:
Start this on your machine and open http://127.0.0.1:8080 to reach Adminer. Choose PostgreSQL and sign in with server db, user postgres, and password demo-only. The browser reaches Adminer through the published port, but it's Adminer, not the browser, that connects to db. The password is just for this local example; don't commit real credentials. docker compose down removes the containers and project network while keeping the named volume. Adding -v removes that volume too, so it can erase the database.
When to use it
Compose is common in local development and CI integration tests, where you want to bring up the same dependencies on demand. It can also run a small deployment on one host. It does not provide cluster scheduling or move workloads to another machine if that host fails, so it isn't a substitute for Kubernetes when those features matter.
What interviewers ask
A typical debugging question: the API starts after the database, so why does it get a connection error? Short-form depends_on controls startup order, not readiness. To wait for the database at startup, define a healthcheck and set the dependency condition to service_healthy. That's separate from handling a dropped connection later: the application still needs its own retry or reconnection behavior.