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 / AlmaLinux
- Install docker-compose on Rocky /AlmaLinux
- Deploy Docker containers with docker-compose
- Use basic docker-compose commands
Step 1: Install Docker Engine
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-plugin
If 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]: y
Accept the prompt to import docker key then start and enable docker to start on system boot.
sudo systemctl enable --now docker
The 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 2024-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
newgrp docker
Step 2: Install Docker Compose
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
Docker Compose version v2.29.7
Step 3: Creating Dockerfile
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.
services:
database:
image: mysql
volumes:
- db_data:/var/lib/mysql
restart: always
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: SQLrootPassw0rd
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: WPUserPassw0rd
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: wordpress
WORDPRESS_DB_PASSWORD: WPUserPassw0rd
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
Save the file and exit editor.
Step 4: Install WordPress using 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.
$ docker-compose up -d
[+] Running 33/33
✔ database Pulled 103.3s
✔ eba3c26198b7 Pull complete 66.6s
✔ fc6c33853069 Pull complete 66.7s
✔ f1fa3ee22bea Pull complete 66.7s
✔ 5b8b24615ae8 Pull complete 67.2s
✔ cded0449fb1a Pull complete 67.2s
✔ 095378692b4a Pull complete 67.2s
✔ 110d87e5d2a3 Pull complete 69.1s
✔ bd1dbbbda514 Pull complete 69.1s
✔ 982f92841ea3 Pull complete 98.6s
✔ de34c1fda3aa Pull complete 98.6s
✔ wordpress Pulled 140.5s
✔ 302e3ee49805 Pull complete 85.4s
✔ 07fc0890b857 Pull complete 85.5s
✔ 141aa7d58c57 Pull complete 128.4s
✔ 2720d4bca8b3 Pull complete 128.4s
✔ 82deca51468c Pull complete 129.4s
✔ dec741dfa526 Pull complete 129.4s
✔ e204b0efab94 Pull complete 129.4s
✔ 0a9b8825ee85 Pull complete 129.6s
✔ ef45e9da2633 Pull complete 129.6s
✔ f2b46378d521 Pull complete 130.4s
✔ 5c104459ddad Pull complete 130.4s
✔ 282db878d4dd Pull complete 130.4s
✔ 4a1f827cc210 Pull complete 130.4s
✔ 16a044b841d6 Pull complete 131.9s
✔ 5aae6c13158b Pull complete 132.8s
✔ aafbf1137210 Pull complete 132.8s
✔ fae7038f494a Pull complete 132.9s
✔ 4306628e8a77 Pull complete 132.9s
✔ 5d2474f1d0a5 Pull complete 135.6s
✔ 5e101cb8455d Pull complete 135.6s
✔ 5db2b012f33a Pull complete 135.7s
[+] Running 5/5
✔ Network wordpress_default Created 0.1s
✔ Volume "wordpress_db_data" Created 0.0s
✔ Volume "wordpress_wordpress_data" Created 0.0s
✔ Container mysql_db Started 1.1s
✔ Container wordpress_site Started 0.8s
We have now successfully downloaded and installed WordPress with MySQL database.
$ docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mysql_db mysql "docker-entrypoint.s…" database 8 minutes ago Up 8 minutes 3306/tcp, 33060/tcp
wordpress_site wordpress:latest "docker-entrypoint.s…" wordpress 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp
To continue with installation on web browser, use your server_IP
or your_domain
.
http://localhost:80
Select language then click continue to go to the next step.
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.
Step 5: Basic 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.
#run doker-compose file and detach from shell
docker-compose up -d
#validate and view docker-compose file
docker-compose config
#pause running containers
docker-compose pause
#restart paused/stopped containers
docker-compose restart
#view donloaded images
docker-compose images
#list active and running containers
docker-compose ps
#stop and release resources to running containers
docker-compose down
#view running processes
docker-compose top
#view helpful docker commands
docker-compose a
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!