This tutorial is going to cover how to install Docker on CentOS 7. There are several approaches for us:
- Install using Docker Repository
- Install using RPM Package
We are going to get through both approaches and then additional Docker configurations.
1. Prerequisites
- The CentOS 7 must be 64-bits OS
- Kernel version should be 3.10 at minimum
You can do a quick check by issuing the following command:
1 |
uname -r |
My CentOS 7 kernel version is 3.10.0. It’s good to get started.
- Logged in user has sudo or root privileges
2. Install Docker on CentOS 7 using Docker Repository
By install Docker on CentOS 7 using repository, we firstly need to set up the Docker repository. Afterward, we can install, update, or downgrade Docker from the repository.
2.1. Setup The Docker Repository
Firstly, we need to install yum-utils, which provides the yum-config-manager utility:
1 |
sudo yum <span class="hljs-keyword">install</span> -y yum-utils |
Then, set up the stable repository by using the following command:
1 2 3 |
sudo yum-config-manager \ --add-repo \ https://docs.docker.com/engine/installation/linux/repo_files/centos/docker.repo |
2.2. Install Docker
2.2.1. Update the Yum package index
1 |
sudo yum makecache fast |
2.2.2. Install the latest version of Docker or go to next step to install a specific version
1 |
sudo yum -y <span class="hljs-keyword">install</span> docker-<span class="hljs-keyword">engine</span> |
2.2.3. Install a specific version of Docker
Step 1. List all available versions of Docker
1 |
yum list docker-engine.x86_64 --showduplicates |sort -r |
The output will be similar to below:
1 2 3 |
docker-engine.x86_64 1.13.0-1.el7.centos docker-main docker-engine.x86_64 1.12.6-1.el7.centos docker-main docker-engine.x86_64 1.12.5-1.el7.centos docker-main |
Step 2. Install a specific version
We can install a specific version of Docker by appending the version string to the package name and separate them by a hyphen (-):
1 |
sudo yum -y install docker-engine-<VERSION_STRING> |
For example, let’s install the Docker version 1.12.6-1.el7.centos
1 |
sudo yum -y install docker-engine-1.12.5-1.el7.centos |
2.3. Start the Docker
1 |
sudo systemctl <span class="hljs-keyword">start</span> docker |
2.4. Verify the installation
Verify that docker is installed correctly by running the hello-world image
1 |
sudo docker run hello-world |
You should see the result that is similar as below image:
3. Install Docker on CentOS 7 using RPM Package
If for any reason that we cannot use Docker’s repository to install Docker on CentOS 7, we can download the .rpm file for our release and install it manually. By installing Docker this way, to upgrade Docker, we have to download the newer package and install it by ourselves.
3.1. Download the Docker RPM Package
Go to https://yum.dockerproject.org/repo/main/centos/, choose the subdirectory for your CentOS version and download the .rpm file for the Docker version we want to install.
For example, the following command will download the Docker RPM package 1.3.1-1
1 |
curl -O https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.13.1-1.el7.centos.x86_64.rpm |
3.2. Install the Docker
The next step is to install the RPM package. We can change the path below to the path where we downloaded the Docker package:
1 |
sudo yum -y install ./docker-engine-1.13.1-1.el7.centos.x86_64.rpm |
4. Additional Configurations
4.1. Avoid using sudo command when use docker commands.
By default, docker deamon run with root user. To avoid using sudo when use docker commands, we will create a group docker and add user to that group. According to the docker document, when the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
4.1.1. Create docker group
1 |
sudo groupadd docker |
4.1.2. Add our desire user to that group.
1 |
sudo usermod <span class="hljs-_">-a</span>G docker <span class="nv"><span class="hljs-variable">$USER</span></span> |
4.1.3. Log out and log in again
4.1.4. Verify that we don’t need sudo anymore.
You can issue below command to check:
1 |
docker run hello-world |
The output should be the same as step 2.3
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[vagrant@localhost ~]$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. ... |
4.2. Configure Docker to start on boot
You can do this by using below command:
1 |
sudo systemctl enable docker |
or
1 |
sudo chkconfig docker on |
5. Uninstall Docker
For any reason that you want to uninstall Docker on CentOS 7, you can do as following.
5.1. Uninstall the Docker package
1 |
sudo yum -y remove docker-engine |
5.2. Remove all images, containers, volumes, or customized configuration files on your host
1 |
sudo rm -rf /var/lib/docker |
6. Conclusion
Above are simple steps to install Docker on CentOS 7 by using Docker repository or using RPM package. With the first approach, it’s is easier for us to upgrade or downgrade Docker while with the second approach, we must download and upgrade or downgrade manually by ourselves. Below are other articles related to Docker. If you’re interested in, you can refer to the following links:
Copy Files, Folders from Host to Docker Container and Vice Versa
How to Get IP Address of a Docker Container
How To Pull A Docker Image And Run A Container
Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step
Introduction to Docker Compose
Vagrant Docker Provider Tutorial