How to install Docker Compose on Ubuntu
Docker Compose is particularly well-suited for development and test environments but also for smaller production deployments. We explain how you can easily orchestrate Docker applications on Ubuntu with Compose.
What are the requirements for Docker Compose on Ubuntu?
Before you can use Docker Compose, you must ensure that your system meets the following prerequisites:
- Docker Engine: Compose is an extension to the Docker Engine that you need to install.
- Operating system: Ubuntu, sudo user with root privileges.
To use an operating system other than Linux, see our guide on how to install Docker Compose on Windows and Docker Compose on macOS.
Step-by-step guide to installing Docker Compose on Ubuntu
To use Docker Compose on Ubuntu, you must first install Docker Engine and verify that it’s running properly. 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 GitHub repository by entering 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
shellNow, you can give Docker Compose execute rights:
$ sudo chmod +x /usr/local/bin/docker-compose
shellUse the --version
option to check if Compose was successfully installed.
$ docker-compose --version
shellYou’ll get the following output:
If installation fails, check the path.
You can also create a symbolic link to the /usr/bin path:
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
shellStep 2: Configure the docker-compose.yml file
To demonstrate the design of a Docker Compose YAML file, we 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
shellSwap to the directory and create a new folder for the root directory of your Nginx environment.
$ cd ~/compose-test
$ mkdir app
shellYou can use any text editor like nano to create index.html.
$ nano app/index.html
shellHere’s the HTML code for a sample 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>
htmlSave and close the HTML file and create docker-compose.yml.
$ nano docker-compose.yml
shellThe contents are divided into the version number of the configuration and the services block.
version: '3.9'
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./app:/usr/share/nginx/html
YAMLWithin the services block, there is a single service named “web.” It is associated with the specified Nginx image and port redirection. In our configuration, any requests made to port 8000 on the host machine are redirected to the web container on port 80, where Nginx is running. Furthermore, we use a shared volume between the host and container. This makes the local “app” folder accessible to the Nginx application
Step 3: Execute Docker Compose
With the following command we create a web container and run the container environment in the background:
$ docker-compose up -d
shellIf the image specified in the YAML file is not present on the local system, it will be downloaded automatically.
To test whether the Nginx environment is running, enter the ps
command.
$ docker-compose ps
shellThe sample page you created earlier is now accessible on localhost:8000 when you run the demo on your machine. If you’re using a remote server, simply specify the IP address of your server instead of “localhost”.
You can stop the container application using stop
.
$ docker-compose stop
shellNot sure how to get started with container tools? Check out our Docker tutorial and our overview of Docker Commands. Or find out more about Docker orchestration with Swarm and Compose.