6.21 Docker Configuration on the Server

TheHost Docker Banner

Docker – is a popular containerization platform that enables the development, deployment, and management of application containers. Through containerization, developers can quickly build, test, and deploy software in an isolated environment, minimizing dependency conflicts. This makes Docker an ideal tool for developing and maintaining modern cloud-based solutions. Docker significantly simplifies the application deployment process by allowing software to run in a unified environment, regardless of the host operating system’s configuration. Containers provide scalability, enhanced security, and ease of management, making them indispensable in DevOps practices.

Today, Docker is used in a wide range of scenarios — from local development to deploying complex distributed systems. In this guide, we will cover the following:

  • Installing Docker on various operating systems;
  • Running containers and working with images;
  • Essential Docker commands;
  • Configuring container autostart.

Terminology

The following sections use standard terminology related to working with Docker. Key concepts are described below:

Container (Docker Container)

Container– an isolated process running from an image. A container has:

  • its own namespace (processes, network, filesystem);
  • limited resources (CPU, RAM, disk);
  • access only to what is explicitly permitted.
Image (Docker Image)

Image– a template used to create containers. It includes:

  • a filesystem (e.g., Ubuntu, Alpine);
  • dependencies (libraries, packages);
  • application code;
  • instructions (from a Dockerfile), such as COPY, RUN, CMD. Important: Images are immutable. When you update the application, you create a new image.
Volume (Docker Volume)

Volume– a template used to create containers. It includes:

  • storing database data, logs, and configuration files;
  • sharing files between the host and the container;
  • providing shared access to data for multiple containers.

Server Preparation and Docker Installation

Important: Before setting up Docker, make sure that:

  • You have root access. Installation can be performed on our virtual or dedicated servers;
  • Virtualization support is enabled (required for running containers);
  • There is enough free disk space (Docker can consume significant space during active use).

Before performing any such actions, it is recommended to update your system. The command depends on your operating system:

Debian/Ubuntu:

1. Update the package list and system:

sudo apt update
sudo apt upgrade -y

2. Install the required dependencies:

#These packages allow the system to download Docker from an external repository over HTTPS.
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

Install the required dependencies 3. Add the official Docker GPG key:

# The GPG key is used to verify the authenticity of Docker packages.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

4. Add the Docker repository:

# Now the system knows where to download Docker from.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

5. Update the package list and install Docker:

# After that, Docker will be installed and ready to use.
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

Update the package list and install Docker

CentOS/AlmaLinux/RockyLinux

1. Install the necessary plugins for working with repositories:

sudo dnf install -y dnf-plugins-core

2. Add the official Docker repository:

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

3. Install Docker and its components:

sudo dnf install -y docker-ce docker-ce-cli containerd.io
How to enable Docker to start on boot?

If needed, you can configure Docker to start automatically after a system reboot:

sudo systemctl start docker
sudo systemctl enable docker

Essential Commands

Next, we will explore some commands with examples. In the examples, we will use the following placeholders:

  • my_command for commands
  • my_container for containers
  • my_image for images
  • my_tag for tags, etc

We will also use several command-line flags. For more detailed information about each command and its available flags, please refer to the official Docker.

Image Management
docker image my_command
  • build – build an image;
  • push – push an image to a remote registry;
  • ls – list available images;
  • history – show image layer history;
  • inspect – display detailed information about images;
  • rm – remove an image.

Examples:

Building an image with a specific tag
docker image build -t my_repo/my_image:my_tag
  • -t(--tag) – assigns a tag to the built image. In this case, it’s my_tag
Show list of available images
docker image ls

Show list of available images

Container Management
docker container my_command
  • create – create a container from an image;
  • start – start an existing container;
  • run – create and start a container;
  • ls – list running containers;
  • rename – rename a container;
  • inspect – display detailed information about a container.

Examples:

Creating a Container
docker container create my_repo/my_image:my_tag
Creating and Running a Container
docker container run -i -t -p 1000:8000 --rm my_repo/my_image:my_tag
  • -i(--interactive) --keeps STDIN open even if not attached to the container;
  • -t(--tty) – allocates a pseudo-TTY to connect the terminal with the container’s STDIN and STDOUT streams;
  • -p(--port) – maps a host port to a container port;

Examples of such port mapping:

  • “3000”
  • “3000-3005”
  • “8000:8000”
  • “9090-9091:8080-8081”
[label Creating and running a container with the `sh` terminal
docker container run -it my_repo/my_image:my_tag sh
  • Using both flags -i and -t enables terminal interaction with the container;
Listing running containers
docker container ls
Renaming a container. Changes the name from CONTAINER to NEW_NAME
docker container rename CONTAINER NEW_NAME
Displaying detailed container information
docker container inspect CONTAINER_ID
Viewing container logs
docker container logs
Gracefully stopping a running container by sending SIGTERM, then SIGKILL to the main process
docker container stop
Forcibly stopping a running container by sending SIGKILL to the main process
docker container kill

]

Volume Management
docker volume my_command
  • create – create a volume;
  • ls – list volumes;
  • inspect --view detailed information about a volume;
  • rm – remove a volume;
  • prune – remove all unused volumes.

Примеры:

Creating a standalone volume
docker volume create --name my_volume
Listing volumes
docker volume ls
Inspecting detailed information about a specific volume
docker volume inspect my_volume
Removing a volume
docker volume rm
Removing all volumes not used by any containers
docker volume prune
General Purpose Commands
docker my_command
  • version– display client and server version information;
  • login– log in to a Docker registry;
  • system prune– remove unused containers, networks, and images without a name or tag.

Примеры:

docker version

docker version

docker login

docker login

docker system prune

docker system prune

Common Errors and Their Solutions

Error: Cannot connect to the Docker daemon

Cause: The Docker serviceis not running or there are permission issues.

Solution:

# Make sure the Docker service is running
sudo systemctl status docker
# If it's inactive, start it:
sudo systemctl start docker
# Enable Docker to start on boot:
sudo systemctl enable docker
# If the issue is due to insufficient permissions, add the current user to the docker group:
sudo usermod -aG docker $(whoami)
# Reboot the system to apply the changes:
reboot
# After rebooting, check if Docker is working:
docker run hello-world
Error: No space left on device

Cause:: The server has run out of disk space, preventing the creation of new containers and images.

Solution:

  • Check which images and containers are using up space:
# Images
docker images
# Containers
docker ps -a
  • Remove unused Docker data:
# Remove specific images:
docker rmi <IMAGE_ID>
# Remove all containers:
docker rm $(docker ps -aq)
# Remove only stopped containers:
docker container prune
# Caution: this will remove all stopped containers, unused images, and networks:
docker system prune -a
  • Check remaining disk space:
df -h

Check remaining disk space

Error: Permission denied while trying to connect to the Docker daemon socket

Cause: he current user does not have access to the Docker socket (/var/run/docker.sock).

Solution:

  • Check socket ownership:
# If the owner is `root` and the current user is not in the `docker` group, this could be the issue.
ls -l /var/run/docker.sock
  • Add the user to the docker group:
sudo usermod -aG docker $(whoami)
  • Reboot the system and verify access to Docker:
reboot
# If this command runs successfully, the issue is resolved:
docker run hello-world

docker run hello-world

Error: Bind for 0.0.0.0:80 failed: port is already allocated

Cause: The port you’re trying to use (80 in this case) is already in use by another process.

Solution:

  • Find and stop the process:
# Replace `port` with the port number from the error message
sudo lsof -i :`port`
# (where `<PID>` is the process ID from the output of `lsof`)
sudo kill -9 <PID>

lsof

  • Or run the container on a different port:
docker run -d -p 8081:80 nginx

Note: For more information, refer to the official Docker resources: