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.
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.
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:
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.
Also, you will be asked to confirm the GPG key for Docker repository. Input to confirm, then press ENTER.
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:
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:
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.
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.
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
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‘.
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.
You should expect an output like this – The container ‘web‘ with the status ‘Up‘ and exposed the port 8080 on the Docker host.
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‘.
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‘.
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‘.
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
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
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‘.
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
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 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to Docker Nginx</title> </head> <body> <h2>Hello from Nginx container</h2> </body> </html> EOF
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.
When successful, you should expect an output like the following – The container ‘web‘ is running and exposed port 8080 on the host machine.
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.
Below is the page of custom index.html when accessed from the web browser (i.e: http://192.168.5.43:8080/).
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‘.
Next, run the following command to check the IP address and the default gateway of the container.
ip a route -n
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.
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.