Introduction
So far, we've focused on single containers. However, most real-world applications consist of multiple interconnected services (e.g., a web server, a database, a caching layer). Manually running and linking these containers with docker run commands can become cumbersome. This lesson introduces Docker Compose, a tool that simplifies the definition and management of multi-container Docker applications. You'll learn how to define your entire application stack in a single YAML file.
Key Concepts
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file (typically named docker-compose.yml) to configure your application's services. Then, with a single command, you can create and start all the services from your configuration.
-
Declarative: Describes your entire application stack declaratively in a
docker-compose.ymlfile. -
Multi-Service: Manages multiple services (containers) that work together.
-
Single Command: Simplifies starting, stopping, and rebuilding your entire application with commands like
docker compose up.
docker-compose.yml File Structure
The docker-compose.yml file is at the heart of Docker Compose. It typically consists of a version field and a services section, which defines the individual containers that make up your application.
-
version: Specifies the Compose file format version (e.g.,3.8). This impacts available features. -
services: Defines the application's individual components (containers).
Each service entry represents one container and includes:
image: The Docker image to use (e.g.,nginx:latest).build: Path to a Dockerfile to build a custom image for this service.ports: Port mappings (e.g.,"8080:80").environment: Environment variables (e.g.,DB_PASSWORD: mysecret).volumes: Volume mounts (e.g.,db_data:/var/lib/mysql).networks: Which networks the service connects to.depends_on: Expresses dependencies between services to control startup order (though it doesn't wait for readiness).
-
volumes(top-level): Defines named volumes that can be shared between services. -
networks(top-level): Defines custom networks for services to communicate over.
Key Docker Compose Commands
-
docker compose up: Builds, creates, starts, and attaches to containers for all services defined in thedocker-compose.ymlfile.docker compose up -d: Runs containers in detached mode (in the background).docker compose up --build: Rebuilds images before starting containers.
-
docker compose ps: Lists running containers for the application. -
docker compose stop: Stops running containers without removing them. -
docker compose down: Stops and removes containers, networks, and volumes defined in thedocker-compose.ymlfile.
Example/Code
Here's a docker-compose.yml file for a simple web application with an Nginx reverse proxy and a Node.js backend:
yamlversion: '3.8' services: web: image: nginx:alpine ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro depends_on: - app networks: - my-app-network app: build: . # Assuming your Node.js Dockerfile is in the current directory ports: - "3000:3000" # For direct access during dev, optional environment: NODE_ENV: development networks: - my-app-network networks: my-app-network: driver: bridge
To run this application, place the docker-compose.yml in a directory along with your Node.js application files (including Dockerfile and nginx.conf).
Then, from that directory, run:
bashdocker compose up -d
Summary/Key Takeaways
-
Docker Compose simplifies defining and running multi-container Docker applications.
-
Applications are defined declaratively in a
docker-compose.ymlfile. -
The
servicessection defines individual containers with their images, ports, volumes, and networks. -
Key commands include
docker compose up,docker compose ps,docker compose stop, anddocker compose down.