Install and Use Docker Compose on Ubuntu 22.04

[rank_math_breadcrumb]

Share your love

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 manage the deployments of many different Docker containers centrally. 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.

You can now proceed to the guide steps below on the Orcacore website to complete Docker Compose setup on Ubuntu 22.04.

Steps To Install and Use Docker Compose on Ubuntu 22.04

Before you start to install and use Docker Compose, you need to meet some requirements first.

1. Requirements for Docker Compose Setup

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, 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.

2. 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. At the time of writing this guide, the latest release is 2.11.2.

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.

3. 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:

mkdir app

Here you need to create a new index.html file with your favorite text editor, here we use vi editor:

vi app/index.html

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

When you are finished, save and close the file.

Now you can create the YAML file with the following command:

vi docker-compose.yml

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 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.

4. Use Docker Compose to Run a Container

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 be 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”:

http://your-IP-address:8000

You will see a page like this:

Docker compose demo page

Here we want to show you how to manage Docker Compose commands on Ubuntu 22.04.

5. 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 be 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
docker compose remove image

That’s it, you are done.

Conclusion

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.

You may also like to read the following articles:

Docker Compose Installation on Ubuntu 24.04

Set up Docker Compose on AlmaLinux 9

Install PHP Composer on Ubuntu 22.04

Share your love

Stay informed and not overwhelmed, subscribe now!