Oct 9, 2023 5 min read

How to Install Docker Compose on Ubuntu 20.04

Install Docker Compose on Ubuntu 20.04 with our step-by-step tutorial. Docker and Docker Compose are powerful tools used in software development.

Install Docker Compose on Ubuntu 20.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 and Docker Compose?

Docker and Docker Compose are powerful tools used in software development. Docker provides a way to package applications into containers, which are isolated environments containing all necessary dependencies. This ensures that the application runs consistently on different systems.

Docker Compose, on the other hand, simplifies the management of multiple Docker containers by allowing developers to define and configure them through a single file. With Docker and Docker Compose, developers can easily deploy and scale applications, saving time and improving efficiency.

In this tutorial, you will install the latest version of Docker Compose. Also, we will answer some FAQs regarding the Docker Compose installation.

Advantages of Docker Compose

  1. Containerization: Docker Compose allows applications to be packaged into portable containers, making them easy to deploy and run consistently across different environments.
  2. Simplified Management: With a single file, developers can define and configure multiple containers, simplifying the management and orchestration of complex applications.
  3. Efficient Development: Docker Compose enables developers to quickly set up and tear down environments, making it faster to develop, test, and debug applications.
  4. Scalability: Docker Compose facilitates scaling applications by allowing easy replication and distribution of multiple containers across different hosts.
  5. Consistency: By using Docker Compose, developers ensure that application dependencies and configurations remain consistent, leading to fewer compatibility issues and faster deployment times.

Prerequisites

  • Ubuntu 20.04 Operating System
  • A non-root user with sudo privileges
  • Docker installed from Step 1 and Step 2 of this tutorial
  • Command-line/terminal

Step 1 - Installing 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 - Running 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.

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 will 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 20.04

Can I use Docker Compose without Docker?

No, Docker Compose relies on the Docker engine to build and run containers. You must have Docker installed on your system before you can use Docker Compose.

Can I install Docker Compose using apt-get?

No, Docker Compose is not available in the official Ubuntu repositories. It is recommended to install Docker Compose manually by downloading the binary.

How can I download the Docker Compose binary?

You can download the latest Docker Compose binary using the curl command or by visiting the official Docker Compose GitHub repository and selecting the appropriate release.

Can I upgrade Docker Compose to a newer version?

Yes, you can upgrade Docker Compose by following these steps:

  • Visit the official Docker Compose GitHub releases page.
  • Find the latest release compatible with your system.
  • Download the binary and replace the existing Docker Compose binary in /usr/local/bin with the new one.

How do I start using Docker Compose with my project?

To use Docker Compose with your project, create a docker-compose.yaml file in the root directory of your project. Define the services, networks, and volumes required for your application, and then run docker-compose up to start the containers.

Can I run Docker Compose as a system service?

Yes, you can manage Docker Compose as a system service on Ubuntu 20.04. Create a system service file and configure it to start and stop Docker Compose as needed.

How can I troubleshoot issues with Docker Compose?

If you encounter issues with Docker Compose, check the logs using the docker-compose logs command. Review the documentation and Docker Compose's GitHub issue tracker for known problems and solutions. 

Conclusion

We hope this tutorial has helped you understand how to install Docker Compose on Ubuntu 20.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.