Docker is an OpenSource software that provides OS-level virtualization for developing, shipping, and running applications. In Docker, applications are delivered as packages standardized units called containers. Each container is isolated from one another, including libraries and configuration files, but each of them can communicate through well-defined network channels.

As for now, Docker has become a standard for software development and DevOps. It’s used by millions of developers to build, share, and run any applications every day at work. It’s used in the lifecycle of application development, from the development itself, testing, and production. With Docker, the application becomes more flexible and portable. Applications can be run anywhere in different operating systems with an easy way, but the application itself, remain the same from libraries to configuration files.

In this guide, you will learn how to install Docker CE (Community Edition) on Alma Linux 9. This guide also covers the basic usage of Docker for managing images, containers, and volumes, working with container logs, and accessing containers via Docker Host.

Prerequisites

This guide required the following:

  • A Linux server running Alma Linux 9 – This example uses an Alma Linux server with hostname ‘alma-linux‘ and IP address ‘192.168.5.’
  • A non-root user with sudo/root administrator privileges.

Adding Docker CE Repository

Docker is an orchestration tool for developing, shipping, and running applications via container. It makes the development process faster because Docker allows you to create application images without relying on the host machine. This makes the distribution of applications to developers faster and easier.

Docker is a multi-platform container orchestration, it can be installed on Desktop and Server. If you’re using a Desktop, you can install Docker Desktop, but if you’re using a server, you can install Docker Engine via the official Docker repository.

In the first section, you will set up the Docker CE repository on your Alma Linux server.

Run the following command to install the ‘dnf-utils‘ package.

sudo dnf install dnf-utils

Input y when prompted, then press ENTER to proceed.

<img alt="install dnf utils" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/1-install-dnf-utils.png64627f313853d.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="168" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, add the Docker CE repository using the following dnf command.

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

Once the Docker CE repository is added, verify using the command below. This will show you the list of available repositories on your Alma Linux.

sudo dnf repolist

When successful, you should receive an output like this:

<img alt="adding repository" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/2-adding-docker-repository.png64627f316ac49.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="173" loading="lazy" src="data:image/svg xml,” width=”750″>

Move on to the next section for installing Docker CE.

Installing Docker CE

In this section, you will install Docker CE on the Alma Linux server. By the end of this section, you will have Docker up and running on your system.

Install Docker CE using the following dnf command. With this, you will install Docker with the additional package ‘docker-buildx-plugin‘ is the Docker Build plugin that makes the building of images faster, and the ‘docker-compose-plugin‘ is the Docker Compose plugin for easier development of applications.

sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

When prompted, input y to confirm, then press ENTER to proceed.

<img alt="installing Docker" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/3-installing-docker.png64627f31834dd.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="448" loading="lazy" src="data:image/svg xml,” width=”750″>

Also, you will be asked to confirm the GPG key for Docker repository. Input to confirm, then press ENTER.

<img alt="accept gpg key" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/4-acept-gpg-key.png64627f31ace01.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="193" loading="lazy" src="data:image/svg xml,” width=”603″>

Once Docker is installed, run the following systemctl command to start and enable the ‘docker’ service.

sudo systemctl start docker

sudo systemctl enable docker

After executing the command, Docker will be running and also would be enabled, which means Docker will start automatically upon system startup.

Run the command below to verify the Docker service status and ensure that the Docker is running.

sudo systemctl status docker

When Docker is running, you should expect output like the following:

<img alt="start enable and verify docker" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/5-start-enable-docker.png64627f31e8d5e.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="301" loading="lazy" src="data:image/svg xml,” width=”750″>

Allowing Non-root User To Run Docker Container

By default, only the root user that allowed to run the container via Docker. This section will show you how to allow non-root users to run Docker containers by adding the user to the existing group called ‘docker‘.

Run the following command to add your user to the ‘docker‘ group. This example uses a user called ‘testuser‘.

sudo usermod -aG docker testuser

Now, log in as your user using the following command. Then execute the ‘docker run‘ command below to run the container ‘hello-world‘.

su - testuser

docker run hello-world

When successful, you should receive an output like the following:

<img alt="running container as non root user" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/6-running-container-non-root.png64627f323b6c6.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="615" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the following docker command to check the running container on your system.

docker ps

docker ps -a

The first ‘docker ps‘ command will have no output because it will only show you a running container, and the container ‘hello-world‘ is exited/stopped after the “Hello from Docker” message is printed. But for the second command with ‘docker ps -a‘, it will give you output for both running and exiting containers.

<img alt="docker check container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/7-docker-check-container.png64627f3259985.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="147" loading="lazy" src="data:image/svg xml,” width=”750″>

Basic Usage of Docker

You have now successfully reached the stage where the Docker is up and running, also allowed your non-root user to execute the Docker container. No, move on to the basic usage of Docker.

The following steps will show you some scenarios to use Docker. You will learn how to managingDocker images, working with the container and volume, and also learn how to access the container from the host machine.

With everything in place, let’s get started.

Working with Images

In the first step, you will be working with the Docker image. You will download the image and verify the list of available images on your local machine.

To download an image, visit the DockerHub and input the name Docker image that you want to download. In this example, you will download the ‘nginx‘image with tags ‘alpine‘. If click on the TAGS tab, you should see the list of available versions of the Nginx image.

<img alt="list tags docker hub" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/17-list-tags.png64627f32b4cf8.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="503" loading="lazy" src="data:image/svg xml,” width=”750″>

Now run the ‘docker pull’ command below to download the Docker images. In this example, you will download the image ‘nginx:alpine‘, this will refers to the latest version of the Nginx Alpine image.

docker pull nginx:alpine

<img alt="docker pull download image" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/8-docker-pull-image.png64627f32d6420.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="283" loading="lazy" src="data:image/svg xml,” width=”737″>

Once downloaded, run the following command to list available images on your host. If you follow these steps from the beginning, then you should see two Docker images, ‘hello-world‘ with the tag ‘latest‘ and ‘nginx‘ with the tag ‘alpine‘.

docker images

<img alt="listing images" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/9-listing-images.png64627f3310e67.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="152" loading="lazy" src="data:image/svg xml,” width=”611″>

Working with Container

With the Docker image downloaded, you can now run the container. This section will show you how to create and run a container via Docker.

To run a container, run the following ‘docker run‘ command below.

docker run -it -d -p 8080:80 --name web nginx:alpine

This command will create a new container called ‘web‘, which is based on the Docker image ‘nginx:alpine‘. The new container ‘web‘ will expose port ‘8080‘ on the Docker host, which is equal to port 80 on the container. Additional parameter ‘-it‘ is used to run the container in interactive mode, and the parameter ‘-d‘ will run the container in the background.

After the command is executed, you should see the random number and string, which is the ‘CONTAINER ID‘.

Now, run the command below to check the list of running containers.

docker ps

You should expect an output like this – The container ‘web‘ with the status ‘Up‘ and exposed the port 8080 on the Docker host.

<img alt="run and check container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/10-run-container.png64627f332acb0.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="125" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the curl command below to access the container ‘web’ that is exposed on port 8080. When successful, you should see the default index HTML source code of the container ‘web‘.

curl http://192.168.5.43:8080/

<img alt="check nginx container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/11-checkin-nginx-container.png64627f336438a.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="534" loading="lazy" src="data:image/svg xml,” width=”707″>

Lastly, if you want to access your container from the outside network, you must open port 8080 on your Alma Linux server via the ‘firewall-cmd‘ command below.

sudo firewall-cmd --add-port=8080/tcp --permanent

sudo firewall-cmd --reload

Now open the web browser and your local machine and visit the server IP address followed by port 8080 (i.e: http://192.168.5.43:8080/). You should expect an output of the index.html page for the container ‘web‘.

<img alt="index.html nginx container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/18-default-index.png64627f339844f.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="257" loading="lazy" src="data:image/svg xml,” width=”750″>

Container Logging

Logging is important in Docker, especially if you’re developing images for your applications. Logging helps you debug your application development.

Run the following command to get full logs of the container ‘web‘.

docker logs web

Below is the similar output that you will get from the container ‘web‘.

<img alt="logging container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/12-logging-container.png64627f33b64b8.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="347" loading="lazy" src="data:image/svg xml,” width=”750″>

You can also limit the output of the logs via the ‘tail‘ parameter as below. This command will show you the last 5 lines of logs of the container ‘web‘.

docker logs --tail 5 web

<img alt="limit logging" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/13-limit-logs.png64627f33f333c.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="169" loading="lazy" src="data:image/svg xml,” width=”750″>

Managing Container

In this section, you will learn how to manage a container. You will learn how to start, stop, and remove the container.

To stop a container ‘web‘, run the ‘docker stop‘ command below.

docker stop web

Now, verify the container using the following command. You should get an output of the container ‘web‘ with the status ‘Exited‘.

docker ps

docker ps -a

<img alt="stopping container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/14-start-stop-container.png64627f342c218.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="139" loading="lazy" src="data:image/svg xml,” width=”750″>

To start again the container ‘web‘, run the command ‘docker start‘ below. Then verify the list of running containers using the ‘docker ps‘ command.

docker start web

docker ps

You should get an output that the container ‘web‘ is running with the status ‘Up‘.

<img alt="starting container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/16-starting-container.png64627f3441079.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="107" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, run the following command to remove the container ‘web‘. Before deleting the container, you must stop the container first.

docker stop web

docker rm web

If you check the list containers via the ‘docker ps‘ command, you should expect an output that the container ‘web‘ is deleted.

docker ps -a

<img alt="removing container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/15-removing-container.png64627f347c0fd.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="154" loading="lazy" src="data:image/svg xml,” width=”750″>

Working with Volume

Volume is where you attached your local volume/directory to the container. You can assume volume is like a ‘shared-folder‘ in the virtual machine. In this section, you will create a new container with a custom volume that allows you to attach the local directory to the container.

First, create a new project directory ‘~/project/data‘, and move your working directory into it.

mkdir -p ~/project/data; cd ~/project

Now, run the following command to create a new file ‘data/index.html‘. This file will replace the default index.html page of the new ‘web‘ container.

cat > data/index.html << EOF







 

  Welcome to Docker Nginx





 

Hello from Nginx container







EOF

<img alt="create custom index" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/19-create-index-html.png64627f34a3ad5.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="378" loading="lazy" src="data:image/svg xml,” width=”606″>

Now, run the following command to create and run a new container ‘web‘ with the custom volume via the ‘-v‘ parameter. In this example, you will mount the local directory ‘~/project/data‘ to the container ‘web’ with the target directory ‘/usr/share/nginx/html‘. The additional option ‘:ro‘ will make the directory ‘/usr/share/nginx/html‘ on the container read-only, while on the host machine read and write is still allowed.

docker run -it -d -p 8080:80 --name web -v ~/project/data:/usr/share/nginx/html:ro nginx:alpine

Next, run the following command to ensure that the new container ‘web‘ is running.

docker ps

When successful, you should expect an output like the following – The container ‘web‘ is running and exposed port 8080 on the host machine.

<img alt="create container with volume" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/20-create-container-custom-volumes.png64627f34c5d10.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="127" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, run the curl command below to access the container ‘web‘. When everything goes smoothly, you should see the source code of the custom index.html page that you’ve created.

curl http://192.168.5.43:8080/

<img alt="nginx container with custom index" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/21-access-index-html-custom.png64627f34efed5.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="275" loading="lazy" src="data:image/svg xml,” width=”620″>

Below is the page of custom index.html when accessed from the web browser (i.e: http://192.168.5.43:8080/).

<img alt="custom index" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/22-index-html-container.png64627f352b6d3.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="147" loading="lazy" src="data:image/svg xml,” width=”461″>

Accessing Container from Host

To access and log in to the container, you can use the ‘docker exec’ command, which allows you to execute the command that is available on the container.

Run the following command to log in to the container ‘web‘. This will execute the command ‘/bin/sh‘ on the container ‘web‘ and the parameter ‘-it‘ will take you straight to the container.

docker exec -it web /bin/sh

Once logged in to the container, run the following command to check the detailed hostname of your container. The container hostname should be the same as the ‘CONTAINER ID‘.

id

hostname

<img alt="log in to container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/23-login-to-container-via-terminal.png64627f3554d1e.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="218" loading="lazy" src="data:image/svg xml,” width=”724″>

Next, run the following command to check the IP address and the default gateway of the container.

ip a

route -n

You should expect the output like the following:

<img alt="show container ip" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/24-show-container-ip.png64627f357116b.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="326" loading="lazy" src="data:image/svg xml,” width=”750″>

Because the container ‘web’ is running the Nginx process, you can access your container via the local IP address via the curl command below. In this example, the container IP address is ‘172.17.0.2‘. You should get the same index.html page.

curl 172.17.0.2

<img alt="accessing nginx via local ip container" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/05/echo/25-access-via-host.png64627f359dab2.jpg" ezimgfmt="rs rscb7 src ng ngcb6" height="298" loading="lazy" src="data:image/svg xml,” width=”475″>

Conclusion

Now that you have completed this guide, you should know how to install Docker on Alma Linux 9. You have also completed the scenario for basic usage of Docker for managing images and containers, logging containers for debug purpose, working with docker volume, and the basic usage of ‘docker exec’ for logging in to the container.