Tutorial

How To Install Docker Compose on Ubuntu 18.04

Updated on April 28, 2022
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)

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Tony Tran

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
5 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel