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 details how to install Docker Compose on Ubuntu.

how to install docker compose on ubuntu

Prerequisites

  • A system running Ubuntu 18.04
  • A user account with sudo privileges
  • The Docker software installed
  • A command line / terminal window (Ctrl-Alt-T)

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:
Check if curl is installed.

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/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This will download Docker Container 1.24.0. (At the time this article was written, Docker Compose 1.24.0 is the latest version.)

  • 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 /1.24.0/
  • The –o option changes the filename, so it’s easier to type
  • The file will be saved in /usr/local/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.

How to install Docker Compose?


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

The output should appear similar to this:
How to check Docker Compose version?

How to Uninstall Docker Compose

To uninstall Docker Compose, simply delete the binary:

sudo rm /usr/local/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:

version: '2'
 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:

Running the docker-compose up command.

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.

List Docker Images

To display a list of all Docker images:

sudo docker ps –a

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

To view all currently running images:

sudo docker ps

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

Remove a Docker Image

Since this is a test, we don’t need to tie up disk space with old images.

First, remove the container:

docker rm container_id

Replace container_id with the actual container ID you found with the docker ps –a command. You won’t be able to remove an image until you remove all the containers linked to it.

Since 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.