Docker Compose is an open-source container tool for running a multi-container Docker application. Docker Compose uses a YAML syntax structure, that is commonly used for configuration files and to define and run container-based applications.
Compose also support to define and configure application's services in YAML format.
The main advantage of Docker Compose is that you can define a multi-container application in one single file, that will spin up the application with just a single command which does need to be done to get it running.
With Compose you can manage the whole lifecycle of application:
- Managing services with start, stop, or rebuild
- View the status of running services
- Output the log of running services
- Run a one-off command on a service
In this tutorial, we learn how to install Docker-compose on Ubuntu 20.04, and how to run the sample-container app.
Install Docker Compose on Ubuntu
For successful installation of Docker Compose, need to have Docker installed on Ubuntu 20.04.
After installing Docker, you can verify that docker is installed and versio with following command:
$ docker version
Client: Docker Engine - Community Version: 20.10.6 API version: 1.41 Go version: go1.13.15 Git commit: 370c289 Built: Fri Apr 9 22:47:17 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.6 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 8728dd2 Built: Fri Apr 9 22:45:28 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.4 GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e runc: Version: 1.0.0-rc93 GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec docker-init: Version: 0.19.0 GitCommit: de40ad0
Since we now have a Docker, we are ready to install Docker compose.
To install docker compose from the Official Ubuntu repository, run:
$ sudo apt install docker-compose
This option will not guarantee that you downloading the latest docker-compose version.
Instead of downloading Docker Compose from the Ubuntu repository, we are suggesting downloading the binary package using an URL with the curl command:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This command saves the file in: /usr/local/bin directory, under the name docker-compose.
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 633 100 633 0 0 5456 0 --:--:-- --:--:-- --:--:-- 5456 100 12.1M 100 12.1M 0 0 40.0M 0 --:--:-- --:--:-- --:--:-- 40.0M
Next, we need to change the file permission, making the downloaded file executable with the following command:
$ sudo chmod +x /usr/local/bin/docker-compose
Verify installation, and check Docker Compose version:
$ docker–compose –version
docker-compose version 1.29.2, build 5becea4c
Test Docker Compose with Sample Container
Create new directory for your sample container example:
$ mkdir docker-compose-test
Change directory that you just created:
$ cd docker-compose-test
From there, create a YAML configuration file:
$ sudo nano docker-compose.yaml
And copy the following configuration into docker-compose.yaml file that you just opened:
command: [/bin/echo, 'Hello world']
Save this, and run:
$ docker-compose up
You should see steps for:
- pulling hello_world(ubuntu)
- Downloaded image for ubuntu:latest
- Creating docker-compose-test_hello_world_1
- Attaching to docker-compose-test_hello_world_1
Docker-compose using folder name as the project name to prefix containers and networks.
This created the container from the ubuntu image, and run commands that were specified on the docker-compose.yaml.
In this tutorial, we learned how to install Docker-compose on Ubuntu 20.04, and how to run the sample-container app. Docker-compose allows you to run multiple docker containers on just a single host, if you need to run multiple containers on multiple nodes, please refer to solutions such as Kubernetes or Swarm.