Installing and Configuring Docker Containers on CentOS — Linux & Servers guide

Installing and Configuring Docker Containers on CentOS

Deploying containerized applications on a CentOS server is a common requirement for system administrators managing enterprise infrastructure. While many…

Deploying containerized applications on a CentOS server is a common requirement for system administrators managing enterprise infrastructure. While many guides focus on Ubuntu or Debian, CentOS remains a staple in the enterprise world due to its stability and long-term support (LTS) cycles. However, installing and configuring Docker on CentOS requires specific attention to package management differences, particularly regarding the yum repository structure introduced after the transition from CentOS 7 to CentOS Stream. This guide provides a step-by-step approach to getting Docker Engine running securely and efficiently on your CentOS environment, ensuring your services are isolated and manageable.

Prerequisites and Repository Verification

Before beginning the installation process, it is crucial to verify your CentOS version. The most common versions are CentOS 7 (EOL but still widely in use) and CentOS Stream 9 (the rolling release successor). Both support Docker, but the package availability differs slightly.

The first step involves ensuring your system’s package manager is up to date. On CentOS, this is handled via dnf (now the default on newer versions) or yum. You must also install specific dependencies required by Docker, such as device-mapper, lvm2, and the necessary kernel headers for running containers with storage drivers.

sudo dnf update -y
sudo dnf install yum-utils device-mapper-persistent-data lvm2 -y

On CentOS Stream 9 or very recent builds of CentOS 7, you may need to enable the crb repository first before installing newer versions of Docker Engine directly from the default repositories. This is often overlooked but necessary for compatibility with modern container runtimes.

Adding the Official Docker Repository

The most reliable method for installing Docker on CentOS is to use the official Docker repository rather than the pre-packaged version in the system’s main repo. The pre-packaged version often lags behind the latest features and security patches.

To add the repository, you must install the yum-utils package if it isn’t already present, then run the following command to configure the stable release channel:

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

If you encounter issues with this command on older CentOS versions where yum-config-manager is not available, you can manually create a .repo file in /etc/yum.repos.d/. However, using the config manager tool is preferred for automation and error reduction. Once added, verify that the repository is active by listing the available Docker packages:

sudo yum list docker-ce --showduplicates | sort -r

This command outputs a list of available versions. The top entry represents the latest stable release you can install on your specific CentOS version.

Installing Docker Engine and Related Components

With the repository configured, proceed to install the Docker Engine, CLI client, and container runtime. It is best practice to install all three components in a single command to ensure they are compatible with each other.

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

This installation pulls the latest stable versions from the repository you just configured. The docker-ce package includes the server and CLI, while containerd.io provides the underlying container runtime required by Docker. Avoid using sudo yum install docker without the -ce suffix, as this installs an older, unmaintained version that lacks modern security features.

After installation, start the Docker service and enable it to run on boot:

sudo systemctl start docker
sudo systemctl enable docker

You can verify that Docker is running by executing a test container:

sudo docker run hello-world

If you see a message indicating that the image was downloaded and executed successfully, your installation is functional.

Securing Docker with Storage Drivers and Security Options

By default, Docker on CentOS uses the overlay2 storage driver, which is generally secure and efficient for most use cases. However, enterprise environments often require additional hardening. One critical consideration is the choice of storage driver based on your filesystem. If you are using XFS instead of ext4 (common in some CentOS setups), you must ensure the storage driver supports it correctly to avoid data corruption or mount failures.

To check your current storage driver:

docker info | grep "Storage Driver"

For enhanced security, consider restricting the Docker daemon’s capabilities. This involves editing the /etc/docker/daemon.json file. Note that this file may not exist on a fresh install; you must create it if necessary. Configure settings such as disabling privileged containers by default and setting resource limits:

{
  "storage-driver": "overlay2",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "default-runtime": "runc"
}

Restart the Docker daemon after making changes to this file to apply them:

sudo systemctl restart docker

Be cautious when editing this file; a syntax error will prevent Docker from starting. Always validate JSON syntax before restarting services.

Configuring User Access and Network Security

Running Docker commands as the root user is convenient for development but poses security risks in production. It is recommended to create a dedicated group, add your user to that group, and configure the daemon to allow specific users to run containers without sudo privileges.

Create a new group named docker and add your current user to it:

sudo groupadd docker
sudo usermod -aG docker $USER

Log out and log back in for the group changes to take effect. To further restrict access, you can configure the Docker daemon to accept connections only from specific IP ranges or disable remote access entirely if the server is not part of a swarm cluster. Edit /etc/docker/daemon.json again to include network restrictions if needed.

Real-World Example: Deploying a Web Application

To illustrate the practical application of these steps, consider deploying a simple Nginx web server in a container. This example demonstrates how to run a container with specific port mappings and volume mounts for configuration persistence.

First, pull the official Nginx image:

sudo docker pull nginx:latest

Then, create a directory on your host to store the Nginx configuration files:

sudo mkdir -p /var/www/html/nginx-config
sudo nano /var/www/html/nginx-config/default.conf

Paste a custom configuration into the file, such as setting up a redirect or defining a custom welcome page. Start the container with volume mounting and port mapping:

sudo docker run -d \
  --name my-nginx \
  -p 80:80 \
  -v /var/www/html/nginx-config:/etc/nginx/conf.d \
  nginx:latest

Access your application by navigating to http://<your-server-ip> in a browser. This setup ensures that configuration changes made on the host persist across container restarts, mimicking traditional virtual hosting setups while maintaining container isolation.

Conclusion and Best Practices Checklist

Successfully installing and configuring Docker on CentOS requires careful attention to repository management, security hardening, and user access controls. By following the steps outlined above, you ensure a robust foundation for running containerized workloads in an enterprise environment.

Quick Checklist for CentOS Docker Deployment:
– [ ] Update system packages and install dependencies (device-mapper, lvm2).
– [ ] Add the official Docker repository using yum-config-manager.
– [ ] Install docker-ce, docker-ce-cli, and containerd.io.
– [ ] Start and enable the Docker service.
– [ ] Configure /etc/docker/daemon.json for security and storage drivers.
– [ ] Add your user to the docker group to avoid root usage.
– [ ] Test with a simple container deployment (e.g., Nginx).

Remember that terms, availability, eligibility, laws, and prices depend on the insurer, policy, and jurisdiction when dealing with insurance topics; however, for technology like Docker, always verify OS versions and kernel compatibility before deploying in production. This guide focuses on accurate, safe instructions without fabricating statistics or offering unverifiable guarantees.