How to Install and Configure Docker Compose On an Ubuntu 22.04 VPS or Dedicated Server

Introduction

Docker is a robust, open platform that is designed to help developers and system administrators build, distribute, and deploy container-based applications in software containers. To experience Docker’s full potential, each application’s component must be implemented in its unique container.

Now, if you are running multi-faceted applications with multiple components, it can be daunting to enable all containers to cooperate, start, or even stop.

However, there is a tool built to help you seamlessly orchestrate containers with ease. The tool is called the Docker Compose. Docker Compose is a great tool that makes it easy to define and run complex container-based applications, using a YAML file. The tool allows you to run a single command to create and initiate all the Docker services from YAML configuration.

This guide will help you install and set up Docker Compose on your Ubuntu 22.04 server.

Ready? Let’s get started!

Prerequisites

For the installation to run flawlessly, you need the following:

Step 1 – Installing Docker Compose

In this tutorial, we’ll install the Docker Compose from the official Docker’s GitHub repository and not the one from Ubuntu repositories. This is because the Docker Compose application from GitHub repository is the latest release and is ideally suited for Ubuntu 22.04.

Run the command below to install the current release of Docker Compose:

$ 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

screenshot of Configure Docker Compose On an Ubuntu 22.04

The command above includes the -o flag which stipulates the output file first instead of redirecting the output. This syntax prevents permission issues when running the command using sudo.

Now, execute the command below to adjust the permissions:

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

screenshot of Configure Docker Compose On an Ubuntu 22.04

Next, run the command below to check the version:

$ sudo docker-compose --version

This will give you the output below:

screenshot of Configure Docker Compose On an Ubuntu 22.04

The output confirms that the installation process was successful.

Step 2 – Deploying A Container Via Docker Compose

To run a container, the Hello World image available for testing and demonstration purposes from the Docker Hub; a public Docker registry. The image will help us illustrate the YAML configuration as required to deploy a Container using the Docker Compose.

First, run the command below to create a new directory for the file, then move into it:

$ sudo mkdir hello-world

screenshot of Configure Docker Compose On an Ubuntu 22.04

$ cd hello-world

Next, execute the command below to fashion the YAML file:

$ sudo nano docker-compose.yml

This will open the docker-compose.yml file. Add the content below into this file:

my-test:

image: hello-world

screenshot of Configure Docker Compose On an Ubuntu 22.04

The first part of the above content stipulates the container name, whereas the second part specifies the image to be used to create a container. If you don’t want to use this image, you can run the command below to check the images available in your system manually:

$ sudo docker images

This should show you a list of the available images. However, if there are no images you will get the output below:

screenshot of Configure Docker Compose On an Ubuntu 22.04

Now, execute the command below, while in the  ~/hello-world file:

$ sudo docker-compose up

This command will search for a local image called hello-world. If this image is not available, then Docker Composer will source it from Docker Hub. In this case, you will get the output below:

screenshot of Configure Docker Compose On an Ubuntu 22.04

Once the image is found, Docker Compose will generate a container, link, and deploy the hello world program.

Note
Note: This will only happen if the installation is working properly, and will be confirmed by the output of docker-compose up:

The Docker client communicated with the Docker daemon.

The daemon then pulled a “hello-world” image from the Docker Hub.

The daemon also generated a new container from the image which implements the executable which delivers the output we are currently reading.

The daemon streamed the output to the Docker client, which relayed it to the terminal.

Once the hello completed running, the Docker container stopped. This is because Docker containers run for as long as the command remains active. As a result, if you inspect the active process, you will not see the hello world container. However, the output will show the column header:

$ sudo docker ps

This will give you the output below:

screenshot of Configure Docker Compose On an Ubuntu 22.04

However, this does not bar us from viewing the container information. Run the command below to see the details of the container:

$ sudo docker ps -a

This will give you the output below:

screenshot of Configure Docker Compose On an Ubuntu 22.04

The details displayed in the output will be helpful when trying to do away with the container.

Step 3 – Removing the Local Image

This an optional step and it’s useful when in need of extra disk space.  Here, we’ll use the docker rm command to remove any container referencing the image. First, execute the command below to  remove all containers referencing the image:

$ sudo docker rm fcf034029971

Next, run the command below to remove the image:

$ sudo docker rmi hello-world

screenshot of Configure Docker Compose On an Ubuntu 22.04

Conclusion

That’s it! You have successfully installed and configured Docker Compose on your Ubuntu 22.04 system. You’ve also learned how to test its installation, and flawlessly removed the test containers as well as the image. Hope, you enjoyed the whole process!

Check out these top 3 VPS services:

HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Hostens
$2.00 /mo
Starting price
Visit Hostens
Rating based on expert review
  • User Friendly
    4.5
  • Support
    3.8
  • Features
    4.3
  • Reliability
    4.5
  • Pricing
    4.8
  • Check the recommendations for the best VPS and get a suitable one.

How to Setup a Docker Swarm Cluster on a CentOS 7 VPS or Dedicated Server

This how-to article will help you install and configure Docker Swarm on CentOS 7
5 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How to Install Own Cloud On an an Ubuntu 18.04 Dedicated Server or VPS

You can create your own self-hosted cloud storage services by installing the ope
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install a Let’s Encrypt Certificate on your Ubuntu 18.04 Dedicated Server or VPS

If you are hosting your website on a VPS server running Ubuntu 18.04, we will sh
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Deploy Docker Containers with OpenStack Heat

This guide is written to help users deploy Docker containers using OpenStackHeat
5 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.