Install Docker CentOS 7: Step-by-Step Guide
Docker is a powerful platform for building, shipping, and running applications in containers. This guide provides a comprehensive, step-by-step process for installing Docker Community Edition (CE) on CentOS 7. It covers prerequisites, different installation methods (using the repository is recommended), and post-installation setup to ensure a smooth and secure experience.
Prerequisites:
- CentOS 7: This guide assumes you have a clean or existing CentOS 7 system. The instructions may work on other RHEL-based distributions, but are optimized for CentOS 7.
- Root or Sudo Access: You’ll need
root
privileges or a user account withsudo
access to perform the installation. - Internet Connection: An active internet connection is required to download the necessary packages.
- 64-bit Architecture: Docker requires a 64-bit version of CentOS 7.
Installation Methods:
There are several ways to install Docker on CentOS 7. The recommended and most common method is using the official Docker repository. We’ll cover that method in detail, then briefly mention the alternative methods.
1. Installing Docker CE using the Repository (Recommended):
This is the preferred method as it ensures you get the latest stable release and makes future updates easier.
Step 1: Update System Packages
Before installing anything, it’s crucial to update your system’s package index:
bash
sudo yum update -y
The -y
flag automatically answers “yes” to any prompts, streamlining the process.
Step 2: Install Required Packages
Install the necessary utilities for managing repositories:
bash
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
- yum-utils: Provides the
yum-config-manager
utility, which is essential for adding and managing repositories. - device-mapper-persistent-data and lvm2: These packages are dependencies for the recommended
overlay2
storage driver. Docker uses a storage driver to manage the layers of container images.overlay2
is generally preferred over the olderdevicemapper
driver on CentOS 7.
Step 3: Add the Docker Repository
Add the official Docker CE repository to your system:
bash
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
This command adds the repository information to your system’s yum configuration.
Step 4: (Optional) Enable the edge
or test
repositories
If you want to install pre-release versions (e.g., for testing or development), you can enable the edge
or test
repositories:
“`bash
For the edge (monthly) releases:
sudo yum-config-manager –enable docker-ce-edge
For the test (nightly) releases:
sudo yum-config-manager –enable docker-ce-test
“`
Important: Only enable these repositories if you understand the risks associated with using pre-release software. For production environments, stick to the stable repository (enabled by default). You usually would not enable both edge
and test
. Choose one or neither.
Step 5: Install Docker CE
Now, install the Docker CE package:
bash
sudo yum install docker-ce docker-ce-cli containerd.io -y
This command installs:
- docker-ce: The Docker Engine.
- docker-ce-cli: The Docker command-line interface (CLI).
- containerd.io: A container runtime that Docker uses.
Step 6: Start the Docker Service
Start the Docker daemon:
bash
sudo systemctl start docker
Step 7: Enable Docker on Boot
Ensure Docker starts automatically on system boot:
bash
sudo systemctl enable docker
Step 8: Verify the Installation
Verify that Docker is installed and running correctly by running the hello-world
image:
bash
sudo docker run hello-world
This command downloads a test image and runs it in a container. If successful, you’ll see output confirming that Docker is working, including a message like:
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
2. Alternative Installation Methods (Less Recommended):
-
Installing from a Package (.rpm): You can download the
.rpm
package directly from the Docker website and install it usingyum localinstall
. However, this method doesn’t automatically handle dependencies or updates, making it less convenient.
bash
# Download the appropriate .rpm package
# Example (replace with the actual URL and filename):
# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-<VERSION>.el7.x86_64.rpm
# sudo yum localinstall docker-ce-<VERSION>.el7.x86_64.rpm -y -
Using a Convenience Script: Docker provides a convenience script (
get-docker.sh
) that automates the installation process. While convenient, it’s generally recommended to understand the steps involved and use the repository method for better control and maintainability. Use this method with caution and only if you trust the source of the script.
bash
# curl -fsSL https://get.docker.com -o get-docker.sh
# sudo sh get-docker.sh
This script detects your OS and installs Docker. Inspect the script before running it to understand what it does.
Post-Installation Setup:
Step 9: (Recommended) Manage Docker as a Non-Root User
Running Docker commands with sudo
all the time can be cumbersome and potentially risky. It’s best practice to add your user to the docker
group, allowing you to run Docker commands without sudo
.
bash
sudo usermod -aG docker $USER
usermod -aG docker
: Adds the user to thedocker
group.$USER
: This is an environment variable that automatically represents your current username.
Important: After running this command, you need to log out and log back in (or open a new terminal session) for the group membership change to take effect. Alternatively, you can use the following command to apply the group changes without logging out (but logging out is still recommended):
bash
newgrp docker
After logging back in (or using newgrp
), verify that you can run Docker commands without sudo
:
bash
docker run hello-world
Step 10: (Optional) Configure Docker to Use a Different Storage Driver
By default, CentOS 7 often uses the devicemapper
storage driver with loop-lvm
mode, which is not recommended for production. The overlay2
driver is generally preferred. The installation steps above should have configured overlay2
automatically, but it’s good to verify.
Check the current storage driver:
bash
docker info | grep "Storage Driver"
If the output shows Storage Driver: overlay2
, you’re all set. If it shows devicemapper
, and you see a warning about loop-lvm
in the docker info
output, you should switch to overlay2
.
To switch to overlay2
:
- Stop Docker:
sudo systemctl stop docker
- Backup (Optional but Recommended): If you have existing containers and images, back them up before making changes to the storage driver.
-
Edit
/etc/docker/daemon.json
: Create the file if it doesn’t exist. Add the following content:json
{
"storage-driver": "overlay2"
} -
Start Docker:
sudo systemctl start docker
- Verify:
docker info | grep "Storage Driver"
(should now showoverlay2
).
Step 11: (Optional) Configure Docker Daemon Options
You can further configure the Docker daemon by editing /etc/docker/daemon.json
. This file allows you to set various options, such as:
log-driver
: Configure the logging driver (e.g.,json-file
,syslog
,journald
).log-opts
: Configure logging options (e.g.,max-size
,max-file
).dns
: Specify custom DNS servers for containers.insecure-registries
: Allow connections to insecure Docker registries (use with caution).bip
: Specify a custom IP address and netmask for the Docker bridge network
For example, to limit the size and number of log files for containers using the json-file
driver:
json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Important: After making any changes to /etc/docker/daemon.json
, restart the Docker daemon:
bash
sudo systemctl restart docker
Troubleshooting:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
: This error usually means the Docker daemon isn’t running. Make sure you’ve started it withsudo systemctl start docker
. Also, ensure you’ve added your user to thedocker
group and logged out/back in if you’re trying to run Docker commands withoutsudo
.- Permission denied while trying to connect to the Docker daemon socket…`: Similar to the above, this often indicates a permissions issue. Double-check your group membership and ensure the Docker daemon is running.
- Errors related to storage driver: If you encounter issues with the storage driver, consult the Docker documentation for specific troubleshooting steps for your chosen driver.
yum
errors: If you have problems withyum
(e.g., repository errors), ensure your internet connection is working, and check the repository URLs for any typos. You might need to clean the yum cache (sudo yum clean all
).
This detailed guide covers the essential steps for installing and configuring Docker CE on CentOS 7. By following these instructions carefully, you should have a working Docker environment ready for your containerization needs. Remember to consult the official Docker documentation for more advanced configurations and troubleshooting.