15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
31.10.2024
1 +1

How to Install Docker on CentOS: Complete Step-by-Step Guide

Docker has fundamentally changed how developers build, ship, and run applications. By packaging software into lightweight, portable containers, Docker eliminates the classic "it works on my machine" problem and dramatically simplifies both development workflows and production deployments. If you're running CentOS and want to harness the power of containerization, this comprehensive guide walks you through every step — from a fresh system update to advanced configuration.

Whether you're deploying a single web application or orchestrating complex multi-container environments, getting Docker installed correctly on CentOS is the critical first step.

Prerequisites

Before you begin, make sure you have:

  • A CentOS 7 or CentOS 8 server (physical or virtual)
  • A user account with sudo privileges
  • A stable internet connection
  • Basic familiarity with the Linux command line

> Pro Tip: For the best Docker experience, consider running it on a VPS Hosting plan. A virtual private server gives you full root access, dedicated resources, and the flexibility to run containers without the restrictions of shared environments.

Step 1: Update the System

Before installing any new software, always update your existing packages to ensure compatibility and security. Run the following command:

sudo yum update -y

This command refreshes all installed packages to their latest versions. It may take a few minutes depending on how many updates are pending. A fully updated system reduces the risk of dependency conflicts during the Docker installation.

Step 2: Install Required Dependencies

Docker relies on several system-level packages to function correctly. Install them with a single command:

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

Here's what each package does:

PackagePurpose
yum-utilsProvides the yum-config-manager utility for managing repositories
device-mapper-persistent-dataRequired for Docker's device mapper storage driver
lvm2Logical Volume Manager support for Docker's storage backend

These dependencies ensure Docker can manage container storage efficiently and reliably on your CentOS system.

Step 3: Add the Official Docker Repository

CentOS's default package repositories don't include the latest Docker packages. You need to add Docker's official repository to get the most current, stable release:

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

This command adds Docker's official CentOS repository to your system's repository list. Using the official source guarantees you receive genuine, up-to-date Docker packages directly from Docker Inc., rather than potentially outdated versions from third-party mirrors.

You can verify the repository was added successfully by listing your configured repositories:

sudo yum repolist

Step 4: Install Docker Engine

With the repository in place, installing Docker is straightforward:

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

> Note: It's recommended to also install docker-ce-cli (the Docker command-line interface) and containerd.io (the container runtime) alongside the main engine for a complete, production-ready setup.

This installs the Docker Community Edition (CE) — the free, open-source version of Docker that is perfectly suited for both development and production workloads.

Step 5: Start and Enable the Docker Service

Installing Docker doesn't automatically start it. You need to start the Docker daemon and configure it to launch automatically on system boot:

sudo systemctl start docker
sudo systemctl enable docker
  • systemctl start docker — Starts the Docker service immediately
  • systemctl enable docker — Configures Docker to start automatically every time the server reboots

Verify the Service Status

Confirm Docker is running correctly:

sudo systemctl status docker

You should see output indicating the service is active (running). If the status shows any errors, review the system logs with journalctl -u docker for troubleshooting details.

Step 6: Verify the Docker Installation

Run Docker's built-in test to confirm everything is working as expected:

sudo docker run hello-world

This command does the following:

  1. Contacts the Docker Hub registry
  2. Downloads the lightweight hello-world test image
  3. Creates a new container from that image
  4. Runs the container, which prints a confirmation message

If your installation is successful, you'll see output that begins with:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Congratulations — Docker is now fully installed and operational on your CentOS server.

By default, Docker commands require sudo (root privileges). For security and convenience in development environments, you can allow specific non-root users to run Docker commands by adding them to the docker group.

Create the Docker Group

In most cases, this group is created automatically during installation. If not, create it manually:

sudo groupadd docker

Add Your User to the Docker Group

Replace your_username with the actual Linux username:

sudo usermod -aG docker your_username

Apply the Changes

Log out and log back in to refresh your group membership. Alternatively, activate the change in the current session:

newgrp docker

Test Without Sudo

docker run hello-world

If this runs without sudo and without a permission error, the configuration is working correctly.

> Security Warning: The docker group grants privileges equivalent to the root user. Only add trusted users to this group in production environments.

Step 8: Essential Docker Commands Reference

Once Docker is running, here are the most important commands you'll use on a daily basis:

Container Management

# List all running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# Start a stopped container
docker start container_id

# Stop a running container
docker stop container_id

# Remove a container
docker rm container_id

# Remove all stopped containers at once
docker container prune

Image Management

# List all locally stored images
docker images

# Pull an image from Docker Hub
docker pull image_name

# Pull a specific version/tag
docker pull image_name:tag

# Remove a local image
docker rmi image_name

# Remove all unused images
docker image prune -a

Running Containers

# Run a container interactively
docker run -it image_name /bin/bash

# Run a container in detached (background) mode
docker run -d image_name

# Run a container with port mapping
docker run -d -p 8080:80 image_name

# Run a container with a custom name
docker run -d --name my_container image_name

Viewing Logs and Stats

# View container logs
docker logs container_id

# Follow live log output
docker logs -f container_id

# View real-time resource usage
docker stats

Step 9: Confirm Docker Starts on Boot

Docker should already be configured to start on boot after running systemctl enable docker. To double-check or re-enable this behavior:

sudo systemctl enable docker

To disable automatic startup (for example, on a development machine where you don't always need Docker running):

sudo systemctl disable docker

Step 10: Advanced Configuration (Optional)

Install Docker Compose

Docker Compose is an essential tool for defining and managing multi-container applications using a simple YAML configuration file. Install the latest stable version:

sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify the installation:

docker-compose --version

With Docker Compose, you can define entire application stacks — web servers, databases, caches — in a single docker-compose.yml file and spin them all up with one command: docker-compose up -d.

Configure the Docker Daemon

You can customize Docker's behavior by editing (or creating) the daemon configuration file:

sudo nano /etc/docker/daemon.json

A common configuration example:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "storage-driver": "overlay2",
  "default-address-pools": [
    {"base": "172.17.0.0/16", "size": 24}
  ]
}

After editing the daemon configuration, restart Docker to apply changes:

sudo systemctl restart docker

Common daemon settings you can configure include:

  • Log driver and rotation — Prevent container logs from consuming all disk space
  • Storage driveroverlay2 is the recommended driver for CentOS
  • DNS settings — Set custom DNS servers for containers
  • Registry mirrors — Speed up image pulls with local mirrors

Configure Firewall Rules (CentOS 7)

If you're running firewalld, you may need to allow Docker's network traffic:

sudo firewall-cmd --zone=public --add-masquerade --permanent
sudo firewall-cmd --reload

Choosing the Right Hosting Environment for Docker

Running Docker in production requires a reliable, high-performance server environment. Here are the best options depending on your use case:

  • VPS Hosting — Ideal for most Docker deployments. Full root access, scalable resources, and cost-effective pricing make VPS the go-to choice for containerized applications.
  • Dedicated Servers — Best for high-traffic production environments or when you need maximum CPU, RAM, and storage performance for running dozens of containers simultaneously.
  • GPU Hosting — Perfect for AI/ML workloads running inside Docker containers that require GPU acceleration for model training or inference.
  • VPS with cPanel — If you need Docker alongside a user-friendly control panel for managing websites and email, this combination offers the best of both worlds.

Troubleshooting Common Docker Issues on CentOS

Docker Daemon Fails to Start

Check the system logs for error details:

journalctl -u docker --no-pager | tail -50

Permission Denied Errors

If you see Got permission denied while trying to connect to the Docker daemon socket, ensure your user is in the docker group and that you've logged out and back in.

Cannot Pull Images

Verify your server has outbound internet access and that DNS is resolving correctly:

curl -I https://registry-1.docker.io

Container Networking Issues

Restart Docker to reinitialize network bridges:

sudo systemctl restart docker

Conclusion

You've now successfully installed Docker on CentOS and have a solid foundation for working with containers. Here's a quick recap of what was covered:

  1. ✅ Updated the system and installed dependencies
  2. ✅ Added the official Docker repository
  3. ✅ Installed Docker CE and verified the installation
  4. ✅ Configured Docker to start on boot
  5. ✅ Set up non-root user access
  6. ✅ Learned essential Docker commands
  7. ✅ Installed Docker Compose for multi-container management
  8. ✅ Explored daemon configuration options

Docker on CentOS opens up a world of possibilities — from running isolated development environments to deploying scalable microservices architectures in production. The containerization skills you build here will serve as the foundation for more advanced topics like Kubernetes orchestration, CI/CD pipelines, and cloud-native application development.

For the best Docker experience, make sure your server environment is up to the task. Explore AlexHost's VPS Hosting plans for a powerful, reliable, and affordable foundation for all your containerized workloads.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started