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.
In this context, we shall look into how to install Docker Compose on Ubuntu 20.04 LTS and explore some simple examples of using Docker Compose as well.
How to install Docker Compose on Ubuntu ?
Before installing Docker Compose, you have to install Docker on your Ubuntu 20.04 machine first.
i. 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
ii. 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"
iii. Once the Docker repository was added, you can install the Docker by running:
$ sudo apt update
$ sudo apt install docker-ce
iv. Verifying that Docker was successfully installed and started automatically:
$ sudo systemctl status docker
v. Next, let's see how to install 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
vi. Then, grant the execute permission for docker-compose file:
$ sudo chmod +x /usr/local/bin/docker-compose
vii. Verifying that the Docker Compose was successfully installed:
$ docker-compose --version
An Example to test Docker Compose on Ubuntu
Here, we're going to build an Nginx web server using Docker Compose:
i. First, create a new folder called myapp then change directory to it:
$ mkdir myapp
$ cd myapp
ii. Then, using your favorite editor to create a compose file called docker-compose.yaml
$ vim docker-compose.yaml
In this compose file, we defined one service: webserver. It used docker image nginx:alpine from Docker Hub.
iii. In myapp directory, run the following command to start the application:
$ sudo docker-compose up
iv. Open new terminal and using curl command to verify that webserver is running:
$ curl localhost:80
v. To list the running service, run:
$ sudo docker-compose ps