Install and Use Docker Compose on Ubuntu 22.04 | Best Setup
This guide will walk you through How To Install and Use Docker Compose on Ubuntu 22.04. Docker Compose is a powerful tool for managing multi-container Docker applications. It allows you to define and run complex applications with multiple services, each running in its own container. This is crucial for microservices architectures, where each service can be independently managed and scaled.
Let’s dive into setting up Docker Compose on your Ubuntu 22.04 system.
Before we begin, ensure you meet the necessary prerequisites.
1. Requirements for Docker Compose Setup
Before installing Docker Compose, you’ll need:
- A non-root user with sudo privileges. Refer to our guide on Initial Server Setup with Ubuntu 22.04 for assistance.
- Docker installed on your server. Follow our article on How to Install and Use Docker on Ubuntu 22.04.
Once you’ve satisfied these requirements, you can proceed with the installation of Docker Compose on Ubuntu 22.04.
2. Install Docker Compose on Ubuntu 22.04
To install Docker Compose, first, determine the latest version available on the releases page. At the time of writing, the latest version is 2.11.2.
Download the 2.11.2 release using the following command:
# mkdir -p ~/.docker/cli-plugins/
# curl -SL https://github.com/docker/compose/releases/download/v2.11.2/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
Next, grant the downloaded file executable permissions:
sudo chmod +x ~/.docker/cli-plugins/docker-compose
Verify the installation by checking the Docker Compose version:
docker compose version
You should see output similar to:
**Output**
Docker Compose version v2.11.2
Congratulations! You have successfully installed Docker Compose on Ubuntu 22.04.
Now, let’s explore how to create a YAML file and define the minimum configuration needed to run a container using Docker Compose.
3. Set Up a YAML file on Ubuntu 22.04
In this step, we’ll create a simple web server environment using the official Nginx image from DockerHub. This environment will serve a static HTML file.
First, create a new directory and navigate into it:
# mkdir ~/compose-demo
# cd ~/compose-demo
Create a folder to serve as the document root for your Nginx environment:
mkdir app
Create an index.html
file within the app
directory using your preferred text editor (e.g., vi):
vi app/index.html
Paste the following HTML content into the file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Docker Compose Demo</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>
<h1>This is a Docker Compose Demo Page.</h1>
<p>This content is being served by an Nginx container.</p>
</body>
</html>
Save and close the index.html
file.
Now, create the docker-compose.yml
file:
vi docker-compose.yml
Paste the following configuration into the file:
version: '3.7'
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./app:/usr/share/nginx/html
The docker-compose.yml
file begins with the version
definition, indicating the Docker Compose configuration version. In this configuration, we define a service named web
that uses the nginx:alpine
image, maps port 8000 on the host to port 80 in the container, and mounts the ./app
directory on the host to /usr/share/nginx/html
in the container, which is Nginx’s default document root.
Save and close the docker-compose.yml
file.
Let’s move on to using Docker Compose to run a container on Ubuntu 22.04.
4. Use Docker Compose to Run a Container
Now, you can use Docker Compose to bring up your environment. The following command will download the necessary Docker images, create a container for the web
service, and run the containerized environment in detached mode (background):
docker compose up -d
You should see output similar to:
**Output**
[+] Running 7/7
⠋ web Pulled 4.9s
⠋ 213ec9aee27d Pull complete 1.2s
⠋ 2546ae67167b Pull complete 1.9s
⠋ 23b845224e13 Pull complete 1.9s
⠋ 9bd5732789a3 Pull complete 2.0s
⠋ 328309e59ded Pull complete 2.1s
⠋ b231d02e5150 Pull complete 2.2s
[+] Running 2/2
⠋ Network compose-demo_default Created 0.1s
⠋ Container compose-demo-web-1 Started 0.6s
Your container is now running.
To verify that the container is active, run:
docker compose ps
Your output should be similar to:
**Output**
NAME COMMAND SERVICE STATUS PORTS
compose-demo-web-1 "/docker-entrypoint.…" web running 0.0.0.0:8000->80/tcp, :::8000->80/tcp
You can now access the Docker Compose demo page on Ubuntu 22.04 by entering your server’s IP address in your web browser, followed by :8000
:
http://your-IP-address:8000
You should see a page displaying "This is a Docker Compose Demo Page." served by an Nginx container.
Next, let’s explore how to manage Docker Compose commands on Ubuntu 22.04.
5. Manage Docker Compose Commands
To view the logs generated by your Nginx container, use:
docker compose logs
The output will display the Nginx logs:
**Output**
compose-demo-web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
compose-demo-web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
compose-demo-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
compose-demo-web-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
compose-demo-web-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
....
To pause the container without affecting its current state, use:
docker compose pause
**Output**
[+] Running 1/0
⠋ Container compose-demo-web-1 Paused 0.0s
To unpause the container, use:
docker compose unpause
**Output**
[+] Running 1/0
⠋ Container compose-demo-web-1 Unpause... 0.0s
To stop the container, use:
docker compose stop
**Output**
[+] Running 1/1
⠋ Container compose-demo-web-1 Stopped 0.2s
To remove the containers, networks, and volumes associated with the environment, use:
docker compose down
**Output**
[+] Running 2/2
⠋ Container compose-demo-web-1 Removed 0.0s
⠋ Network compose-demo_default Removed 0.1s
This command does not remove the base image. You can restart the environment using docker compose up
.
To remove the base image, use:
docker image rm nginx:alpine
Conclusion
You have now learned How To Install and Use Docker Compose on Ubuntu 22.04, and have set up a simple containerized environment. You’ve also learned how to manage this environment using various Compose commands.
Alternative Solutions
While Docker Compose is a very common and useful tool, there are alternative approaches to managing multi-container applications on Ubuntu 22.04. Here are two such alternatives:
1. Using Docker Swarm
Docker Swarm is Docker’s built-in orchestration tool. Unlike Docker Compose, which is primarily for single-host deployments, Docker Swarm is designed for managing clusters of Docker nodes.
Explanation:
Docker Swarm allows you to initialize a swarm, add worker nodes, and deploy services across the cluster. Services are defined similarly to Docker Compose, using a YAML file (though there are some differences in syntax). Swarm then manages the deployment, scaling, and networking of these services across the cluster.
How it Differs:
- Cluster Focus: Swarm is designed for multi-host deployments, providing features like service discovery, load balancing, and rolling updates across multiple machines.
- Scalability: Swarm is inherently more scalable than Docker Compose, as it can distribute containers across multiple nodes.
- Complexity: Swarm setup and management can be more complex than Docker Compose, especially for simple single-host deployments.
Example:
First, initialize a Docker Swarm on your manager node:
docker swarm init --advertise-addr <MANAGER-IP>
Then, create a docker-stack.yml
file (similar to docker-compose.yml
):
version: "3.7"
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./app:/usr/share/nginx/html
deploy:
replicas: 3 # Run 3 instances of the web service
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
Deploy the stack:
docker stack deploy -c docker-stack.yml myapp
This will deploy your Nginx service across the swarm, with three replicas, providing inherent redundancy and scaling capabilities.
2. Kubernetes via Minikube or MicroK8s
Kubernetes (often shortened to K8s) is a powerful container orchestration system that is more complex than Docker Compose or Swarm, but also provides a far richer feature set. Minikube and MicroK8s are lightweight Kubernetes distributions designed for local development and testing.
Explanation:
Kubernetes uses a declarative approach, where you define the desired state of your application using YAML files. Kubernetes then works to ensure that the actual state matches the desired state. It provides features like service discovery, load balancing, automated rollouts and rollbacks, self-healing, and horizontal scaling.
How it Differs:
- Complexity: Kubernetes is significantly more complex to set up and manage than Docker Compose.
- Features: Kubernetes offers a much wider range of features, including advanced deployment strategies, service discovery, load balancing, and autoscaling.
- Scalability: Kubernetes is designed for large-scale, production deployments and can easily manage hundreds or thousands of containers.
Example (using Minikube):
First, install Minikube:
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
Start Minikube:
minikube start
Create a deployment.yaml
file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: html-volume
volumes:
- name: html-volume
hostPath:
path: /path/to/your/app # Replace with the actual path to your 'app' directory
Create a service.yaml
file:
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- protocol: TCP
port: 8000
targetPort: 80
type: NodePort
Apply the deployments and services:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Access your application:
minikube service web-service --url
This will output the URL where your application is accessible.
These alternative solutions offer different levels of complexity and features, allowing you to choose the tool that best fits your needs. For simple single-host deployments, Docker Compose is often the easiest and most efficient choice. For more complex, multi-host deployments, Docker Swarm or Kubernetes may be more appropriate. Remember to explore and evaluate these options based on your specific requirements and goals. Install and Use Docker Compose on Ubuntu 22.04 is a great starting point, but expanding your knowledge of these alternatives will make you a more versatile Docker user.