How to Install and use Docker Compose on Ubuntu 20.04

Installing Docker on Ubuntu

Check for system updates and install it.

apt update

apt upgrade

Install basic dependencies.

apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Output:

root@vps:~# apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
 libcurl4 python3-software-properties
The following NEW packages will be installed:
apt-transport-https gnupg-agent
The following packages will be upgraded:
ca-certificates curl libcurl4 python3-software-properties
 software-properties-common

Import docker repository GPG key.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Output:

root@vps:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

Add Docker CE repository.

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Output:

root@vps:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Get:1 https://download.docker.com/linux/ubuntu focal InRelease [36.2 kB]
Hit:2 http://gb.archive.ubuntu.com/ubuntu focal InRelease
Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [3,684 B]
Hit:4 http://gb.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:5 http://gb.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:6 http://gb.archive.ubuntu.com/ubuntu focal-security InRelease
Fetched 39.8 kB in 1s (33.6 kB/s)
Reading package lists... Done

Installing Docker CE.

apt install docker-ce

Output:

root@vps:~# apt install docker-ce
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
aufs-tools cgroupfs-mount containerd.io docker-ce-cli pigz
The following NEW packages will be installed:
aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli pigz

To check status.

systemctl status docker

Output:

root@vps:~# systemctl status docker
● docker.service - Docker Application Container Engine
 Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
 Active: active (running) since Mon 2020-10-26 16:44:47 UTC; 24s ago
TriggeredBy: ● docker.socket
   Docs: https://docs.docker.com
  Main PID: 19539 (dockerd)
  Tasks: 10
 Memory: 44.2M

Installing Docker Compose on Ubuntu

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services. Then, using a single command, you create and start all the services from your configuration.

Use curl to download the Compose file into the /usr/local/bin directory.

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Output:

root@vps:~# curl -L "https://github.com/docker/compose/releases/download/1.25.5/                                                                                        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   651  100   651    0     0   2945      0 --:--:-- --:--:-- --:--:--  2959
100 16.7M  100 16.7M    0     0  1485k      0  0:00:11  0:00:11 --:--:-- 2452k

Next, set the correct permissions so that the docker-compose command is executable.

chmod +x /usr/local/bin/docker-compose

To verify that the installation was successful, you can run following command.

docker-compose --version

Output:

root@vps:~# docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6

Setting Up a docker-compose.yml File

In this section, we’ll use Docker Compose to construct a multi-container WordPress utility.

creating a new directory in your home folder, and then moving into it.

mkdir my_app

cd my_app

Next, create the docker-compose.yml file.

nano docker-compose.yml

Paste the following content on your docker-compose.yml file.

version: '3'

services:
  db:
    image: mysql:5.7
    restart: always
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress

  wordpress:
    image: wordpress
    restart: always
    volumes:
      - ./wp_data:/var/www/html
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    depends_on:
       - db

volumes:
    db_data:
    wp_data:

In this example, we have services, db, and wordpress. Each service runs one image, and creates a separate container when docker-compose is run.

Start up the WordPress application by running the following command.

docker-compose up

Output:

root@vps:~# docker-compose up
Creating network "root_default" with the default driver
Creating volume "root_db_data" with default driver
Creating volume "root_wp_data" with default driver
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
bb79b6b2107f: Pull complete
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete

Navigate to your browser. http://yourserver-ip-address:8080 and you will see the Wordpress installation screen.

images

Start the Compose in a detached mode by following command.

docker-compose up -d

Output:

root@vps:~# docker-compose up -d
Starting root_db_1 ... done
Starting root_wordpress_1 ... done

To check the running services.

docker-compose ps

Output:

root@vps:~# docker-compose ps
  Name                    Command               State          Ports
--------------------------------------------------------------------------------
root_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
root_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp

To stop the services only.

docker-compose stop

To stop and remove containers and networks.

docker-compose down