Docker’s Compose tool is an ideal tool for working in the development and testing environments, but also for small production applications. This article explains how to easily orchestrate Docker applications with Compose on Ubuntu.
Docker Compose under Ubuntu: prerequisites¶
Before you can use Docker Compose, make sure your system meets the following requirements:
- DockerEngine : as Compose is an extension of Docker Engine, you must install it;
- Operating system : Ubuntu, sudo user with root privileges.
Do you want to use an operating system other than Linux? Check out our guide on how to install Docker Compose on Windows and Docker Compose on macOS.
Install Docker Compose on Ubuntu step by step¶
To use Docker Compose on Ubuntu, you must first install the DockerEngine on your system and verify that it is running correctly. Once Docker is running, download Compose and create the YAML file to configure your applications.
Step 1: Download and install Docker Compose¶
Download the latest version of Docker Compose from the official repository GitHub by typing the following command in a terminal:
$ curl -SL https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
shell
You must then grant Docker Compose the performance rights :
$ sudo chmod +x /usr/local/bin/docker-compose
shell
The “–version” option allows you to verify that Compose has been installed correctly.
$ docker-compose --version
shell
Here is the output displayed:
If the installation fails, take a look at the path and verify that it is the correct one.
It is also possible to create a symbolic link towards the path /usr/bin :
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
shell
Step 2: Configure the docker-compose.yml file¶
To explain file design Docker-Compose-YAMLwe use the Nginx image from the official Docker hub for the container environment.
First create a new folder in your home directory:
$ mkdir ~/compose-test
shell
Go to the directory and create a new folder there for the root directory of your environment Nginx.
$ cd ~/compose-test
$ mkdir app
shell
You can use any text editor like nano to create a index.html.
$ nano app/index.html
shell
Here is the HTML code of a test page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Docker Compose Test</title>
</head>
<body>
<h1>This is a Docker Compose Test Page for an Nginx container.</h1>
</body>
</html>
html
Save and close the HTML file then create docker-compose.yml.
$ nano docker-compose.yml
shell
The contents consist of the configuration version number and the Services block.
version: ‘3.9’
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./app:/usr/share/nginx/html
YAML
In the Services block, only the “web” service is listed. Next comes the Nginx image used and the port forwarding. In this case, all requests on port 8000 of the host machine are redirected to the web container on port 80, on which Nginx is running. Additionally, we use a volume shared between host and container which allows the Nginx application to access the local “app” folder.
Step 3: Run Docker Compose¶
The following command creates a web container and run the container environment in the background:
$ docker-compose up -d
shell
If the image specified in the YAML file is not available on the local system, it is automatically downloaded.
To test that the Nginx environment is working correctly, type the command ps
.
The test page created previously is now accessible on localhost:8000 if you run the demo locally on your computer. If you’re going through a remote server, just provide your server’s IP address instead of « localhost ».
The command stop
allows you to exit the container application again.
$ docker-compose stop
shell