Docker Compose Installation
Docker Compose is a powerful tool that allows you to define and run multi-container Docker applications. In this guide, we'll walk through the process of installing Docker Compose on various operating systems.
Prerequisites
Before installing Docker Compose, you need to have Docker Engine installed on your system.
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes. Then, with a single command, you create and start all the services from your configuration.
Using Docker Compose is basically a three-step process:
- Define your app's environment with a
Dockerfile
- Define the services that make up your app in
docker-compose.yml
- Run
docker compose up
to start and run your entire app
Installation Methods
Let's go through the installation process for different operating systems:
Installing on Windows
If you've installed Docker Desktop for Windows, Docker Compose is already included. To verify the installation:
docker compose version
If you need to install Docker Compose separately:
- Download the latest Docker Compose binary from the official GitHub releases page
- Move the binary to a directory in your PATH
- Make it executable
Installing on macOS
Similar to Windows, if you've installed Docker Desktop for Mac, Docker Compose is included. To verify:
docker compose version
For manual installation:
- Use Homebrew (recommended):
brew install docker-compose
- Or download the binary from GitHub:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Installing on Linux
On most Linux distributions, you can install Docker Compose via the package manager:
For Ubuntu/Debian:
sudo apt update
sudo apt install docker-compose-plugin
For Fedora:
sudo dnf install docker-compose-plugin
Manual installation on any Linux distribution:
# Download the latest version
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Apply executable permissions
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
docker compose version
Docker Compose V1 vs V2
Docker Compose is available in two major versions:
- V1 (Legacy): The standalone
docker-compose
binary - V2: Integrated as a plugin within Docker CLI as
docker compose
(note the space instead of a hyphen)
The newer V2 is recommended and is what we'll use in examples. The main difference in usage is:
- V1:
docker-compose up
- V2:
docker compose up
Verifying Your Installation
After installation, you should verify that Docker Compose is working correctly:
docker compose version
You should see output similar to:
Docker Compose version v2.20.2
Installation Diagram
Here's a diagram showing the relationship between Docker Engine and Docker Compose:
Creating Your First Docker Compose File
Now that you have Docker Compose installed, let's create a simple docker-compose.yml
file:
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
Save this file as docker-compose.yml
and run:
docker compose up
This will start an Nginx web server accessible at http://localhost:8080.
Troubleshooting Installation Issues
Permission Denied
If you encounter a "Permission denied" error:
sudo chmod +x /usr/local/bin/docker-compose
Command Not Found
If docker compose
command is not found after installation:
- Ensure the binary is in your PATH
- For V2, make sure the Docker CLI plugins directory exists:
mkdir -p ~/.docker/cli-plugins
- Create a symbolic link to the Docker Compose binary:
ln -s /usr/local/bin/docker-compose ~/.docker/cli-plugins/docker-compose
Summary
In this guide, we've covered:
- What Docker Compose is and why it's useful
- How to install Docker Compose on Windows, macOS, and Linux
- The difference between Docker Compose V1 and V2
- How to verify your installation
- A simple example to get started
- Common troubleshooting tips
Docker Compose is an essential tool for developing multi-container applications with Docker. Now that you have it installed, you can proceed to learn more about creating and managing Docker Compose configurations.
Exercises
- Install Docker Compose on your system and verify the installation.
- Create the example
docker-compose.yml
file and start the Nginx container. - Modify the example to include a second service, such as a database.
- Try stopping your services with
docker compose down
.
Additional Resources
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)