How To Install and Use Docker on AlmaLinux 9

This tutorial intends to teach you How To Install and Use Docker on AlmaLinux 9.

Docker is a software platform for building applications based on containers—small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another.

To know more about Docker, you can visit What is Docker and How Does it Work?

Steps To Install and Use Docker on AlmaLinux 9

to complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with AlmaLinux 9.

Install Docker on Almalinux 9

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

sudo dnf update -y && sudo dnf upgrade -y

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

sudo dnf install epel-release -y

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

sudo dnf remove podman buildah

Then, you need to add the official Docker CE repository on AlmaLinux 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 AlmaLinux 9 with the following command:

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

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 Mon 2022-09-26 05:42:55 EDT; 12s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 16819 (dockerd)
      Tasks: 8
     Memory: 25.8M
        CPU: 354ms
     CGroup: /system.slice/docker.service
...

Add User To the Docker Group

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

Note: To run 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 AlmaLinux 9.

How To Use Docker Command Line Utility on AlmaLinux 9

After the installation of Docker on AlmaLinux 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
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
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.17.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

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
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

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
2db29710123e: Pull complete
Digest: sha256:62af9efd515a25f84961b70f973a798d2eca956b1b2b026d0a4a63a3b0b6a3f2
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/

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.             85        [OK]
almalinux/almalinux        DEPRECATION NOTICE: This image is deprecated…   9    
almalinux/8-micro          AlmaLinux OS 8 official micro container image   2    
almalinux/podman                                                           1    
almalinux/mirror_service   AlmaLinux OS mirror service.                    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
9589bd88d106: Pull complete
Digest: sha256:c8369022fa04c7d8f3c0504d4732fabf402ed756753d47291af5798c7910f7bd
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    d1c25ed4de19   3 weeks ago     189MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB

You need to know about docker containers too. Let’s see how Docker containers work on AlmaLinux 9.

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:

[reita@124c67e3fdad /]#

Important Note: Remember the container ID. Here it is 124c67e3fdad.

Now you can run any command inside the container. For example, install the MariaDB 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 MariaDB in the running container, use the following command:

dnf install mariadb

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 AlmaLinux 9.

After you installed MariaDB 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 mariadb" -a "reita" 124c67e3fdad almalinux1

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
almalinux1    latest    6b6efda1231c   14 seconds ago   324MB
almalinux     latest    d1c25ed4de19   3 weeks ago      189MB
hello-world   latest    feb5d9fea6a5   12 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 AlmaLinux 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
124c67e3fdad   almalinux     "/bin/bash"   5 minutes ago    Exited (0) 2 minutes ago              vibrant_villani
a16d33ad4ec9   almalinux     "/bin/bash"   5 minutes ago    Exited (0) 5 minutes ago              test
b6e386670a79   almalinux     "/bin/bash"   13 minutes ago   Exited (0) 13 minutes ago             jovial_noether
f9d44538d565   almalinux     "/bin/bash"   15 minutes ago   Exited (0) 15 minutes ago             hardcore_merkle
114c9d1bed34   hello-world   "/hello"      18 minutes ago   Exited (0) 18 minutes ago             determined_lichterman

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, login, 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 AlmaLinux 9.

For more guides, you can visit the Docker Tutorials.

You may be like these articles:

Install Caddy Web Server on AlmaLinux 9

Install and Configure XRDP on AlmaLinux 9

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular