// Tutorial //

How To Install Docker Compose on Ubuntu 18.04

Published on July 9, 2018 · Updated on April 28, 2022
Default avatar
English
How To Install Docker Compose on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

Introduction

Docker is a great tool for automating the deployment of Linux applications inside software containers, but to take full advantage of its potential, each component of an application should run in its own individual container. For complex applications with a lot of components, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy.

The Docker community came up with a popular solution called Fig, which allowed you to use a single YAML file to orchestrate all your Docker containers and configurations. This became so popular that the Docker team decided to make Docker Compose based on the Fig source, which is now deprecated.

Docker Compose makes it easier for users to orchestrate the processes of Docker containers, including starting up, shutting down, and setting up intra-container linking and volumes. In this tutorial, you’ll install the latest version of Docker Compose to manage multi-container applications.

Prerequisites

To follow this article, you will need an Ubuntu 18.04 server with the following:

Note: This tutorial will guide you through installing Docker Compose v1, which uses docker-compose. Starting with Docker Compose v2, Docker has migrated towards using the compose CLI plugin command as documented in our latest Ubuntu 22.04 version of this tutorial, and away from the original docker-compose. While the installation differs, in general the actual usage involves dropping the hyphen from docker-compose calls to become docker compose. For full compatibility details, check the official Docker documentation on command compatibility between the new compose and the old docker-compose.

Step 1 — Installing Docker Compose

Although you can install Docker Compose from the official Ubuntu repositories, it is several minor versions behind the latest release, so you’ll install Docker Compose from Docker’s GitHub repository. The command below is slightly different than the one you’ll find on the Releases page. By using the -o flag to specify the output file first rather than redirecting the output, this syntax avoids running into a permission denied error caused when using sudo.

Check the current release and if necessary, update it in the command below:

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

Next set the permissions:

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

Then you’ll verify that the installation was successful by checking the version:

  1. docker-compose --version

This will print out the version you installed:

Output
docker-compose version 1.29.2, build 5becea4c

Now that you have Docker Compose installed, you’re ready to run a “Hello World” example.

Step 2 — Running a Container with Docker Compose

The public Docker registry, Docker Hub, includes a Hello World image for demonstration and testing. It illustrates the minimal configuration required to run a container using Docker Compose: a YAML file that calls a single image:

First, you’ll create a directory for the YAML file and move into it:

  1. mkdir hello-world
  2. cd hello-world

Then, you’ll create the YAML file:

  1. nano docker-compose.yml

Put the following contents into the file, save the file, and exit the text editor:

docker-compose.yml
my-test:
    image: hello-world

The first line in the YAML file is used as part of the container name. The second line specifies which image to use to create the container. When you run the command docker-compose up it will look for a local image by the name you specified, hello-world. With this in place, you’ll save and exit the file.

You can look manually at images on your system with the docker images command:

  1. docker images

When there are no local images at all, only the column headings display:

Output
REPOSITORY TAG IMAGE ID CREATED SIZE

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

  1. docker-compose up

The first time you run the command, if there’s no local image named hello-world, Docker Compose will pull it from the Docker Hub public repository:

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

Note: If you run into a permission error regarding the Docker socket, this means you skipped Step 2 of How To Install and Use Docker on Ubuntu 18.04. Going back and completing that step will enable permissions to run docker commands without sudo.

After pulling the image, docker-compose creates a container, attaches, and runs the hello program, which in turn confirms that the installation appears to be 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. . . .

Docker containers only run as long as the command is active, so once hello finished running, the container stopped. Consequently, when you look at active processes, the column headers will appear, but the hello-world container won’t be listed because it’s not running.

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

  1. docker ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06069fd5ca23 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 you’re done with it.

Step 3 — Removing the Image (Optional)

To avoid using unnecessary disk space, you’ll remove the local image. To do so, you’ll need to delete all the containers that reference the image using the docker rm command, followed by either the CONTAINER ID or the NAME. Below, you’re using the CONTAINER ID from the docker ps -a command you just ran. Be sure to substitute the ID of your container:

  1. docker rm 06069fd5ca23

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

  1. docker rmi hello-world

Conclusion

You’ve now installed Docker Compose, tested your installation by running a Hello World example, and removed the test image and container.

While the Hello World example confirmed your installation, the simple configuration does not show one of the main benefits of Docker Compose — being able to bring a group of Docker containers up and down all at the same time. To see the power of Docker Compose in action, you might like to check out this practical example, How To Configure a Continuous Integration Testing Environment with Docker and Docker Compose on Ubuntu 16.04 (note: this article is for Ubuntu 16.04 rather than 18.04)


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Tony Tran

author

Developer and author at DigitalOcean.

Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

Thanks :)

Hello…I was able to successfully install docker compose following the tutorial but I need to uninstall it and install version 1.2.9. Normally I should use sudo rm /usr/local/bin/docker-compose but on checking the bin folder, the docker-compose executable isn’t there. I have checked the other bin and sbin locations and it is not there either.

I think it would be better to use

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

rather than what you have listed:

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

** Great article! **

No problem executing all the code, I tested with a Droplet Ubuntu 18.04.2 x64 1 vCPUs 3GB / 60GB and installed:

docker-compose version 1.23.2, build 1110ad01

Thanks a lot!

This comment has been deleted