How to install and run Docker containers on a Webdock AlmaLinux 8 server

Last updated: July 29th 2024

Introduction

Docker is an open-source platform used to build, manage and run containerized applications. Docker packages the application in a container that has everything that the application needs to run.

This guide gives a brief overview of installation and running Docker containers on a Webdock server.

Prerequisites

  • Webdock cloud AlmaLinux 8 or newer instance.

Disclaimer

Docker under-the-hood manages iptables based on the ports published during the creation of containers. Do not expose ports on all interfaces if you do not want to expose a port publicly, instead use something like a "-p 127.0.0.1:80:80" to expose the port only on localhost (in this case the port is 80. For simplicity sake, you want to publish on all interfaces but block public access, you need to explicity block the port using a firewall like UFW.

This guide on UFW might help.

Installing Docker

First, uninstall the old version of Docker if it is already installed.

$ sudo dnf remove docker docker-engine docker.io containerd runc

It is fine if dnf says that none of these packages are installed.

Add the Docker repo with:

$ sudo dnf install -y yum-utils
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

Now install Docker engine along with dependencies.

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

Enable and start Docker service.

$ sudo systemctl enable docker.service
$ sudo systemctl start docker.service

Add your user to the Docker group. This will enable docker to run without using sudo (as a non-root user).

$ sudo usermod -aG docker $USER

To activate changes to groups, run the following commands.

$ sudo newgrp docker
$ groups

Check the Docker version.

$ docker version

Now to check that Docker is working correctly we will run hello-world.

$ docker run hello-world

If you see the message “Hello from Docker!” it means that docker is working correctly.

Changing storage driver

Note: You need to skip this step if you are installing Docker on our KVM servers as our KVM servers support Docker's overlay2 driver. So, for our KVM servers, a storage driver change is not necessary.By default, Docker on our legacy LXD servers defaults to using vfs as the storage driver, thereby causing high disk usage issues. Changing the storage driver to fuse-overlayfs will significantly reduce Docker disk usage and makes deploying containers a bit faster.

Fortunately, the fuse-overlayfs storage driver gets installed automatically with Docker. No need to install it manually. We just need to tell Docker to use the new storage driver.

Execute:

$ sudo dnf install nano -y
$ sudo nano /etc/docker/daemon.json

Then paste the below content to the above file and save it.

{
  "storage-driver": "fuse-overlayfs"
}

Finally restart the Docker daemon to apply the changes.

$ sudo systemctl restart docker

Now run the below command to check if the new storage driver is effective.

$ docker info | grep "Storage Driver:"

You should see the following output.

Storage Driver: fuse-overlayfs

Managing Docker images

In order to run a Docker container, a Docker image is required. Use the following command to pull the Docker image from DockerHub.

$ docker pull nginx

The above command will pull the nginx Docker image.

List all the downloaded Docker images on the host machine using the following command.

$ docker images

To remove a Docker image, use the following command.

$ docker rmi nginx

The above command will remove the nginx image with the latest tag from the host machine. For example, if you have nginx image with the tag version-2 then nginx in the above command becomes nginx:version-2.

Similarly, all the unused Docker images can be removed by using the following command.

$ docker image prune -a

To know detailed information about a Docker image (in this case nginx):

$ docker image inspect nginx

The above command will return a JSON object containing all the details of the nginx Docker image.

Managing Docker container

A container is created from a Docker image. Use the following command to create a Docker container.

$ docker create nginx

The above command will create a Docker container with a random name from the nginx image.

Multiple Docker containers can be created from a single Docker image. Also, the name of the Docker container can be specified while creating it by using the --name option.

$ docker create --name nginx-1 nginx

It will create a container named nginx-1 from the nginx Docker image.

The create command only creates a container. After creating, start the container using the following command.

$ docker start nginx-1

However, a Docker container can be created and started using a single command.

$ docker run --name nginx-2 nginx

The above command will create and start a container named nginx-2 from the nginx Docker image.

Add the --detach option to run the Docker container in detached mode.

$ docker run --name nginx-3 --detach nginx

Default Docker network does not allow access to the container port from the host machine. In order to access the Docker container port on the host machine, the container's ports are mapped to the host machine. Use -p option to specify port mapping.

$ docker run --name nginx-4 -p 8000:80 --detach nginx

It will run the nginx container and map port 80 of the container with port 8000 of the host machine. Now the nginx server can be accessed through port 8000 of the host machine.

Similarly, any directory of the Docker container can be mapped to the host machine for data persistence. Use the --volume option to map Docker volume to the host machine.

$ docker run --name nginx-5 -p 9000:80 --volume nginx:/var/www/html --detach nginx

It will create a Docker volume on the host machine and map it into the Docker container. List all the Docker volumes using the following command.

$ docker volume ls

To learn more about Docker networks and volumes, please visit this guide.

View all the running docker containers using the following command.

$ docker ps

It will display only running Docker containers. To display all the running including the stopped containers, use the following command.

$ docker ps -a

A running Docker container can be stopped using the following command.

$ docker stop nginx-3

To remove the stopped Docker container, use the following command.

$ docker rm nginx-3

The above command will remove the container named nginx-3 if it is stopped or exited.

To remove a running Docker container use the following command.

$ docker rm -f nginx-5

This command will remove the container named nginx-5 whether it is running or stopped.

To get the logs of a running Docker container for debugging purposes use the following command.

$ docker logs [container name]

Get into the Docker container shell to run some shell commands using the following command.

$ docker exec -it [container name] bash

This command will get you into the Docker container's shell and you can run some basic commands in the container.

The following command returns a JSON object containing detailed information of a Docker container.

$ docker container inspect nginx-2

Check resource utilization by Docker container

Docker containers use the resources of the host machine on which they are running. Check the memory and CPU consumption by Docker containers using the following command.

$ docker stats

This command will stream live memory and CPU utilization by the Docker containers.

Similarly check the storage used by the Docker images, containers, and volumes using the following command.

$ docker system df

This command will return the space utilization by Docker images, containers, and volumes.

Conclusion

This article showed the installation steps to run Docker on a Webdock AlmaLinux 8 server and some basic commands to manage Docker containers, images, and volumes.