This guide intends to teach you How To Install and Use Docker Compose on Ubuntu 22.04.
Docker Compose is a tool you can use to centrally manage the deployments of many different Docker containers. It’s an important tool for any application that needs multiple microservices, as it allows each service to easily be in a separately managed container.
Steps To Install and Use Docker Compose on Ubuntu 22.04
Before you start to install and use Docker Compose, you need some requirements first.
You need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can check our article about the Initial Server Setup with Ubuntu 22.04.
Also, a Docker is installed on your server. You can follow our article about How to Install and Use Docker on Ubuntu 22.04.
When you are done with these requirements, you can start to install Docker Compose on Ubuntu 22.04.
Install Docker Compose on Ubuntu 22.04
To install Docker compose you need to confirm the latest version of Docker Compose on the releases page.
Now run the following command to download the 2.11.2 release:
# 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
Then, you need to set the correct permissions with the following command:
sudo chmod +x ~/.docker/cli-plugins/docker-compose
To see if your installation was successful run the following command:
docker compose version
In your output you will see:
Output Docker Compose version v2.11.2
Now you have successfully installed Docker Compose on Ubuntu 22.04.
Let’s see how to set up the YAML file, and the minimal configuration required to run a container using Docker Compose.
Set Up a YAML file on Ubuntu 22.04
To set up a YAML file, you need to create a web server environment using the official Nginx image from DockerHub. This containerized environment will serve a single static HTML file.
First, create a new directory in your home folder, then move into it with the following command:
# mkdir ~/compose-demo # cd ~/compose-demo
Then, create a folder to serve as the document root for your Nginx environment with the following command:
Here you need to create a new index.html file with your favorite text editor, here we use vi:
Paste the following content into your 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/[email protected]/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>
When you are finished, save and close the file.
Now you can create the YAML file with the following command:
Then, paste the following content 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 typically starts off with the version definition. This will tell Docker Compose which configuration version we’re using.
Save and close the file, when you are done.
Let’s see how to use Docker Compose on Ubuntu 22.04.
Use Docker Compose to Run a Container on Ubuntu 22.04
At this step, you can execute Docker Compose to bring our environment up.
The following command will download the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:
docker compose up -d
In your output you will see:
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 up and running.
To see that your container is active, run the following command:
docker compose ps
Your output should similar to this:
Output NAME COMMAND SERVICE STATUS PORTS compose-demo-web-1 "/docker-entrypoint.…" web running 0.0.0.0:8000->80/tcp, :::8000->80/tcp
Now you can access the Docker Compose demo page on Ubuntu 22.04 by typing your server’s IP address in your web browser followed by “:8000”:
You will see a page like this:
Here we want to show you how to manage Docker Compose commands on Ubuntu 22.04.
Manage Docker Compose Commands
To check the logs produced by your Nginx container, you can use the following command:
docker compose logs
Your output should similar to this:
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 ....
If you want to pause the container without changing the current state of your containers, you can use the following command:
docker compose pause
Output [+] Running 1/0 ⠿ Container compose-demo-web-1 Paused 0.0s
You can unpause the container with the following command:
docker compose unpause
Output [+] Running 1/0 ⠿ Container compose-demo-web-1 Unpause... 0.0s
To stop a container you can use the Docker Compose stop command on Ubuntu 22.04:
docker compose stop
Output [+] Running 1/1 ⠿ Container compose-demo-web-1 Stopped 0.2s
If you want to remove the containers, networks, and volumes associated with this containerized environment, you can use the following command:
docker compose down
Output [+] Running 2/2 ⠿ Container compose-demo-web-1 Removed 0.0s ⠿ Network compose-demo_default Removed 0.1s
This command will not remove the base image, you can bring it up again with the docker-compose-up command.
To remove the base image from your system, you can use the following command:
docker image rm nginx:alpine
Output Untagged: nginx:alpine Untagged: [email protected]:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c37408642bf5d Deleted: sha256:804f9cebfdc58964d6b25527e53802a3527a9ee880e082dc5b19a3d5466c43b7 Deleted: sha256:bd096861e89a30cb348f70e018a3a79dd98cb4e7fecc5c5f5452992446338923 Deleted: sha256:0f5b9e6d2cd4178f00e7877608da01445216c05b1e599cee52ff6cc733c80c09 Deleted: sha256:4064741cd422895de2f6458f4ec47810fe762d488a3064a9c9bfd36a0a7559f9 Deleted: sha256:5ba70ded614c06d3792512671b3eac4c7af91594a169a8673461c38452f22059 Deleted: sha256:c3cb61deaecfc83beb4bc37d56eb6e3d24662947cdac0dc722648de0e838849c Deleted: sha256:994393dc58e7931862558d06e46aa2bb17487044f670f310dffe1d24e4d1eec7
That’s it, you are done.
At this point, you have learned to install and use Docker Compose on Ubuntu 22.04 and set up a containerized environment based on an Nginx web server image. Also, you learn how to manage this environment using Compose commands.
Hope you enjoy this part of the Docker Tutorials on the orcacore website.