How To Install Docker on Rocky Linux 9

In this tutorial, we want to teach you How To Install and Use Docker CE on Rocky Linux 9.

Docker is a free and open-source containerization tool that enables enable developers to package their applications into a container. Later that container image can be used to deploy and run applications on a public or private cloud.

Steps To Install and Use Docker CE on Rocky Linux 9

To install Docker on Rocky Linux 9 you need some requirements first.

Requirements

A fresh Rocky Linux 9.

And you need to log in to your server as a non-root user with sudo privileges. To do this, you can visit our guide on Initial server setup with Rocky Linux 9.

Install Docker on Rocky Linux 9

You need to update and upgrade the system’s package repository with the following command:

sudo dnf update && dnf upgrade

Now you need to install the EPEL repository on Rocky Linux 9 with the following command:

sudo dnf install epel-release -y

Note: If the podman and buildah packages exist, you need to remove them with the following command:

sudo dnf remove podman buildah

Add Docker CE Repository

Then, you need to add the official Docker CE repository on Rocky Linux 9 with the following command:

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

At this point, install the Docker-CE package on Rocky Linux 9 with the following command:

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

Manage Docker Service

When your installation is completed, start and enable the Docker service with the following command:

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

To check that your service is active and running, run the following command:

sudo systemctl status docker

In your output, you will see:

Output
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor pr>
     Active: active (running) since Thu 2022-12-22 05:32:07 EST; 14s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 72732 (dockerd)
      Tasks: 8
     Memory: 25.4M
...

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

How To Run Docker Commands

Note: As we mentioned before to run the docker commands you need to log in as a non-root user with root privileges. Or you can run a command with a user that is in a docker group which is created during the installation of Docker.

If you don’t want to use Sudo to run docker commands, you need to add your user to the docker group. To do this, run the following command:

sudo usermod -aG docker $(whoami)

At this point, log out of your server and then back in with the same user to enable these changes.

If you need to add a user to the docker group that you’re not logged in as, run the following command:

sudo usermod -aG docker username

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

How To Use Docker on Rocky Linux 9

After the installation of Docker on Rocky Linux 9 is finished, let’s see how to use the docker command-line utility.

The syntax of the Docker command is like this:

docker [option] [command] [arguments]

Run the command below to see the options and available commands on the docker:

docker

In your output, you will see:

Output
A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default
                           "/root/.docker")
  -c, --context string     Name of the context to use to connect to the
                           daemon (overrides DOCKER_HOST env var and
                           default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.9.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
...

How To Work with Docker images on Rocky Linux 9

The Docker Image is a portable file that contains a set of instructions that specify which software components the Container should run and how to run it.

At this point, let’s try to download and run the “hello-world” Docker image from the Docker hub. For this, execute the docker command with the subcommand run like the following command:

sudo docker run hello-world

In your output, you should see:

Output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

Search for Docker Images

Also, you can search for a Docker image that if the image exists or is not in the Docker hub. For example, we search for the AlmaLinux:

sudo docker search almalinux
Output
NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
almalinux                  The official build of AlmaLinux OS.             100       [OK]
almalinux/almalinux        DEPRECATION NOTICE: This image is deprecated…   9    
almalinux/8-micro          AlmaLinux OS 8 official micro container image   2    
almalinux/arm64v8          AlmaLinux OS 8 official aarch platform images   2    
almalinux/mirror_service   AlmaLinux OS mirror service.                    1    
almalinux/ks2rootfs        Kickstart to RootFS file builder in docker/p…   0    
almalinux/9-base           AlmaLinux 9 Base container image                0    
almalinux/9-micro          AlmaLinux 9 Micro container image               0    
...

Pull Docker Images

After you find the docker image that you want, use the pull subcommand to get your docker image:

sudo docker pull almalinux
Output
Using default tag: latest
latest: Pulling from library/almalinux
3fc9bb6a1ce5: Pull complete
Digest: sha256:e045e93d1a86963aa1bd4aa0aec05362ed529174d0d6c5617aa1116223b04d6f
Status: Downloaded newer image for almalinux:latest
docker.io/library/almalinux:latest

Run Docker Images

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

sudo docker run almalinux

List Docker Images

Now, you can list docker images with the following command:

sudo docker images

You will see:

Output
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
almalinux     latest    acaca326f3b3   2 weeks ago     190MB
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB

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

How Run a Docker Container on Rocky Linux 9

Against virtual machines, are containers. They can be the best replacement for virtual machines. Containers separate the executive environments and share the operation system’s core.

To run the container with an Almalinux image, run the command below:

sudo docker run -it almalinux

Note: -it switch gives you interactive shell access into the container.

Your output should 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

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

Your output should similar to this:

Output
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
almalinux     latest    79aa11bb00ab   15 seconds ago   190MB
almalinux     <none>    acaca326f3b3   2 weeks ago      190MB
hello-world   latest    feb5d9fea6a5   15 months ago    13.3kB

The size difference means the changes were made.

List Docker Containers

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
Output
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                       PORTS     NAMES
4c185a4e03f4   acaca326f3b3   "/bin/bash"   10 minutes ago   Exited (130) 6 minutes ago             musing_galileo
bef06e108a94   acaca326f3b3   "/bin/bash"   12 minutes ago   Exited (0) 12 minutes ago              sharp_blackwell
7e5e6ef59825   acaca326f3b3   "/bin/bash"   18 minutes ago   Exited (0) 18 minutes ago              gifted_curran
1e04e81eaf68   hello-world    "/hello"      21 minutes ago   Exited (0) 21 minutes ago              distracted_cohen

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.

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, log in, then repeat the push attempt.

Conclusion

At this point, you have learned to Install and Use Docker on Rocky Linux 9.

Hope you enjoy it.

You may be intrested in these articles:

Enable RPM Fusion Repository on Rocky Linux 9

How To Install WineHQ on Rocky Linux 9

Set up NTP Server and Client on Rocky Linux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!