Docker is a robust platform that enables developers to package and run applications in containers, making deploying applications across different environments easier. With Docker, developers can create a standardized environment for their applications and dependencies, ensuring their code works consistently across different machines and operating systems.
In this tutorial, we will walk through installing Docker on Rocky Linux, a community-driven enterprise operating system that provides a stable, reliable, and secure platform for deploying applications. We will cover the process of adding the Docker repository, installing Docker, starting and enabling the Docker service, and verifying that Docker is installed correctly.
We will also explore some post-installation tasks you may want to perform after installing Docker, such as adding your user to the docker group, configuring Docker to start on boot, and restarting the Docker service.
Finally, we will cover how to use Docker Compose, a tool for defining and running multi-container applications. We will walk through an example of using Docker Compose to define a simple web application that runs an NGINX container and maps port 80 to port 8080.
By the end of this tutorial, you should have a good understanding of how to install Docker on Rocky Linux, perform post-installation tasks, troubleshoot issues, and use Docker Compose to define and run multi-container applications. Whether you’re a developer or system administrator, Docker can help simplify the deployment of your applications and provide a consistent environment for your code to run in.
Requirements
- A Rocky Linux server. This tutorial uses Rocky Linux 9.1 (Blue Onyx).
- A non-root user account with sudo privileges.
Install Docker on Rocky Linux
Install Docker on Rocky Linux involves several steps that aren’t too complicated. It is similar to installing any package on your Linux distro with few specific details relating to Docker.
Step 1: Update your system
Log in to your Rocky Linux server and run the following command to make sure your system is up to date:
sudo dnf update
Step 2: Add the Docker repository
The Docker repository is not included by default in Rocky Linux. So, to install Docker, you need to add the Docker repository to your system:
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Note. Learn how to create a live USB using Unetbootin Linux.
Step 3: Install Docker
After adding the repository, you can now install Docker using the following command:
sudo dnf install docker-ce docker-ce-cli containerd.io
When asked to install the Docker GPG keys, press y and Enter to confirm.
You’ll get a summary of the installed packages when the installation is done.
Step 4: Start and enable Docker
Once the installation is complete, start and enable Docker to run at system boot:
sudo systemctl start docker sudo systemctl enable docker
Step 5: Enable Running Docker without Sudo
Add your user to the docker group. This step ensures your user account can run the docker command without sudo.
sudo usermod -aG docker $USER
You can run the command below if you need to add other users to the docker group. Substitute the USERNAME with the user’s username.
sudo usermod -aG docker USERNAME
Step 6: Test Docker
To verify that Docker is installed correctly, run the following command to check the version:
docker --version
If Docker is installed correctly, you should see an output similar to this:
Run this command to confirm that the Docker daemon is running.
sudo systemctl status docker
Lastly, let’s run a Docker container to ensure everything works. In this example, let’s run an Ubuntu container.
docker run -t -i --name ubuntu1 ubuntu
Installing and Using Docker Compose on Rocky Linux
Docker Compose is a tool for defining and running multi-container Docker applications. New Docker installations should already include the docker compose command. But if you need to install it separately, here’s how.
Note. The docker compose command is not the same as docker-compose, which is the old version and is already deprecated.
Step 1: Install the Docker Compose Plugin
The docker compose command will be available when you install the docker-compose-plugin package, and here’s the command to install it.
sudo dnf install docker-compose-plugin
Step 2: Create a docker-compose.yml file
Create a docker-compose.yml file on the current directory using your preferred text editor. In this example, we’ll use nano.
nano docker-compose.yml
Copy the below code and paste it into the editor.
version: '3' services: web: image: nginx ports: - "8080:80"
Now, save the file and exit the editor.
This file defines a ” web ” service that runs an NGINX image and maps port 80 to port 8080.
Step 3: Start the Containers
Now that the compose file is ready, let’s start the container in detached mode.
docker-compose up -d
Now, test whether the NGNIX web is serving the default website.
curl http://localhost:8080
The container has successfully started if you see the raw HTML code below.
Conclusion
In this tutorial, we covered installing Docker on Rocky Linux, performing post-installation tasks, and using Docker Compose to define and run multi-container applications. Docker is a powerful tool for developers and can help simplify the deployment of applications in various environments.