Docker Compose is known as a command-line tool for running multiple containers on Docker defined. It uses the compose file in YAML format to configure the necessary resources for containers such as volumes, networking, and so on.
You can use Docker compose to define an isolated environment for containerized applications that can be run on any system.
This tutorial will explain the detail of installing Docker Compose on Ubuntu 20.04 LTS and explore some simple examples of using Docker Compose as well.
Before installing Docker Compose, you have to install Docker on your Ubuntu 20.04 machine first.
Updating the packages repository and installing the dependent packages by running:
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Then, adding the Docker apt-repository to your OS:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Once the Docker repository was added, you can install the Docker by running:
$ sudo apt update
$ sudo apt install docker-ce
Verifying that Docker was successfully installed and started automatically:
$ sudo systemctl status docker
Installing Docker Compose
At the time of this writing, the latest version of Docker Compose is 1.27.4. To download the docker-compose file to your Ubuntu 20.04 machine, running:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Then, grant the execute permission for docker-compose file:
$ sudo chmod +x /usr/local/bin/docker-compose
Verifying that the Docker Compose was successfully installed:
$ docker-compose --version
A simple example of using Docker Compose
In this section, we’re going to build an Nginx web server using Docker Compose:
First, create a new folder called myapp then change directory to it:
$ mkdir myapp
$ cd myapp
Then, using your favorite editor to create a compose file called docker-compose.yaml
$ vim docker-compose.yaml
version: '3' services: webserver: image: nginx:alpine container_name: web_server restart: unless-stopped ports: - "80:80" - "443:443" networks: - app-network networks: app-network: driver: bridge
In this compose file, we defined one service: webserver. It used docker image nginx:alpine from Docker Hub.
In myapp directory, run the following command to start the application:
$ sudo docker-compose up
Open new terminal and using curl command to verify that webserver is running:
$ curl localhost:80
To list the running service, run:
$ sudo docker-compose ps
You’ve successfully installed and tried running a simple Docker Compose example on your Ubuntu 20.04 LTS machine.
If you have any concerns, feel free to leave your comment and let me know. Thank you!
System Engineer with 6 years of experience in software development, specializes in Embedded Linux, C/C++, Python, Go, and Shell Scripts. He has a solid background in Computer Networking, OpenStack, Kubernetes, Docker/Container, CI/CD, and Google Cloud as well. Now, he is Head of GDG Cloud Hanoi – a non-profit community of cloud developers who meet/share ideas on everything Google Cloud Platform related.