HowToProgram

Install Docker on CentOS 7

This tutorial is going to cover how to install Docker on CentOS 7. There are several approaches for us:

We are going to get through both approaches and then additional Docker configurations.

1. Prerequisites

You can do a quick check by issuing the following command:

uname -r

CentOS 7 Kernel Version

My CentOS 7 kernel version is 3.10.0. It’s good to get started.

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:

sudo yum install -y yum-utils

Then, set up the stable repository by using the following command:

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

sudo yum makecache fast

2.2.2. Install the latest version of Docker or go to next step to install a specific version

sudo yum -y install docker-engine

2.2.3. Install a specific version of Docker

Step 1. List all available versions of Docker
yum list docker-engine.x86_64  --showduplicates |sort -r

The output will be similar to below:

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 (-):

sudo yum -y install docker-engine-<VERSION_STRING>

For example, let’s install the Docker version 1.12.6-1.el7.centos

sudo yum -y install docker-engine-1.12.5-1.el7.centos

2.3. Start the Docker

sudo systemctl start docker

2.4. Verify the installation

Verify that docker is installed correctly by running the hello-world image

sudo docker run hello-world

You should see the result that is similar as below image:

Running hello-world Docker

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

 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:

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

sudo groupadd docker

4.1.2. Add our desire user to that group.

sudo usermod -aG docker $USER

We have just added the current user to the group.

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:

docker run hello-world

The output should be the same as step 2.3

[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:

sudo systemctl enable docker

or

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

 sudo yum -y remove docker-engine

5.2. Remove all images, containers, volumes, or customized configuration files on your host

sudo rm -rf /var/lib/docker

We must delete any edited configuration files manually.

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

Basic Docker commands

Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

Insecure docker registry