How to Install Docker Compose on Ubuntu 18.04

June 10, 2019

Introduction

Docker Compose is a tool for defining and running multi-container Docker applications. It allows users to launch, execute, communicate, and close containers with a single coordinated command.

This guide will show you how to install Docker Compose on Ubuntu.

how to install docker compose on ubuntu

Prerequisites

Steps for Installing Docker Compose on Ubuntu

Update Software Repositories and Packages

Start by updating the software repositories and software packages. Open a terminal window, and enter the following:

sudo apt-get update
sudo apt-get upgrade

Check the curl command by entering:

curl

The system should respond as seen in the image below:

curl installed terminal output

If you see a different message, like curl: command not found, you’ll need to install it.

To install curl, enter the following:

sudo apt install curl

Download the Latest Docker Compose Version

1. To download the latest version of Docker Compose, use the command:

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose

This will download Docker Container v2.2.3.

  • The -L option tells the system to follow any redirects, in case the file has been moved
  • If you want a different version of Docker Compose, you may browse the list and substitute your preferred version for /v2.2.3/
  • The -o option changes the filename, so it’s easier to type
  • The file will be saved in /usr/bin/

2. Next, change the file permissions to allow the new software to be executed on Ubuntu:

sudo chmod +x /usr/bin/docker-compose

You do not need to run an installation script for Docker Compose. Once downloaded, the software is ready to use.

sudo curl -l docker-compose v2.2.3 terminal output

Note: You can also install Docker Compose from the official Ubuntu repository. Simply run sudo apt-get install docker-compose. However, it is recommended to install the software package from Docker’s official GitHub repository. That way, you are always installing the latest version.

Verify Docker Compose Installation

To test for a successful installation, check the version using:

docker-compose --version
docker-compose --version v2.2.3 terminal output

The output should appear similar to this:

How to Uninstall Docker Compose

To uninstall Docker Compose, simply delete the binary:

sudo rm /usr/bin/docker-compose

If you have installed Docker Compose using apt-get, use the following command to uninstall the package:

sudo apt-get remove docker-compose

Next, run a command to remove unnecessary software dependencies:

sudo apt-get autoremove

Getting Started Using Docker Compose

Run a Sample Container with Docker Compose

1. Switch back to your home directory (if needed):

cd ~

2. Create and switch to a new directory:

mkdir hello-world
cd hello-world

3. Create and edit a new YAML configuration file:

sudo nano docker-compose.yml

YAML is a type of configuration file. This file will create a container named test-file based on the Hello World image on Docker Hub.

4. Enter the following text into the editor:

services:
 hello-world:
  image:
   hello-world:latest

5. Press Ctrl-X to exit > Y to save the file > Enter.

If you have existing images on your system, you can display a list with the command:

sudo docker images

Running this command now will generate an empty list. Docker will automatically download the image based on this configuration file.

Enter:

sudo docker-compose up

Docker will look for a local hello-world image. If it can’t find one, it will download one. Then, Docker will create a container, launch it, then run the hello-world script. You should see the output on your screen as below:

sudo docker-compose up terminal output success

Docker-compose now creates a container and runs the hello program. This action confirms that the installation is operating.

It also displays an explanation of the actions completed:

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.

Once this operation finishes, Docker closes the container. You should see a command prompt at the end of the output.

Note: Users new to Docker may find it difficult to use as they often encounter an issue right after installing it. Check out our guide on how to resolve the “Cannot Connect To The Docker Daemon” error that commonly occurs.

List Docker Images and Containers

To display a list of all Docker images stored locally:

sudo docker images ls -a

The –a option shows all docker images. You should see a listing with the hello-world image.

To see a list all containers use:

sudo docker ps -a

To view all currently running containers:

sudo docker ps

Note that this list is empty. This shows you that once the hello-world image completes, it closes out automatically.

Remove a Docker Image and Container

To remove a Docker image, you need to know its IMAGE ID. Use the command for listing images from the passage above to copy that information.

Once you have the ID, run the following syntax to erase that image:

docker image rm [IMAGE_ID]

You can also use this command to remove multiple Docker images:

docker image rm [IMAGE_ID1] [IMAGE_ID2] [IMAGE_ID3]

To remove a Docker container use:

docker rm [CONTAINER_ID]

Replace [CONTAINER_ID] with the actual container ID.

Since this is a test, we don’t need to tie up disk space with old images. That’s why we will erase the example image. You won’t be able to remove an image until you remove all the containers linked to it.

First, remove the container:

docker rm [CONTAINER_ID]

As you only used the one container, you should be able to remove the image:

docker rmi hello-world

Conclusion

Now you know how to install and use Docker Compose on Ubuntu 18.04. Get started creating and managing new containers.

Was this article helpful?
YesNo
Dejan Tucakov
Dejan has over 8 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.
Next you should read
How to Install Docker on CentOS 7
October 30, 2024

Docker is an increasingly popular software package that creates a container for application development...
Read more
How to Manage Docker Containers? Best Practices
January 29, 2019

With Docker Container Management you can manage complex tasks with few resources. Learn the best practices ...
Read more
How to Share Data Between Docker Containers
March 26, 2019

Docker allows users to run applications isolated from a host computer, without the necessity of having ...
Read more
How to Install Docker Compose on CentOS 7
November 19, 2019

If you are a Docker user, you are most likely running and managing multiple containers at the same time...
Read more