Tutorial

How To Install Docker Compose on Ubuntu 16.04

Updated on December 21, 2017
How To Install Docker Compose on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.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, we’ll show you how to install the latest version of Docker Compose to help you manage multi-container applications.

Prerequisites

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

Once these are in place, you’re ready to follow along.

Note: Even though the Prerequisites give instructions for installing Docker on Ubuntu 16.04, the docker commands in this article should work on other operating systems as long as Docker is installed.

Step 1 — Installing Docker Compose

Although we can install Docker Compose from the official Ubuntu repositories, it is several minor version behind the latest release, so we’ll install Docker Compose from the 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.

We’ll 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.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Next we’ll set the permissions:

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

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

  1. docker-compose --version

This will print out the version we installed:

Output
docker-compose version 1.18.0, build 8dd22a9

Now that we have Docker Compose installed, we’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, we’ll create a directory for the YAML file and move into it:

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

Then, we’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 we run the command docker-compose up it will look for a local image by the name we specified, hello-world. With this in place, we’ll save and exit the file.

We can look manually at images on our 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, we’ll execute the following command:

  1. docker-compose up

The first time we 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 . . .

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 we 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

We can see the container information, which we’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 we’ll need to remove the container when we’re done with it.

Step 3 — Removing the Image (Optional)

To avoid using unnecessary disk space, we’ll remove the local image. To do so, we’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, we’re using the CONTAINER ID from the docker ps -a command we 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, we can remove the image:

  1. docker rmi hello-world

Conclusion

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

While the Hello World example confirmed our 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

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

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
9 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!

It seems a bit strange to me do install it through Github. Isn’t there an apt repo that contains it?

If I do:

apt install docker-compose

It works but I get an older version 1.5.2-1, coming from Digital Ocean’s repo.

I already added the Docker Project repo as described elsewhere here, but it doesn’t seem to comtain it. Anyone knows of a repo for this?

I think it would be great if you could replace docker-compose version 1.11.2 with 1.15 which is the latest till now. Even though the process doesn’t change, but its always better to refer the latest version.

latest docker-compose release link

Hi, I have tried to install Docker-compose in my ubun2 droplet with the above-mentioned install command

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

but it is throwing an error. “curl (6) could not resolve host github.com

Can anyone help to solve this issue?

awesome, thank you for posting :)

One really nice way to manage Docker Compose is running it from within a Docker container per their documentation, this isolates it from your system and potentially allows you to install testing versions alongside a stable version without too much hassle, and since you should already have Docker, it doesn’t require using pip or apt-get or yum or anything else and should always be up to date or extremely easy to update.

The “easy way” that you can run this and get the latest version (but pinned to a specific version of the container) is to run a combination of @mvasilenko 's command and the official Docker Compose via Docker scripts. Any time you want a newer version of Docker Compose you can simply redownload to get the newer Docker container tag, or you can just edit the run.sh that is saved as /usr/local/bin/docker-compose and change the version to match the latest.

CURL_OUT=$(curl https://github.com/docker/compose/releases/latest 2>/dev/null)
DOCKER_COMPOSE_VERSION=$(echo ${CURL_OUT} | sed 's:.*/tag/::' | sed 's:">.*$::' )
sudo curl -L https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/run.sh -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

If you were REALLY brave you could actually modify the run.sh script to check for a new version every time you run Docker Compose and update the Docker tag to that latest, this takes a little bit longer when running, but if there isn’t a new version it will use the cached container information already on your machine.

Here’s another snippet that may come in handy, making bash completion work:

sudo curl -o /usr/share/bash-completion/completions/docker-compose -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/bash/docker-compose

What’s the best way to make sure docker-compose executes on reboot?

A one-liner for removing all containers would be:

docker rm $(docker ps -aq)

This snippet gets latest version of docker-compose

CURL_OUT=$(curl https://github.com/docker/compose/releases/latest 2>/dev/null)
DOCKER_COMPOSE_VERSION=$(echo ${CURL_OUT} | sed 's:.*/tag/::' | sed 's:">.*$::' )
curl -L https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

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