Skip to main content
Docker Compose lets you define and run multi‐container applications from a single YAML file. Instead of managing each docker run command manually, you declare services, networks, and volumes in docker-compose.yaml and launch the entire stack with one command:

1. Recap: Running Multiple Containers with docker run

To illustrate the complexity of manual container orchestration, imagine starting four services individually:
Each container runs, but wiring them together (networking, links, ports) quickly becomes tedious.

2. Defining Services in Compose

In docker-compose.yaml, all services and their options live under the services: key:
Then bring the entire stack up:
Every configuration change is version‐controlled in your Compose file—no more hunting down individual CLI commands.

3. Sample Voting Application Architecture

We’ll demonstrate Compose using Docker’s sample voting app, composed of:
  • Voting app (Python web UI): records “cats” or “dogs” votes in Redis.
  • Worker (.NET): reads votes from Redis and updates PostgreSQL.
  • PostgreSQL: stores persistent vote counts.
  • Result app (Node.js web UI): displays tallied results from PostgreSQL.
The image is a diagram of a sample voting application architecture, showing components like a voting app in Python, an in-memory database using Redis, a worker in .NET, and a result app connected to a PostgreSQL database. It also includes a simple voting result table for cats and dogs.

4. Manual Stack Deployment with docker run

If images already exist on Docker Hub, you might start containers like this:
However, without networking configuration, containers cannot communicate.

4.1 Linking Containers (Deprecated)

The --link flag creates /etc/hosts entries for cross-container DNS:
In your Node.js code, you’d connect via the hostname db:
Container links are deprecated. Instead, use user‐defined networks as shown in the next sections.

5. From docker run to docker-compose.yaml

Convert your verified docker run commands into a Compose file:
Compose definition:
Launch with:

6. Building Local Images in Compose

If your service images are built locally, specify a build: context instead of image::
Compose will build these images from each directory’s Dockerfile before starting the containers.

7. Compose File Versions Compared

Different Compose versions introduce new features and schemas. Refer to this summary:

Examples

Version 1

Version 2

Version 3

For full details, see the Docker Compose file reference.

8. Defining Custom Networks

By default, Compose creates a single bridge network. You can isolate traffic with multiple networks:
The image is a diagram illustrating a Docker Compose setup with components like a voting app, result app, Redis, database, and worker, connected to represent a system architecture.
Containers on front-end can only communicate with those also on back-end.

Next Steps

Now that you’ve mastered service definitions, version schemas, and custom networks, try creating and running your own docker-compose.yaml configurations in the exercises below.

Watch Video