Install and Use Docker on Rocky Linux 8

In this guide from the Docker Tutorials, we want to teach you to Install and Use Docker on Rocky Linux 8.

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

To get more details about Docker, you can check our article about What is Docker and How does it work.

Install and Use Docker on Rocky Linux 8

To install Docker, you need to log in to your server as a non-root user with sudo privileges. To do this, you can check our guide the Initial Server Setup with Rocky Linux 8.

Installing Docker on Rocky Linux 8

First, you need to update your local package index with the following command:

sudo dnf update && sudo dnf upgrade

Then, you need to install the EPEL repository on Rocky Linux 8 with the following command:

sudo dnf install epel-release

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

sudo dnf remove podman buildah

Next, you need to add the official Docker CE repository on Rocky Linux 8 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 8 with the following command:

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

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 pres>
   Active: active (running) 
     Docs: https://docs.docker.com
 Main PID: 637 (dockerd)
    Tasks: 8
   Memory: 112.8M
   CGroup: /system.slice/docker.service
           └─637 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/contain>

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

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 that 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 8.

How To Use Docker on Rocky Linux 8

After the installation of Docker on Rocky Linux 8 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
Usage: docker [OPTIONS] COMMAND
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* Build with BuildKit (Docker Inc., v0.6.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.8.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
...

How To Work with Docker images

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
b8dfde127a29: Pull complete
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!

This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub
The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it
...

Also, you can search for a Docker image 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. 41 [OK]
almalinux/almalinux DEPRECATION NOTICE: This image is depre cated… 9
amd64/almalinux The official build of AlmaLinux OS. 1
arm64v8/almalinux The official build of AlmaLinux OS. 1
almalinux/mirror_service AlmaLinux OS mirror service. 1
super685/almalinuxssh 1
koichimurakamik6/almalinux AlmaLinux K6 SDK 1
...

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
ba2c2d4a4d0c: Pull complete
Digest: sha256:7843d8cb9fb74495159ba58d27421cbc408d1760ce94b5bf54e8ebda83272199
Status: Downloaded newer image for almalinux:latest
docker.io/library/almalinux:latest

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

sudo docker run almalinux

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    7a497d63e726   2 months ago   209MB
hello-world   latest    d1165f221234   6 months ago   13.3kB

You need to know about docker containers too. let’s see how Docker containers work on Rocky Linux 8.

How to run a Docker container

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:

[[email protected] /]#

Important Note: remember the container ID. here it is db8e8012568f.

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

How to 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 8.

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 "reita" 8ff73d2a54e1 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    d8597396d8df   10 seconds ago   499MB
almalinux     <none>    7a497d63e726   2 months ago     209MB
hello-world   latest    d1165f221234   6 months ago     13.3kB

The size difference means the changes were made.

How to List Docker containers

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

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
db8e8012568f 7a497d63e726 "/bin/bash" 24 hours ago Exited (0) 24 hours ago objective_mcnulty
43533bb6e777 7a497d63e726 "/bin/bash" 24 hours ago Exited (0) 24 hours ago funny_turing
a17f0282bb61 hello-world "/hello" 24 hours ago Exited (0) 24 hours ago relaxed_banzai

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.

How to 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 learn what Docker is and you can easily install it on your server and use it.

Hope you enjoy this article about How to Install and Use Docker on Rocky Linux 8.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular