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;
- Install docker on Rocky Linux 8
- Install docker-compose on Rocky Linux 8
- Deploy Docker containers with docker-compose
- 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 docker-ce-cli containerd.io docker-compose-pluginIf you encounter a containerd.io error, use command below to bypass the error.
Below screen will prompt you to accept and continue with installation by typing Y then press Enter.
Transaction Summary
======================================================================================================================================================
Install 6 Packages
Remove 4 Packages
Total download size: 107 M
Is this ok [y/N]: yAccept the prompt to import docker key then start and enable docker to start on system boot.
sudo systemctl enable --now dockerThe status command output below shows that docker is up and running.
$ systemctl status docker
● 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.sockFor 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
newgrp dockerStep 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 curlUse 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_64Move the file to your /usr/local/bin directory:
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-composeValidate installation by checking software version:
$ docker-compose version
Docker Compose version v2.6.0Step 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 ~/wordpressNext, cd to the folder then create docker-compose.yml file in it.
cd ~/wordpress
vim docker-compose.ymlCopy 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 -dIf 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 ... doneWe 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_ipSelect language then click continue to go to the next step.
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 -dTo validate and view docker-compose file.
$ docker-compose configPause running containers
$ docker-compose pauseRestart paused or stopped containers.
$ docker-compose restartView docker images downloaded.
$ docker-compose imagesTo list all active and running containers.
$ docker-compose psStop and release resources to running containers.
$ docker-compose downTo view running processes.
$ docker-compose topView helpful docker-compose commands.
$ docker-compose a