Install Docker on Rocky Linux 9 | Free Containerization Tool

Posted on

Install Docker on Rocky Linux 9 | Free Containerization Tool

Install Docker on Rocky Linux 9 | Free Containerization Tool

In this tutorial, we will guide you through the process of How To Install Docker on Rocky Linux 9. Docker is a powerful, free, and open-source containerization tool. It allows developers to package their applications and dependencies into a standardized unit called a container. These containers can then be easily deployed and run across various environments, from local development machines to public or private clouds. This makes application deployment consistent and efficient, regardless of the underlying infrastructure. Let’s dive into the steps to get Docker up and running on your Rocky Linux 9 system.

You can now proceed to the guide steps below on the Orcacore website to install Docker CE on Rocky Linux 9.

Steps To Install and Use Docker CE on Rocky Linux 9

Before you begin, ensure your system meets the necessary requirements.

Requirements for Docker CE on Rocky Linux 9:

  • A running instance of Rocky Linux 9.
  • A user account with sudo privileges.
  • Stable internet connection.

1. Install Docker on Rocky Linux 9

First, it’s essential to update and upgrade your system’s package repository to ensure you have the latest packages. This will help prevent compatibility issues during the Docker installation process. Execute the following command in your terminal:

sudo dnf update && dnf upgrade

Next, install the EPEL (Extra Packages for Enterprise Linux) repository. EPEL provides additional packages that are not available in the default Rocky Linux repositories. Use the following command:

sudo dnf install epel-release -y

Note: If the podman and buildah packages are already installed on your system, it’s crucial to remove them to avoid conflicts with Docker. Podman and Buildah are alternative containerization tools that can interfere with Docker’s functionality. Use the following command to remove them:

sudo dnf remove podman buildah

Add Docker CE Repository

Now, you need to add the official Docker CE repository to your system. This repository contains the Docker CE packages required for installation. Use the following command:

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

With the Docker CE repository added, you can now install the Docker CE package, along with the Docker CE command-line interface (CLI) and containerd.io, which is a container runtime. Execute the following command:

sudo dnf install docker-ce docker-ce-cli containerd.io -y

Manage Docker Service

After the installation is complete, you need to start and enable the Docker service. Starting the service initiates the Docker daemon, which manages containers. Enabling the service ensures that Docker starts automatically at boot time. Use the following commands:

sudo systemctl start docker.service
sudo systemctl enable docker.service

To verify that the Docker service is active and running correctly, use the following command:

sudo systemctl status docker

The output should indicate that the Docker service is active and running.

● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2023-10-27 10:00:00 UTC; 10s ago
       Docs: https://docs.docker.com
   Main PID: 1234 (dockerd)
      Tasks: 8
     Memory: 50.0M
        CPU: 1.234s
     CGroup: /system.slice/docker.service
             └─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Here your service is active and running. Now you have the docker command-line utility too.

2. How To Run Docker Commands?

Note: To run Docker commands, you typically need to log in as a non-root user with root privileges (using sudo). Alternatively, you can add your user to the docker group, which is created during the installation of Docker.

If you prefer not to use sudo to run Docker commands, you can add your user to the docker group. This grants your user the necessary permissions to interact with the Docker daemon. To do this, run the following command:

sudo usermod -aG docker $(whoami)

After adding your user to the docker group, log out of your server and then log back in with the same user to apply the changes.

If you need to add a different user to the docker group, run the following command, replacing <username> with the actual username:

sudo usermod -aG docker <username>

Let’s see how to use docker on Rocky Linux 9.

3. How To Use Docker CE on Rocky Linux 9?

After successfully completing the Install Docker on Rocky Linux 9 process, let’s explore how to utilize the docker command-line utility.

The general syntax of a Docker command is as follows:

docker [<option>] [<command>] [<arguments>]

To view the available options and commands for Docker, simply run the docker command without any arguments:

docker

The output will display a list of available commands, options, and general help information.

4. How To Work with Docker Images on Rocky Linux 9?

A Docker image is a lightweight, portable, and executable package that contains everything needed to run an application, including the code, runtime, system tools, system libraries, and settings.

Let’s download and run the "hello-world" Docker image from Docker Hub, a public registry for Docker images. To do this, execute the docker command with the run subcommand, like this:

sudo docker run hello-world

The output will display a message indicating that the Docker installation appears to be working correctly.

Search for Docker Images

You can also search for Docker images on Docker Hub if you’re unsure whether an image exists. For example, to search for images related to AlmaLinux, use the following command:

sudo docker search almalinux

The output will display a list of images that match the search term, along with their descriptions, stars, and other information.

Pull Docker Images

Once you’ve found a Docker image that you want to use, you can download it to your local system using the pull subcommand:

sudo docker pull almalinux

This command will download the latest version of the AlmaLinux image from Docker Hub.

Run Docker Images

After the image is downloaded successfully, you can run the image with the following command:

sudo docker run almalinux

List Docker Images

To see a list of all Docker images that are currently stored on your system, use the following command:

sudo docker images

The output will display a table containing information about each image, including its repository, tag, image ID, creation date, and size.

At this point, you need to know about docker containers too. Let’s see how Docker containers work on Rocky Linux 9.

5. How to Run a Docker Container on Rocky Linux 9?

Containers are a lightweight and efficient alternative to virtual machines. They isolate applications from each other while sharing the host operating system’s kernel.

To run a container based on the AlmaLinux image, use the following command:

sudo docker run -it almalinux

Note: The -it switch provides you with an interactive shell access into the container.

Your output should be similar to this:

[root@4c185a4e03f4 /]#

Important Note: Remember the container ID. Here it is 4c185a4e03f4.

Now you can run any command inside the container. For example, install the MySQL server in the running container. No need to run any command with sudo, because you’re operating inside the container with root privileges.

To install MySQL in the running container use the following command:

dnf install mysql

6. Commit changes in a container to a Docker image

In this step, you learn how to save the state of a container as a new Docker image on Rocky Linux 9.

After you installed MySQL in the Almalinux container, now you have a container running off an image, but the container is different from the image you used to create it.

First of all, you need to exit from it to save the state of the container as a new Docker image.

exit

Then, run the command below:

sudo docker commit -m "<What did you do to the image>" -a "<Author Name>" <container-id> <repository>/<new_image_name>

For example:

sudo docker commit -m "install mysql" -a "sam" 4c185a4e03f4 almalinux

Note: Remember to replace the container ID with your own.

Now you can list your docker images:

sudo docker images

The size difference means the changes were made.

7. List Docker Containers on Rocky Linux 9

In this step, we want to show how to list Docker containers on Rocky Linux 9.

To see active containers run the following command:

docker ps
**Output**
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

You can see all containers including active and non-active with the command below:

docker ps -a

If you want to see the latest container you created type:

docker ps -l

To stop a running or active container run the following command:

docker stop <container-id>

Note: The container-id can be found in the output from the docker ps command.

8. Push Docker images to a Docker repository

After you create a new image from an existing image you may want to share it with a few of your friends, the whole world on Docker Hub, or other Docker registries that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.

To have an account on Docker hub you need to register at Docker Hub.

If you want to log in to the Docker hub you will be asked for authentication :

docker login -u <docker-registry-username>

If you enter the correct password, authentication should succeed. Then you may push your own image using the following command:

docker push <docker-registry-username>/<docker-image-name>

It will take a little time to complete. After you push an image to a registry, it should be listed on your account’s dashboard.

Note: If a push attempt results in an error of this sort, login, then repeat the push attempt.

Conclusion

At this point, you have learned to Install and Use Docker CE on Rocky Linux 9. Also, you have learned to work with docker commands, pull images, list containers, etc. Mastering Install Docker on Rocky Linux 9 is an invaluable asset to every developer.

Hope you enjoy it. You may also interested in these articles:

Alternative Solutions for Containerization on Rocky Linux 9

While Docker is a popular choice for containerization, other viable options exist. Here are two alternative solutions for running containers on Rocky Linux 9:

1. Podman

Podman is a daemonless container engine for developing, managing, and running OCI Containers on your Linux System. Podman is designed to be a drop-in replacement for Docker in many cases. It offers several advantages, including rootless container execution and enhanced security.

Explanation:

  • Daemonless Architecture: Unlike Docker, Podman doesn’t require a central daemon process. This reduces the attack surface and simplifies management.
  • Rootless Containers: Podman allows you to run containers as a non-root user, improving security by limiting the potential impact of vulnerabilities.
  • Docker Compatibility: Podman is largely compatible with Docker images and commands, making migration relatively easy.

Installation and Usage:

  1. Installation:

    sudo dnf install podman -y
  2. Running a Container:

    podman run -it almalinux

    The commands are very similar to Docker, making the transition easier.

2. LXC/LXD

LXC (Linux Containers) is an operating-system-level virtualization environment for running multiple isolated Linux systems (containers) on a single control host. LXD is a container management tool built on top of LXC, offering a simpler and more user-friendly interface.

Explanation:

  • System Containers: LXC/LXD focuses on system containers, which are more like lightweight virtual machines than application containers.
  • Full Operating System: LXC/LXD containers can run a complete operating system, allowing for greater flexibility and compatibility with legacy applications.
  • Resource Isolation: LXC/LXD provides strong resource isolation between containers, ensuring that they don’t interfere with each other.

Installation and Usage:

  1. Installation:

    sudo dnf install snapd
    sudo systemctl enable --now snapd.socket
    sudo snap install lxd
    sudo lxd init
  2. Running a Container:

    lxc launch images:almalinux/9 mycontainer
    lxc exec mycontainer bash

These commands will launch a new AlmaLinux 9 container named "mycontainer" and provide you with a shell inside the container.

Choosing between these alternatives depends on your specific needs and priorities. If you prioritize security and rootless execution, Podman might be a good choice. If you need to run full operating systems within containers and require strong resource isolation, LXC/LXD might be more suitable. Remember that properly installing Install Docker on Rocky Linux 9 is vital for many developers.