Cyberithub

How to Install Docker Compose on Ubuntu 20.04 LTS [Step By Step]

Advertisements

In this article, I will take you through step by step guide to install docker compose on Ubuntu 20.04 LTS. If you are using multiple containers based application then you might be aware of the pain of writing and managing tons of code in multiple docker files to create and deploy that application. Further more, with the increase in the number of application containers, it becomes almost impossible to keep track of and manage all those containers.

So to make life easier it is always recommended to use a completely free and open source tool called Docker Compose. It is known to be the orchestrating and launching tool for multi container applications. It uses a YAML config file called docker-compose.yml to create and run multiple containers as a single service. It can be easily installed in almost all kind of environments. Here we will see the steps to install docker compose on Ubuntu 20.04 LTS.

How to Install Docker Compose on Ubuntu 20.04 LTS [Step By Step]

How to Install Docker Compose on Ubuntu 20.04 LTS

Also Read: Docker CMD vs ENTRYPOINT: Understanding the difference and Usage

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your System.

d) You should also have gpg, curl and tee utility installed in your System.

 

Step 2: Update Your Server

It is essential to run System update using apt update or apt-get update command to let the System know if there are any updates or a new package available in the repo.

root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:3 http://ppa.launchpad.net/hluk/copyq/ubuntu focal InRelease
Hit:4 https://download.docker.com/linux/ubuntu focal InRelease
Get:5 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Hit:6 https://artifacts.elastic.co/packages/oss-7.x/apt stable InRelease
Hit:7 https://dl.google.com/linux/chrome/deb stable InRelease
Get:8 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Ign:9 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 InRelease

 

Step 3: Install Docker

a) Download GPG Key

First you need to download the Docker GPG Key using below curl command. This is required to verify the integrity of the package.

root@localhost:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

b) Setup Repository

Then add the stable docker repository URL in the apt source file using below command.

root@localhost:~# echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

c) Update Package

Next step is to update the package cache with all the package information from the newly added repository by using apt update or apt-get update command as shown below.

root@localhost:~# apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal InRelease
Ign:3 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 InRelease
Get:4 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:5 http://ppa.launchpad.net/hluk/copyq/ubuntu focal InRelease
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:8 https://artifacts.elastic.co/packages/oss-7.x/apt stable InRelease [10.4 kB]
Hit:9 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 Release
Hit:10 https://dl.google.com/linux/chrome/deb stable InRelease

d) Install Package

Finally, download and install all the required docker packages from repository by using apt-get install docker-ce docker-ce-cli containerd.io command as shown below.

root@localhost:~# apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
i965-va-driver intel-media-va-driver kwayland-data kwayland-integration libaacs0 libaom0 libass9 libavcodec58 libavdevice58 libavfilter7 libavformat58
libavresample4 libavutil56 libbdplus0 libbluray2 libbs2b0 libchromaprint1 libcodec2-0.9 libdbusmenu-qt5-2 libdc1394-22 libfam0 libfftw3-double3 libflite1
libgme0 libgsm1 libigdgmm11 libkf5config-bin libkf5config-data libkf5configcore5 libkf5coreaddons-data libkf5coreaddons5 libkf5guiaddons5 libkf5idletime5
libkf5notifications-data libkf5notifications5 libkf5waylandclient5 libkf5windowsystem-data libkf5windowsystem5 liblilv-0-0 libllvm11 libmysofa1 libnorm1
libopenal-data libopenal1 libopenmpt0 libpgm-5.2-0 libpostproc55 libqt5texttospeech5 librubberband2 libsdl2-2.0-0 libserd-0-0 libshine3 libsnappy1v5
libsndio7.0 libsord-0-0 libsratom-0-0 libssh-gcrypt-4 libswresample3 libswscale5 libtie-ixhash-perl libva-drm2 libva-x11-2 libva2 libvdpau1 libvidstab1.1
libx264-155 libx265-179 libxml-xpathengine-perl libxvidcore4 libzmq5 libzvbi-common libzvbi0 linux-headers-5.11.0-40-generic
linux-hwe-5.11-headers-5.11.0-40 linux-image-5.11.0-40-generic linux-modules-5.11.0-40-generic linux-modules-extra-5.11.0-40-generic mesa-va-drivers
mesa-vdpau-drivers va-driver-all vdpau-driver-all
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
docker-scan-plugin pigz
Suggested packages:
aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
containerd.io docker-ce docker-ce-cli docker-scan-plugin pigz
0 upgraded, 5 newly installed, 0 to remove and 43 not upgraded.
Need to get 3,575 kB/89.2 MB of archives.
After this operation, 388 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

 

Step 4: Install Docker Compose

You can go to Compose Official page and download the latest version using any of the file transfer tools like curl or wget. It will download the latest binary package under /usr/local/bin directory with the output file name as docker-compose as shown below.

root@localhost:~# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 3905 0 --:--:-- --:--:-- --:--:-- 3905
100 12.1M 100 12.1M 0 0 9473k 0 0:00:01 0:00:01 --:--:-- 20.1M

 

Step 5: Provide Execute Permission

Once downloaded, you need to provide the execute permission using chmod +x /usr/local/bin/docker-compose command to make this file executable as shown below.

root@localhost:~# chmod +x /usr/local/bin/docker-compose

 

Step 6: Check Docker Compose Version

After successful installation, you can verify the current installed version by using docker-compose --version command. As you can see current installed version is 1.29.2.

root@localhost:~# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

 

Step 7: Create docker-compose.yml 

Next step is to create a docker-compose.yml file specifying all the application components required to be created. Here we are taking an example of creating and deploying Postgres database and Nginx web server using docker containers. First we need to create and open docker-compose.yml file using nano docker-compose.yml command. Then for configuring Postgres database, we need to define database name, user and password as shown below. Similarly, for Nginx web server we need to specify the Nginx Image in docker-compose.yml in below given format. Then press Ctrl+X to save and exit the file.

root@localhost:~# nano docker-compose.yml
version: '2'

services:
  databases:
    image: "postgres"
    ports:
    - "54321:5432"
   environment:
   - POSTGRES_DB=sampledb
   - POSTGRES_USER=cyberithub
   - POSTGRES_PASSWORD=Test123
web:
    image: "nginx"

 

Step 8: Create all the Services

Once the compose file is setup and ready, you can run docker-compose up command to create all the container for given service.

root@localhost:~# docker-compose up
Creating network "root_default" with the default driver
Pulling databases (postgres:)...
latest: Pulling from library/postgres
.......................................................................................
databases_1 |
databases_1 | PostgreSQL init process complete; ready for start up.
databases_1 |
databases_1 | 2021-12-19 14:26:01.915 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
databases_1 | 2021-12-19 14:26:01.915 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
databases_1 | 2021-12-19 14:26:01.915 UTC [1] LOG: listening on IPv6 address "::", port 5432
databases_1 | 2021-12-19 14:26:01.931 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
databases_1 | 2021-12-19 14:26:01.944 UTC [63] LOG: database system was shut down at 2021-12-19 14:26:01 UTC
databases_1 | 2021-12-19 14:26:01.951 UTC [1] LOG: database system is ready to accept connections

 

Step 9: Show Containers

You can check the status of all the containers created through docker-compose.yml file using docker ps -a command as shown below.

root@localhost:~# docker ps -a
CONTAINER ID IMAGE     COMMAND               CREATED       STATUS       PORTS                                       NAMES
63d0a50d5c57 postgres "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:54321->5432/tcp, :::54321->5432/tcp root_databases_1
b09d31ca51d9 nginx    "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp                                      root_web_1

 

Step 10: Remove all the Services

You can also bring down all the application containers created through compose by running docker-compose down command as you can see below. This will stop and remove both the database as well as web server.

root@localhost:~# docker-compose down
Stopping root_databases_1 ... done
Stopping root_web_1 ... done
Removing root_databases_1 ... done
Removing root_web_1 ... done
Removing network root_default

Leave a Comment