Installing Docker Compose
Docker Compose is typically included with Docker Desktop or can be installed separately. Below is how to install the Docker Compose CLI plugin in a Linux environment.
Installing the Docker Compose CLI plugin on Linux
Install the latest Docker Compose CLI plugin by downloading it and granting execute permissions. Use this method to install Docker Compose if you are not using Docker Desktop.
Download and Install Docker Compose Plugin
sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Download the latest version of the Docker Compose CLI plugin, grant execute permissions, and place it in the appropriate path.
Check Docker Compose Version
docker compose version
Verify the Docker Compose version to confirm that the installation is complete.
Key Docker Compose Commands
Docker Compose is used to manage services defined in a `docker-compose.yml` file. Easily control multi-container applications with the options below.
1. Building and Running Applications
2. Stopping and Removing Applications
3. Managing Services
Generated command:
Try combining the commands.
Description:
`docker compose` Executes the command.
Combine the above options to virtually execute commands with AI.
`docker-compose.yml` File Structure
Docker Compose uses a `docker-compose.yml` file to define services, networks, volumes, and more. This file is written in YAML format and clearly represents the components of the application and their relationships.
Basic Structure
The `docker-compose.yml` file has top-level keys such as `version`, `services`, `networks`, and `volumes`. The most important part is the `services` section, which defines each container that makes up the application.
Simple Web Application Example
version: '3.8'
services:
web:
image: nginx:latest
ports:
- '80:80'
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- api
api:
build: .
ports:
- '3000:3000'
environment:
NODE_ENV: production
volumes:
data_volume:
A `docker-compose.yml` example for a simple application consisting of an Nginx web server and a Node.js backend service.
Key Explanations
Explanations of key settings frequently used in the `docker-compose.yml` file.
- version: Specifies the Compose file format version. It is recommended to use the latest version (e.g., '3.8').
- services: This section defines each container service that makes up the application.
- image: Specifies the name of the Docker image to be used for the service. (e.g., `nginx:latest`)
- build: Specifies the path to the Dockerfile to build the image. (e.g., `.`)
- ports: Defines port mappings between the host and the container. (e.g., `- '80:80'`)
- volumes: Defines volume mounts for data persistence. (e.g., `- ./data:/app/data`)
- environment: Defines environment variables to be set inside the container.
- depends_on: Defines dependencies between services. (e.g., when the `db` service must start before the `web` service)
- networks: Defines the network(s) the service will connect to.
Usage Examples
Learn how to efficiently manage multi-container applications through practical usage examples of Docker Compose commands.
Start a Docker Compose project (in the background)
docker compose up -d
Reads the `docker-compose.yml` file in the current directory and starts all services in the background.
Restart a specific service
docker compose restart web
Restarts only the 'web' service defined in `docker-compose.yml`.
View logs in real-time
docker compose logs -f
Streams logs from all services to the terminal in real-time to monitor application behavior.
Stop application and remove related resources
docker compose down -v
Stops and removes all running service containers, and also removes defined networks and anonymous volumes.
Specify the path to the Docker Compose file
docker compose -f /path/to/my-app/docker-compose.prod.yml up -d
Runs a Compose file with a different name or located in a different path instead of the default `docker-compose.yml`.