Install Docker on Rocky Linux 8|9

|
Last Updated:
|
|

Welcome to our guide on how to install docker on Rocky Linux 8/Rocky Linux 9. Docker is a platform that enables developers and system administrators to build, run, and share applications with containers.

There exits two editions of docker available, Docker CE and Docker EE:

  • Docker (Community Edition) is the open-source, community supported version of Docker and is available for free.
  • Docker EE (Enterprise Edition) is a commercial/premium version of docker and is supported by Docker Inc.

We will be learning how to install and use Docker CE on Rocky Linux 8/Rocky Linux 9.

Installing Docker on Rocky Linux 8/Rocky Linux 9

There are different methods in which you can install Docker;

  • Installing docker from Docker Repositories
  • Installing docker manually using the RPM binary package
  • Installing docker using Docker Installation Script (Beyond the scope of this tutorial).

Install Docker on from Docker Repositories

Uninstall old Docker Versions

If any of the old docker versions are installed on your system, ensure that you remove them and their dependencies before you proceed.

dnf remove docker*

Install docker Repository on Rocky Linux 8/Rocky Linux 9

To ensure seamless future upgrades of Docker packages, you can install it from their official repos. For this, you need to setup the Docker repositories on Rocky Linux 8/Rocky Linux 9 by executing the command below;

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

The command above installs and enable docker repo on Rocky Linux 8/Rocky Linux 9.

Install Docker Engine (docker) on Rocky Linux 8/Rocky Linux 9

Once the repos are in place, proceed to install docker-ce and other tools including containerd.io, an open and reliable container runtime on Rocky Linux 8/Rocky Linux 9 and docker CLI utility, docker-ce-cli.

dnf install docker-ce

Sample output;

Docker CE Stable - x86_64                                                                                                               138 kB/s |  32 kB     00:00    
Dependencies resolved.
========================================================================================================================================================================
 Package                                            Architecture                 Version                                   Repository                              Size
========================================================================================================================================================================
Installing:
 docker-ce                                          x86_64                       3:24.0.7-1.el9                            docker-ce-stable                        24 M
Installing dependencies:
 checkpolicy                                        x86_64                       3.5-1.el9                                 appstream                              345 k
 container-selinux                                  noarch                       3:2.205.0-1.el9_2                         appstream                               50 k
 containerd.io                                      x86_64                       1.6.24-3.1.el9                            docker-ce-stable                        33 M
 docker-ce-cli                                      x86_64                       1:24.0.7-1.el9                            docker-ce-stable                       7.1 M
 fuse-common                                        x86_64                       3.10.2-5.el9.0.1                          baseos                                 8.1 k
 fuse-overlayfs                                     x86_64                       1.11-1.el9_2                              appstream                               71 k
 fuse3                                              x86_64                       3.10.2-5.el9.0.1                          appstream                               53 k
 fuse3-libs                                         x86_64                       3.10.2-5.el9.0.1                          appstream                               91 k
 libslirp                                           x86_64                       4.4.0-7.el9                               appstream                               68 k
 policycoreutils-python-utils                       noarch                       3.5-1.el9                                 appstream                               71 k
 python3-audit                                      x86_64                       3.0.7-103.el9                             appstream                               83 k
 python3-distro                                     noarch                       1.5.0-7.el9                               appstream                               36 k
 python3-libsemanage                                x86_64                       3.5-1.el9                                 appstream                               79 k
 python3-policycoreutils                            noarch                       3.5-1.el9                                 appstream                              2.0 M
 python3-setools                                    x86_64                       4.4.1-1.el9                               baseos                                 543 k
 python3-setuptools                                 noarch                       53.0.0-12.el9                             baseos                                 839 k
 slirp4netns                                        x86_64                       1.2.0-3.el9                               appstream                               45 k
 tar                                                x86_64                       2:1.34-6.el9_1                            baseos                                 876 k
Installing weak dependencies:
 docker-buildx-plugin                               x86_64                       0.11.2-1.el9                              docker-ce-stable                        13 M
 docker-ce-rootless-extras                          x86_64                       24.0.7-1.el9                              docker-ce-stable                       3.9 M
 docker-compose-plugin                              x86_64                       2.21.0-1.el9                              docker-ce-stable                        13 M

Transaction Summary
========================================================================================================================================================================
Install  22 Packages

Total download size: 98 M
Installed size: 385 M
Is this ok [y/N]: y

Installing Docker from an RPM package

You can as well install docker on using an RPM package.

Download the latest versions of the individual packages installed above from Docker Packages page for Rocky Linux. Choose your release number e.g 8 for Rocky 8 or 9 for Rocky 9. After that, navigate to /x86_64/stable/Packages/ depending on your system arch.

Replace the version/release numbers of the packages accordingly.

dnf install https://download.docker.com/linux/centos/9/x86_64/stable/Packages/docker-ce-24.0.7-1.el9.x86_64.rpm  -y
dnf install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/docker-ce-cli-24.0.7-1.el9.x86_64.rpm 
dnf install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.6.9-3.1.el9.x86_64.rpm 

Starting Docker Service

To start and enable Docker and Containerd service to run on system boot;

systemctl enable --now docker containerd

Running Docker as a non-root user

If you run Docker as standard user, you may get such an error;

[kifarunix@rocky9 ~]$ docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json: dial unix /var/run/docker.sock: connect: permission denied

Therefore, if you need to run docker as non-root user, simply add the user you want to run docker as to docker group or simply, grant the user sudo rights.

usermod -aG docker USER

For example to add a user, kifarunix, to docker group;

groups kifarunix
kifarunix : kifarunix docker

Next, log out and login again as the non root user that you added to the docker group so that your group membership can be re-evaluated.

You can then run docker as a non root user.

Verifying docker installation

Check the docker version;

docker --version
Docker version 24.0.7, build afdd53b

To verify that docker is running well, you can try running the hello-world container image.

[kifarunix@rocky8 ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

To list all running/stopped containers;

[kifarunix@rocky8 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
4083300e3660        hello-world         "/hello"            3 minutes ago       Exited (0) 5 minutes ago                       laughing_visvesvaraya

Running Docker Images on Rocky Linux 8/Rocky Linux 9

While verifying docker installation above, we ran the hello-world docker container image. Building and running docker containerized applications is the next step after setting up your docker environment.

So there are two things here:

  • You can either build and run your own docker container images or
  • pull already created and shared images from docker hub, an image repository for Docker images.

In this demo, we will learn how to use already created docker images from docker hub. With docker command, you can easily pull docker images from docker hub, as we did with the hello-world image above.

If you want to build your own images and share on docker hub, then you need to sign up to create your own namespace for storing your images, which then can be publicly available.

Using docker command

docker command has quite a number of command line options for running various tasks.

The command line syntax is;

docker [OPTIONS] COMMAND

You can view various command options and commands by executing;

docker --help
...
Options:
      --config string      Location of client config files (default "/home/kifarunix/.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
...
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
...
Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
...

To view help for a specific docker command;

docker COMMAND --help

For example, to find the usage of docker run command;

docker run --help

Running Docker Images from Docker Hub

Searching for Docker Images on Docker Hub

You can search for the specific publicly available Docker images on Docker Hub using docker search command. The docker search command syntax is;

docker search [OPTIONS] TERM

Use docker search --help to see more options.

For example, to search all the images containing the term hello-world;

docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1479      [OK]       
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   150                  
tutum/hello-world                          Image to test docker deployments. Has Apache…   83                   [OK]
dockercloud/hello-world                    Hello World!                                    19                   [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          15                   [OK]
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   5                    [OK]
arm32v7/hello-world                        Hello World! (an example of minimal Dockeriz…   3                    
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
datawire/hello-world                       Hello World! Simple Hello World implementati…   1                    [OK]
markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                    [OK]
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   1                    
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   1                    [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]

Some column headings are self explanatory. Other columns include;

  • STARS : Shows the number of likes the image has had.
  • OFFICIAL : Specifies whether the image is built from a trusted source.
  • AUTOMATED : Shows whether the image has been automatically build from source code in an external repository and automatically pushed to Docker repositories.

Usually, an image with no prefix is considered official docker hub image, stable and being maintained. It also contains [OK] on the Official column.

Pulling Docker Image to Local Registry

Once you have identified the image that you need to run, you can either pull it and store it on your local registry using the the docker pull command or simply run it using docker run command.

For example, to search and pull an Ubuntu system docker image;

docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   12507     [OK]       
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   552                  [OK]
websphere-liberty                                         WebSphere Liberty multi-architecture images …   276       [OK]       
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   254                  [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   241                  [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   111       [OK]       
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                   [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   84        [OK]       
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                   [OK]
open-liberty                                              Open Liberty multi-architecture images based…   47        [OK]       
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44        [OK]       
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   25                   
solita/ubuntu-systemd                                     Ubuntu + systemd                                24                   [OK]
...

Let us pull the official Ubuntu image.

docker pull ubuntu

This will by default pull the most recent version of Ubuntu currently available, usually the latest LTS versions.

Using default tag: latest
latest: Pulling from library/ubuntu
a31c7b29f4ad: Pull complete 
Digest: sha256:b3e2e47d016c08b3396b5ebe06ab0b711c34e7f37b98c9d37abe794b71cea0a2
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

You can specify other tags for specific types of Ubuntu though;

docker search ubuntu:20.04

List Available Docker Images

You can list locally available images using docker images command.

docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        latest    c29284518f49   5 days ago     72.8MB
hello-world   latest    d1165f221234   4 months ago   13.3kB

Running a Docker Container Image

Once you have pulled a container from Docker hub and is available locally, you can either run it with docker run or docker create command.

With docker run, you can create a container, start it and access it using a shell to run any command inside it. The docker run command line syntax is;

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

For example, to run our Ubuntu image we pulled from Docker hub above in an interactive manner (option -i) drop to the shell (option -t, to allocate pseudo-TTY);

docker run -it ubuntu

This drops to the shell;

root@9b93a21dac21:/#

You are now inside an Ubuntu container, with a container ID, 9b93a21dac21, as root user. Anything that you run or execute under this shell happens within the filesystem of the container itself.

You can execute the commands directly without dropping to container shell;

docker run ubuntu cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

If you run the container interactively and started a shell, you can run your commands inside it.

When you exit the shell, the shell attached to the container is detached and container stops. If you used the options, -i and -t when running docker container, you can exit the shell without stopping the container, use the keystrokes Ctrl + p then Ctrl + q.

You can as well run container in background and print container ID

docker run -dit --name ubuntudemo ubuntu

To attach it to the console;

docker attach ubuntudemo

Listing Docker Containers

You can list live (currently running) containers using docker ps command.

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
a1a815bb0de6   ubuntu    "bash"    5 seconds ago   Up 2 seconds             ubuntu-demo

To list all containers, those running and those exited;

docker ps -a
CONTAINER ID   IMAGE         COMMAND                 CREATED              STATUS                          PORTS     NAMES
a1a815bb0de6   ubuntu        "bash"                  26 seconds ago       Up 22 seconds                             ubuntu-demo
075d2a694b0c   ubuntu        "bash"                  About a minute ago   Exited (0) About a minute ago             ubuntudemo
be8a66af520a   ubuntu        "cat /etc/os-release"   2 minutes ago        Exited (0) 2 minutes ago                  eloquent_dirac
9b93a21dac21   ubuntu        "bash"                  3 minutes ago        Exited (0) 2 minutes ago                  happy_elbakyan
61407c90dae8   hello-world   "/hello"                13 minutes ago       Exited (0) 13 minutes ago                 intelligent_gagarin

Start and Stop Docker Containers

You can start exited/stopped containers using docker start command.

For example, to start container named, happy_elbakyan, with an ID of 9b93a21dac21, run the command;

docker start happy_elbakyan

Or

docker start 9b93a21dac21

Similarly, you can stop a running docker using its name or container ID with docker stop command.

For example;

docker stop ubuntudemo

Removing Docker Containers

Once you are done with your containers, you can remove them using docker rm command.

To remove a stopped container with an ID of 9b7867c1daad, for example, in our docker ps -a output above;

docker rm 9b7867c1daad

You cannot remove a running container unless you force the removal using using -f or --force option of docker rm command.

docker rm 1c280cf21322 -f

Removing Docker Images

Docker images can as well be removed using docker rmi command. For example, you can list images and get the image ID of the specific image you want to remove;

docker images -a

You can show numeric IDS using -q option.

docker images -a -q
74435f89ab78
bf756fb1ae65

Delete an image;

docker rmi 74435f89ab78

Or use the name repo name;

docker rmi ubuntu

Use option -f or --force to force the deletion.

Further Reading

Docker Documentation

Other Tutorials

Install ModSecurity 3 with Apache in a Docker Container

Deploy a Single Node Elastic Stack Cluster on Docker Containers

Install and Run MariaDB as a Docker Container

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment