With gradual growth in technology, cloud services are becoming prominent and containerization technology taking over. This has to lead to more innovation and modification of resources to fit in cloud generation. Docker is one of the tools that is currently widely used in setting up cloud infrastructures. Is used in running containers in an isolated environment in a host OS. Unlike Virtual Machines, during installation, no resources are deliberately allocated to a container. Has emerged as a suitable, scalable, effective, and efficient modern way of virtualization.

In this article, we are going to discuss on how to install and use docker-compose on Rocky Linux 8. Docker-compose is a tool in deploying multiple containers at the same time with only a single docker-compose up command. Use docker files such as yaml or yml file to declare properties of multiple containers to be deployed. At the end of this guide you should be able to;

  1. Install docker on Rocky Linux 8
  2. Install docker-compose on Rocky Linux 8
  3. Deploy Docker containers with docker-compose
  4. Use basic docker-compose commands

Step 1: Install Docker CE on Rocky Linux 8

First and foremost, we need to install docker. This an engine that enables you to run docker-compose command in deploying and managing docker containers. Being a new and emerging distro, Rocky Linux has really inherited and still using centos repositories just like Ubuntu using Debian. Therefore, we are going to add a centos repo that Rocky OS can use to install docker.

sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install docker-ce

If you encounter a containerd.io error, use command below to bypass the error.

sudo dnf install docker-ce --allowerassing

Below screen will prompt you to accept and continue with installation by typing Y then press Enter.

Dependencies resolved.
======================================================================================================================================================
 Package                                 Architecture         Version                                            Repository                      Size
======================================================================================================================================================
Installing:
 docker-ce                               x86_64               3:20.10.6-3.el8                                    docker-ce-stable                27 M
Installing dependencies:
 containerd.io                           x86_64               1.4.6-3.1.el8                                      docker-ce-stable                34 M
     replacing  runc.x86_64 1.0.0-70.rc92.module+el8.3.0+161+9c4b50ff
 docker-ce-cli                           x86_64               1:20.10.6-3.el8                                    docker-ce-stable                33 M
 docker-ce-rootless-extras               x86_64               20.10.6-3.el8                                      docker-ce-stable               9.2 M
 docker-scan-plugin                      x86_64               0.7.0-3.el8                                        docker-ce-stable               4.2 M
 libcgroup                               x86_64               0.41-19.el8                                        baseos                          69 k
Removing dependent packages:
 buildah                                 x86_64               1.16.7-4.module+el8.3.0+161+9c4b50ff               @AppStream                      29 M
 cockpit-podman                          noarch               27.1-3.module+el8.3.0+161+9c4b50ff                 @AppStream                     3.7 M
 podman                                  x86_64               2.2.1-7.module+el8.3.0+161+9c4b50ff                @AppStream                      52 M
 podman-catatonit                        x86_64               2.2.1-7.module+el8.3.0+161+9c4b50ff                @AppStream                     753 k

Transaction Summary
======================================================================================================================================================
Install  6 Packages
Remove   4 Packages

Total download size: 107 M
Is this ok [y/N]: y

Accept the prompt to import docker key.

Import key 
warning: /var/cache/dnf/docker-ce-stable-fa9dc42ab4cec2f4/packages/containerd.io-1.4.6-3.1.el8.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Docker CE Stable - x86_64                                                                                              36 kB/s | 1.6 kB     00:00    
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <[email protected]>"
 Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
 From       : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction

Start and enable docker to start on system boot.

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

The status command output below shows that docker is up and running.

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-06-13 07:35:49 EDT; 17s ago
     Docs: https://docs.docker.com
 Main PID: 2578 (dockerd)
    Tasks: 8
   Memory: 163.8M
   CGroup: /system.slice/docker.service
           └─2578 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

For flexibility, it is always good to run docker as non root user. To achieve this, create docker group in case not created by default. Add user that you would like to run docker in docker group.

sudo groupadd docker
sudo usermod -aG docker $USER

Step 2: Install Docker Compose on Rocky Linux 8

Now that we have the docker engine running, next we are going to install docker-compose on Rocky Linux 8. This is a docker component that enables you to run deploy multiple Docker containers with the execution of a single command. We are going to use the curl command to install docker-compose. In case you don’t have curl, download with help of this command.

sudo dnf install curl

Use curl command to install docker compose.

curl -s https://api.github.com/repos/docker/compose/releases/latest \
  | grep browser_download_url \
  | grep docker-compose-Linux-x86_64 \
  | cut -d '"' -f 4 \
  | wget -qi -

Give execution bits to the file downloaded.

chmod +x docker-compose-Linux-x86_64

Move the file to your /usr/local/bin directory:

sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

Validate installation by checking software version:

$ docker-compose version

Step 3: Creating Dockerfile to use with Docker Compose

Docker file is an automated script that contains commands you will call with docker-compose to pull and run docker images in docker containers. In this illustration, we are going to deploy WordPress in Rocky Linux 8 with docker-compose. WordPress image has in-build web-server already integrated. Therefore we only need to create a file Create a directory WordPress.

mkdir ~/wordpress

Next, cd to the folder then create docker-compose.yml file in it.

cd ~/wordpress
vim docker-compose.yml

Copy and paste the script below in the file.

version: "3.9"
services:
  database:
    image: mysql
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    container_name: mysql_db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: testdb
      MYSQL_USER: testuser
      MYSQL_PASSWORD: testuser_password
    
  wordpress:
    depends_on:
      - database
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "80:80"
    restart: always
    container_name: wordpress_site
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_USER: testuser
      WORDPRESS_DB_PASSWORD: testuser_password
      WORDPRESS_DB_NAME: testdb
volumes:
  db_data: {}
  wordpress_data: {}

Save the file and exit editor. To do this, Escape the session with Esc Key, full colon (:), write the file (w), quite (q), end with an exclamation (!) then click Enter. Your command should look this way; :wq!+Enter.

Step 4: Install WordPress using Docker Compose

To install WordPress, we are going to ignite yml file with the docker-compose command to pull and run containers. For the command to locate and run, make sure you run it within the location where your docker-compose.yml file is else it will output an error.

cd ~/wordpress
docker-compose up -d

If your file is well configured then below output will appear on you screen.

Creating network "wordpress_default" with the default driver
Creating volume "wordpress_db_data" with default driver
Creating volume "wordpress_wordpress_data" with default driver
Pulling database (mysql:)...
latest: Pulling from library/mysql
69692152171a: Pull complete
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Downloading [===========================================>       ]  101.5MB/115.8MB
16415d159dfb: Downloading [=============================================>     ]  105.3MB/115.8MB
b0a4a1a77178: Download complete
16415d159dfb: Downloading [=============================================>     ]  105.8MB/115.8MB
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Status: Downloaded newer image for mysql:latest
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
69692152171a: Already exists
2040822db325: Pull complete
9b4ca5ae9dfa: Pull complete
ac1fe7c6d966: Pull complete
5b26fc9ce030: Pull complete
3492f4769444: Pull complete
1dec05775a74: Pull complete
77107a42338e: Pull complete
f58e4093c52a: Pull complete
d32715f578d3: Pull complete
7a73fb2558ce: Pull complete
667b573fcff7: Pull complete
75e2da936ffe: Pull complete
759622df3a7b: Pull complete
c2f98ef02756: Pull complete
50e11300b0a6: Pull complete
de37513870b9: Pull complete
f25501789abc: Pull complete
0cf8e3442952: Pull complete
d45ce270a7e6: Pull complete
534cdc5a6ea6: Pull complete
Digest: sha256:e9da0d6c867249f364cd2292ea0dd01d7281e8dfbcc3e4b39b823f9a790b237b
Status: Downloaded newer image for wordpress:latest
Creating mysql_db ... done
Creating wordpress_site ... done

We have now successfully downloaded and installed WordPress with MySQL database. To continue with installation on web browser, use your server_IP or your_domain.

http://server_ip

Select language then click continue to go to the next step.

Fill in the details below respectively. Username and password are credentials you will use for admin login. When done click Install WordPress to continue.

Success!!, WordPress is now installed and running.

Click login to open admin login page to access admin management console. Enter credentials you set earlier correctly to login.

Now you can access admin console. Here you can design your website and even create and publish your posts.

Basic Docker Compose Commands.

When learning or interested in docker compose, which almost taking over the over the cloud computing technology, here are are basic commands the will help you catch up easily and familiarize with docker compose quickly.

Command that is used to run docker-compose file. Flag d (-d) will detach the shell once images have been pulled and installed.

$ docker-compose up -d

To validate and view docker-compose file.

$ docker-compose config

Pause running containers

$ docker-compose pause

Restart paused or stopped containers.

$ docker-compose restart

View docker images downloaded.

$ docker-compose images

To list all active and running containers.

$ docker-compose ps

Stop and release resources to running containers.

$ docker-compose down

To view running processes.

$ docker-compose top

View helpful docker-compose commands.

$ docker-compose a

Best Docker Learning Courses:

Conclusion

I trust that it was a simple, well-explained, and illustrated guide on docker-compose. At this point, you should be in a position to install docker-compose and deploy multi-containers with docker-compose. Docker is becoming the basis of most developing cloud providers. Therefore being interested in learning docker will guarantee you a better position in the current and future state of technology. Like, comment and share. Good luck!

You can also read on ;

Install KVM With Virtualization Manager on Rocky Linux 8

Install and Configure Samba File Sharing on Rocky Linux 8

Install and Configure vsftpd FTP Server on Rocky Linux 8

LEAVE A REPLY

Please enter your comment!
Please enter your name here