How to Install Docker Compose on Ubuntu 18.04

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 is a containerization platform that packages software for easy deployment. These containers enable faster deployment, scalability, and consistency across different environments.

Docker Compose, on the other hand, is a tool that simplifies the management of multi-container applications. It allows you to define and run multiple services with a single configuration file. Docker and Docker Compose revolutionize software development by streamlining the process and ensuring compatibility, making it easier to build and deploy applications in any environment.

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

Advantages of Docker Compose

  1. Simplified Application Management: Docker Compose simplifies the management of applications by allowing you to define and orchestrate multiple containers as a single unit.
  2. Easy Multi-Container Configuration: With Docker Compose, you can easily configure and connect multiple containers, defining their relationships and dependencies in a single configuration file.
  3. Improved Scalability: Docker Compose facilitates the scaling of containers by allowing you to replicate and distribute them across different machines, ensuring efficient resource utilization.
  4. Streamlined Development Workflow: By providing a standardized way to define and run multi-container applications, Docker Compose streamlines the development process, making it easier to reproduce environments and collaborate with team members.
  5. Enhanced Collaboration: Docker Compose promotes collaboration among developers, system administrators, and other stakeholders by providing a consistent and portable environment for application deployment, ensuring everyone works with the same configuration.

Prerequisites

  • Ubuntu 18.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 the 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.

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 18.04

How do I check if Docker Compose is already installed on my Ubuntu 18.04?

You can run the command docker-compose --version to check if Docker Compose is already installed. If installed, it will display the version information.

How do I verify the successful installation of Docker Compose? 

After installation, you can verify it by running docker-compose --version. It should display the version information if the installation was successful.

Can I install Docker Compose using apt-get?

No, Docker Compose is not available through the apt-get package manager. The recommended way to install it is by using the official Docker Compose script.

Can I update Docker Compose to the latest version?

Yes, you can update Docker Compose to the latest version by running the installation steps again. It will replace the existing version with the newer one.

What if the docker-compose command is not found after installation?

 If the docker-compose command is not found, you may need to add /usr/local/bin/ to your PATH environment variable. You can do this by running export PATH=/usr/local/bin:$PATH or adding it to your shell profile (e.g., ~/.bashrc).

Are there any system requirements for Docker Compose on Ubuntu 18.04? 

Docker Compose has minimal system requirements. It requires a reasonably up-to-date version of Ubuntu 18.04 and a functioning Docker installation.

Where can I find further documentation or support for Docker Compose? 

You can refer to the official Docker Compose documentation available at https://docs.docker.com/compose/ for detailed usage instructions and troubleshooting. Additionally, there is an active community forum where you can seek support or ask questions.

Conclusion

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

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