How to Install and use Docker Compose on Ubuntu 21.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... Done
Reading state information... Done
ca-certificates is already the newest version (20210119build1).
ca-certificates set to manually installed.
curl is already the newest version (7.74.0-1ubuntu2).
curl set to manually installed.
software-properties-common is already the newest version (0.99.10).
software-properties-common set to manually installed.
The following packages were automatically installed and are no longer required:
  eatmydata libeatmydata1 python3-certifi python3-importlib-metadata python3-jinja2 python3-json-pointer
  python3-jsonpatch python3-jsonschema python3-markupsafe python3-more-itertools python3-pyrsistent
  python3-requests python3-urllib3 python3-zipp
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  apt-transport-https gnupg-agent
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,940 B of archives.
After this operation, 212 kB of additional disk space will be used.
Do you want to continue? [Y/n]

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"
Repository: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu hirsute stable'
Description:
Archive for codename: hirsute components: stable
More info: https://download.docker.com/linux/ubuntu
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-hirsute.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-hirsute.list
Hit:1 http://de.archive.ubuntu.com/ubuntu hirsute InRelease
Get:2 http://de.archive.ubuntu.com/ubuntu hirsute-updates InRelease [109 kB]
Get:3 https://download.docker.com/linux/ubuntu hirsute InRelease [28.3 kB]
Hit:4 http://de.archive.ubuntu.com/ubuntu hirsute-backports InRelease
Get:5 http://de.archive.ubuntu.com/ubuntu hirsute-security InRelease [101 kB]
Get:6 https://download.docker.com/linux/ubuntu hirsute/stable amd64 Packages [2,406 B]
Fetched 240 kB in 1s (364 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... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  eatmydata libeatmydata1 python3-certifi python3-importlib-metadata python3-jinja2 python3-json-pointer
  python3-jsonpatch python3-jsonschema python3-markupsafe python3-more-itertools python3-pyrsistent
  python3-requests python3-urllib3 python3-zipp
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  containerd.io docker-ce-cli docker-ce-rootless-extras docker-scan-plugin libslirp0 pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-scan-plugin libslirp0 pigz
  slirp4netns
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 108 MB of archives.
After this operation, 465 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

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: enabled)
     Active: active (running) since Sat 2021-05-01 03:45:52 UTC; 1min 6s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 632611 (dockerd)
      Tasks: 8
     Memory: 41.1M
     CGroup: /system.slice/docker.service
             └─632611 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root@vps:~#

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.

curl -L "https://github.com/docker/compose/releases/download/1.29.1/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.29.1/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   633  100   633    0     0    561      0  0:00:01  0:00:01 --:--:--   561
100 12.1M  100 12.1M    0     0  7465k      0  0:00:01  0:00:01 --:--:-- 32.2M

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.29.1, build c34c88b2

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
f7ec5a41d630: Pull complete
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: 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