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
sudoprivileges - 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 -yThis 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 lvm2Here's what each package does:
| Package | Purpose |
|---|---|
yum-utils | Provides the yum-config-manager utility for managing repositories |
device-mapper-persistent-data | Required for Docker's device mapper storage driver |
lvm2 | Logical 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.repoThis 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 repolistStep 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 dockersystemctl start docker— Starts the Docker service immediatelysystemctl enable docker— Configures Docker to start automatically every time the server reboots
Verify the Service Status
Confirm Docker is running correctly:
sudo systemctl status dockerYou 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-worldThis command does the following:
- Contacts the Docker Hub registry
- Downloads the lightweight
hello-worldtest image - Creates a new container from that image
- 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.
Step 7: Configure Docker for Non-Root Users (Optional but Recommended)
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 dockerAdd Your User to the Docker Group
Replace your_username with the actual Linux username:
sudo usermod -aG docker your_usernameApply the Changes
Log out and log back in to refresh your group membership. Alternatively, activate the change in the current session:
newgrp dockerTest Without Sudo
docker run hello-worldIf 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 pruneImage 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 -aRunning 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_nameViewing 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 statsStep 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 dockerTo disable automatic startup (for example, on a development machine where you don't always need Docker running):
sudo systemctl disable dockerStep 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-composeVerify the installation:
docker-compose --versionWith 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.jsonA 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 dockerCommon daemon settings you can configure include:
- Log driver and rotation — Prevent container logs from consuming all disk space
- Storage driver —
overlay2is 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 --reloadChoosing 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 -50Permission 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.ioContainer Networking Issues
Restart Docker to reinitialize network bridges:
sudo systemctl restart dockerConclusion
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:
- ✅ Updated the system and installed dependencies
- ✅ Added the official Docker repository
- ✅ Installed Docker CE and verified the installation
- ✅ Configured Docker to start on boot
- ✅ Set up non-root user access
- ✅ Learned essential Docker commands
- ✅ Installed Docker Compose for multi-container management
- ✅ 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.
