Ubuntu

How to Install Docker Compose on Ubuntu 22.04

Docker is a software platform utilized for the development of Containers-based (lightweight execution environments) applications that have the capability of sharing the operating system kernel in isolation. Whereas Docker Compose is a utility that permits you to run multi-container application setups based on YAML definitions. It creates fully customized environments with multiple containers that have the capability to share data volumes and networks using service definitions.

This write-up will demonstrate the method of installing Docker Compose on Ubuntu 22.04. So, let’s start!

Note: Before moving towards the installation of Docker Compose, you should install Docker on your Ubuntu 22.04 system; If you do not have it already.

How to install Docker on Ubuntu 22.04

You must follow the below-given procedure for installing Docker on Ubuntu 22.04.

Step 1: Update system repositories

First of all, open up the terminal by hitting “CTRL+ALT+T” in Ubuntu 22.04 and write out the below-given commands for updating the system repositories:

$ sudo apt update

Upgrade the system packages as well:

$ sudo apt upgrade

Step 2: Install required dependencies

After updating the system packages, the next step is to install required dependencies for Docker:

$ sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y

Step 3: Adding Docker repository to system sources

When a Docker repository is added to the system sources, it makes the Docker installation easier and provides faster updates.

To add the Docker repository to the system sources, firstly, import the Docker GPG key required for connecting to the Docker repository:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

After doing so, execute the following command for adding the Docker repository to your Ubuntu 22.04 system sources list:

$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: Update system packages

After adding Docker repository to the system sources, again update the system packages:

$ sudo apt update

Step 5: Install Docker on Ubuntu 22.04

At this point, our Ubuntu 22.04 system is all ready for the Docker installation:

$ sudo apt install docker-ce

Note that we are utilizing the “docker-ce” package instead of “docker-ie” as it is supported by the official Docker repository:

Enter “y” to permit the Docker installation to continue:

The below-given error-free output indicates that Docker is successfully installed on our Ubuntu 22.04 system:

Step 6: Verify Docker status

Now, execute the below-given “systemctl” command to verify if the Docker is currently active or not on your system:

$ sudo systemctl status docker

Now, let’s check out the method of installing Docker Compose on Ubuntu 22.04.

How to install Docker Compose on Ubuntu 22.04

You must follow the below-given procedure for installing Docker Compose on Ubuntu 22.04.

Step 1: Download Docker Compose package

First of all, verify the latest version of the Docker Compose package from the release page. For example, at this moment, the most stable version of Docker Compose is “2.5.0”.

So, we will create a directory with the help of the following “mkdir” command:

$ mkdir -p ~/.docker/cli-plugins/

After doing so, utilize the below-given “curl” command for installing Docker Compose on Ubuntu 22.04:

$ curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

Step 2: Docker Compose Installation

In the next step, set the executable permissions to the “docker-compose” command:

$ chmod +x ~/.docker/cli-plugins/docker-compose

Then, verify the version of the installed Docker Compose:

$ docker compose version

As you can see, on our Ubuntu 22.04 system, we have installed Docker Compose version “2.5.0”:

Step 3: Create a docker-compose.yml file

Before setting up a “docker-compose.yml” file, write out the below-given “mkdir” command for creating a new directory in “home”:

$ mkdir ~/compose-demo

Then, switch to the newly created directory:

$ cd ~/compose-demo

Now, we will create a new application folder named “app” that will serve as a document root for our Nginx environment:

$ mkdir app

Next, open the “nano” editor and create an “index.html” file:

$ nano app/index.html

In the opened file, write out the following code and press “CTRL+O” to save it:

<!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/>

</head>

<body>

<h1>This is a Docker Compose Demo Page.</h1>

<p>This content is being served by linuxhint.com</p>

</body>

</html>

At this point, we are all ready to set up the “docker-compose.yml” file. To do so, create “docker-compose.yml” file:

$ nano docker-compose.yml

Then, add the below-given code in it:

version: '3.7'

services
:

web
:

image
: nginx:alpine

ports
:

- "8000:80"

volumes
:

- ./app:/usr/share/nginx/html

Now, move to the next step.

Step 4: Run Docker Compose

As soon as your environment is up, you can run the below-given “docker-compose” command. This command will create a container for web service, download the essential Docker images, and execute the containerized environment in the background:

$ docker-compose up -d

You can also validate that the container is active or not with the help of the following command:

$ sudo docker-compose ps

The above-given output indicates that we can now access our demo application by browsing the “localhost:8000” web page:

http://localhost:8000/

If you have carefully followed the previously given steps, then you will see the following web page:

We have compiled the simplest method to install Docker Compose on Ubuntu 22.04.

Conclusion

For the installation of Docker Compose, firstly, you have to utilize the “$ sudo apt install docker-ce” command to install Docker on your system. Then, download the latest version of the Docker Compose package from the release page and install it. After doing so, create a “docker-compose.yml” file and execute the “$ docker-compose up -d” command. This write-up demonstrated the method of installing Docker Compose on Ubuntu 22.04.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.