Jul 3, 2023 5 min read

How to Install Docker Compose on Ubuntu 22.04

Install Docker Compose on Ubuntu 22.04 with our step-by-step tutorial. It simplifies the deployment of multi-container applications.

Install Docker Compose on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Docker Compose on Ubuntu, let's briefly understand - What is Docker Compose?

Docker Compose is a powerful tool that simplifies the deployment of multi-container applications. It allows you to define and manage all the services and their dependencies in a single configuration file. With Docker Compose, you can easily spin up and orchestrate multiple containers, ensuring seamless communication between them.

Whether you're developing, testing, or deploying applications, Docker Compose streamlines the process and boosts productivity. It's a must-have tool for containerization enthusiasts and teams looking to streamline their workflow.

In this tutorial, you will install the latest version of Docker Compose. We will also address a few FAQs on how to install Docker Compose on Ubuntu 22.04.

Advantages of Docker Compose

  1. Simplifies Deployment: Docker Compose makes deploying multi-container applications easy and efficient.
  2. Container Orchestration: It enables seamless orchestration and communication between containers.
  3. Streamlines Workflow: Docker Compose enhances productivity by simplifying development, testing, and deployment processes.
  4. Configuration Management: It allows you to define and manage services and dependencies in a single configuration file.
  5. Scalability and Portability: Docker Compose enables scalable and portable applications, making it ideal for various environments.

Prerequisites to Install Docker Compose on Ubuntu 22.04

  • Ubuntu 22.04 Operating System
  • A non-root user with sudo privileges
  • Command-line/terminal

Step 1 – Install Docker Compose

You can install Docker Compose from the official Ubuntu repository but as it is many versions behind the latest release, you need to install Docker Compose from Docker's GitHub repository.

Use the command below to check and update the current version.

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Now, you'll set the permissions

sudo chmod +x /usr/local/bin/docker-compose

After that, you need to verify if the installation is successful by checking the version.

docker-compose --version

This will show up in the version you installed.

Output

docker-compose version 1.21.2, build a133471

Now that you have installed Docker Compose, you can run a “Hello World” example.

Step 2 – Run a Container with Docker Compose

Hello, World image is included in Docker Hub. It demonstrates the minimal configuration required to run a container using Docker Compose: a YAML file that calls a single image.

At first, you need to create a directory for the YAML file and move into it.

mkdir hello-world
cd hello-world

After that, you need to create the YAML file.

nano docker-compose.yml

Add the following contents into the docker-compose yaml file, save the file and then exit the text editor.

my-test:
 image: hello-world

The first line in the yaml you created is used as part of the container name. The second line specifies the image you need to use to create the container. Now, when you will run the docker compose up command it will check for the image locally with the name you specified. After that, save and exit the file.

You can check for images manually on your system with the docker images command:

docker images

If there are no local images, then it will display only the column headings:

Output

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

Now, execute the following command, while you're still in the ~/hello-world directory:

docker-compose up

If you run the command and there's no local image named hello-world, Docker Compose will then pull it from the public Docker Hub repository.

Output

Pulling my-test (hello-world:latest)...
latest: Pulling from library/hello-world
c04616da8d49: Downloading [==================================================>] c04616da8d49: Extracting [==================================================>] c04616da8d49: Extracting [==================================================>] c04616da8d49: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
. . .

After the image pull is successful, docker-compose will create a container, attach, and run the hello program, which will confirm that the installation is working:

Output

. . .
Creating helloworld_my-test_1...
Attaching to helloworld_my-test_1
my-test_1 |
my-test_1 | Hello from Docker.
my-test_1 | This message shows that your installation appears to be working correctly.
my-test_1 |
. . .

Then it prints an explanation of what it did:

Output of docker-compose up
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

As long as the command is active, the Docker container will run till then only. So once hello finished running, the container will stop. Similarly, when you will look at active processes, the column headers will appear, but it won't list the hello-world container as it is not running anymore.

docker ps
Output

CONTAINER  ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES

You can see the container information, which you’ll need in the next step, by using the -a flag which shows all containers, not just the active ones:

docker ps -a
Output

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
07079gh5ca54        hello-world         "/hello"            35 minutes ago      Exited (0) 35 minutes ago                       drunk_payne

This displays the information you’ll need to remove the container when we’re done with it.

Step 3 – Removing the Image (Optional)

If you want to avoid the usage of disk space which is unnecessary, you need to remove the local image. For that, you need to delete all the containers that reference the image using the docker rm command, which can be followed by either the Container ID or the Container Name. You need to add your Container ID or Name with the docker rm command. Don't copy the one mentioned here.

docker rm 07079gh5ca54

Once all containers that reference the image have been removed, you can remove the image:

docker rmi hello-world

FAQs to Install Docker Compose on Ubuntu 22.04

Do I need Docker installed before installing Docker Compose?

Yes, Docker Compose relies on Docker Engine to function properly. So, make sure you have Docker installed on your Ubuntu 22.04 system before installing Docker Compose.

Can I use Docker Compose on other Linux distributions?

Yes, Docker Compose is compatible with various Linux distributions, including Ubuntu, Debian, CentOS, and Fedora.

Is Docker Compose available for Windows or macOS?

Yes, Docker Compose is available for Windows and macOS platforms. You can download the respective installation packages from the Docker website.

How do I check the version of Docker Compose?

You can check the version of Docker Compose by running the following command: docker-compose --version.

Are there any system requirements for running Docker Compose?

Docker Compose has minimal system requirements. As long as you have Docker Engine installed and running on your Ubuntu 22.04 system, you should be able to use Docker Compose.

Can I uninstall Docker Compose from my Ubuntu 22.04 system?

Yes, you can uninstall Docker Compose by running the following command: sudo apt remove docker-compose.

Conclusion

We hope this tutorial has helped you understand how to install Docker Compose on Ubuntu 22.04.

If you have any further queries or concerns, do leave us a comment below.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.