How to Install Docker Compose on Ubuntu 20.04: A Practical Guide
Docker simplifies application management within containers. Are you looking to streamline multi-container deployments on Ubuntu 20.04? This guide offers a step-by-step approach to install Docker Compose and manage your containerized applications effectively.
Why Use Docker Compose?
Docker Compose orchestrates multi-container applications, making it easy to manage complex environments. Instead of managing individual containers, Docker Compose uses YAML files to define and manage services, networks, and volumes. By using it, you can:
- Simplify application deployment.
- Manage multi-container environments easily.
- Define all application services in a single file.
Prerequisites for Installing Docker Compose
Before installing Docker Compose, ensure you have the following:
- An Ubuntu 20.04 server or local machine.
- A non-root user with sudo privileges.
- Docker installed (Steps 1 & 2 of the Docker on Ubuntu 20.04 tutorial).
With these prerequisites in place, you are ready to proceed with the installation.
Step 1: Installing Docker Compose on Ubuntu
This section focuses on installing the most updated stable version of Docker Compose from its official GitHub repository.
Download the Latest Docker Compose Version
First, visit the Docker Compose releases page to find the latest version. Then, use the following curl
command, replacing 1.29.2
with the version you found:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Set Executable Permissions
Grant execute permissions to the downloaded binary:
sudo chmod +x /usr/local/bin/docker-compose
Verify the Installation using the version command
Check the installation using below, you should see the version details to confirm correct set up.
docker-compose --version
Step 2: Setting Up a docker-compose.yml
File
A docker-compose.yml
file defines your application's services, networks, and volumes. This example creates a simple web server environment using the official Nginx image.
Create a Project Directory
mkdir ~/compose-demo
cd ~/compose-demo
mkdir app
Create an index.html
File
Create a basic HTML file to be served by Nginx:
nano app/index.html
Add the following HTML content:
Define the docker-compose.yml
File
Create a docker-compose.yml
file:
nano docker-compose.yml
And add the following configuration:
In this file:
version
: Specifies the Docker Compose file format version.services
: Defines the services that make up your application.web
: Service uses thenginx:alpine
image.ports
: Maps port 8000 on the host to port 80 on the container.volumes
: Mounts theapp
directory on the host to/usr/share/nginx/html
in the container.
Step 3: Running Docker Compose
Start your application using docker-compose, which will pull images, create networks, and start containers based on your configuration.
docker-compose up -d
This command runs the environment in detached mode (-d
), meaning it runs in the background.
Verify the Application
Check if the container is running:
docker-compose ps
Visit localhost:8000
or your_server_ip:8000
in your browser to see the demo page. Any changes to index.html
will be reflected immediately due to the shared volume.
Step 4: Mastering Docker Compose Commands
Effectively manage your applications using these commands:
docker-compose logs
: View container logs for debugging.docker-compose pause
: Temporarily pause services.docker-compose unpause
: Resume paused services.docker-compose stop
: Stop running services.docker-compose down
: Stop and remove containers, networks, and volumes.docker image rm nginx:alpine
: Remove the base image from your system.
Conclusion
You’ve successfully installed Docker Compose on Ubuntu 20.04 and deployed a simple web application using Nginx. With Docker Compose, managing multi-container applications becomes more manageable and efficient. You can further explore more complex setups, such as WordPress or Node.js applications, using Docker Compose.