Introduction
Docker Compose is a powerful tool for creating and managing multi-container applications. It allows you to define and run multiple containers as a single service. In this tutorial, we will show you how to install Docker Compose on Ubuntu 18.04. We will also cover how to configure and use Docker Compose to create and manage multi-container applications. By the end of this tutorial, you will have a working Docker Compose environment on your Ubuntu 18.04 server.
How to Install Docker Compose on Ubuntu 18.04
1. Update the apt package index:
sudo apt update
2. Install packages to allow apt to use a repository over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
3. Add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
4. Add the Docker repository to APT sources:
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable”
5. Update the apt package index:
sudo apt update
6. Install the latest version of Docker Compose:
sudo apt install docker-compose
7. Verify the installation by checking the version:
docker-compose –version
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.
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:
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.
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 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:
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 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.
How to Install Docker Compose on Ubuntu 18.04
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a set of containers, their configuration, and the relationships between them. This guide will show you how to install Docker Compose on Ubuntu 18.04.
Prerequisites
Before you begin, you’ll need to have the following:
- A server running Ubuntu 18.04.
- A user with sudo privileges.
Step 1 — Installing Docker Compose
The Docker Compose binary is available in the official Ubuntu repositories but it may not always be the latest version. To get the latest version, you can download it from the Docker website.
First, download the binary with curl:
curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Next, apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
Finally, verify that the installation was successful by checking the version:
docker-compose --version
You should see output like this:
docker-compose version 1.25.4, build 8d51620a
Conclusion
You have successfully installed Docker Compose on your Ubuntu 18.04 server. You can now use it to define and run multi-container applications.