Steps to Install Docker on CentOS 7

In this Docker tutorials, we’ll explain the steps to Install Docker on CentOS 7 or Amazon Linux. same steps can be followed to install docker on Amazon Linux.

Steps to Install Docker on CentOS 7

For this article, we will take the example of CentOS 7 64-bit Operating System for installing Docker-engine Distribution and type don’t matter when you doing on kernel 3.1.0 or better/above. So lets start with the steps to install Docker on CentOS 7.

  • Docker-engine is available on CentOS7 repository but it is an older version
  • Create a yum repository, so that you can install docker with yum command

Steps to Install Docker on CentOS 7

Step 1: Need to Uninstall Old Docker Versions (If already installed) 

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
Sample Output:
[root@CentOS1 ~]# sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
[root@CentOS1 ~]#

It’s OK if yum reports that none of these packages are installed.

Step 2: Install YUM Utils in CentOS

$ sudo yum install -y yum-utils

Sample Output:

[root@CentOS1 ~]# yum install -y yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.vanehost.com
 * extras: mirror.vanehost.com
 * updates: mirror.vanehost.com
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-50.el7 will be updated
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================================================
 Package                       Arch                       Version                             Repository                   Size
================================================================================================================================
Updating:
 yum-utils                     noarch                     1.1.31-54.el7_8                     updates                     122 k
Transaction Summary
================================================================================================================================
Upgrade  1 Package
Total download size: 122 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/updates/packages/yum-utils-1.1.31-54.el7_8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for yum-utils-1.1.31-54.el7_8.noarch.rpm is not installed
yum-utils-1.1.31-54.el7_8.noarch.rpm                                                                     | 122 kB  00:00:01
...
...
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : yum-utils-1.1.31-54.el7_8.noarch                                                                             1/2
  Cleanup    : yum-utils-1.1.31-50.el7.noarch                                                                               2/2
  Verifying  : yum-utils-1.1.31-54.el7_8.noarch                                                                             1/2
  Verifying  : yum-utils-1.1.31-50.el7.noarch                                                                               2/2
Updated:
  yum-utils.noarch 0:1.1.31-54.el7_8
Complete!
[root@CentOS1 ~]#

Step 3: Create Docker YUM Repository

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

Sample Output

[root@CentOS1 ~]# sudo yum-config-manager \
>     --add-repo \
>     https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@CentOS1 ~]#

Verify Repository

[root@CentOS1 ~]# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://download.docker.com/linux/centos/$releasever/source/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://download.docker.com/linux/centos/$releasever/source/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://download.docker.com/linux/centos/$releasever/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[root@CentOS1 ~]#

Step 4: Clean YUM Cache and Update all Package

#This cleans up any cached packages in any enabled repository cache directory which are normally stored in “/var/cache/yum” 

$ yum clean all
$ yum repolist

#Update all package which is already installed

$ yum update

Step 5: Command to Install Docker in CentOS 7

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

Sample Output

[root@CentOS1 ~]# sudo yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.vanehost.com
 * extras: mirror.vanehost.com
 * updates: mirror.vanehost.com
docker-ce-stable                                                                                         | 3.5 kB  00:00:00
(1/2): docker-ce-stable/7/x86_64/updateinfo                                                              |   55 B  00:00:00
(2/2): docker-ce-stable/7/x86_64/primary_db                                                              |  46 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.3.7-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.3.7-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:19.03.13-3.el7 will be installed
---> Package docker-ce-cli.x86_64 1:19.03.13-3.el7 will be installed
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================================================
 Package                        Arch                Version                                 Repository                     Size
================================================================================================================================
Installing:
 containerd.io                  x86_64              1.3.7-3.1.el7                           docker-ce-stable               29 M
 docker-ce                      x86_64              3:19.03.13-3.el7                        docker-ce-stable               24 M
 docker-ce-cli                  x86_64              1:19.03.13-3.el7                        docker-ce-stable               38 M
Installing for dependencies:
 container-selinux              noarch              2:2.119.2-1.911c772.el7_8               extras                         40 k
Transaction Summary
================================================================================================================================
Install  3 Packages (+1 Dependent package)
Total download size: 91 M
Installed size: 385 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                                              |  40 kB  00:00:01
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.13-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.13-3.el7.x86_64.rpm is not installed
(2/4): docker-ce-19.03.13-3.el7.x86_64.rpm                                                               |  24 MB  00:00:04
(3/4): containerd.io-1.3.7-3.1.el7.x86_64.rpm                                                            |  29 MB  00:00:22
(4/4): docker-ce-cli-19.03.13-3.el7.x86_64.rpm                                                           |  38 MB  00:00:24
--------------------------------------------------------------------------------------------------------------------------------
Total                                                                                           3.2 MB/s |  91 MB  00:00:28
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                           1/4
  Installing : containerd.io-1.3.7-3.1.el7.x86_64                                                                           2/4
  Installing : 1:docker-ce-cli-19.03.13-3.el7.x86_64                                                                        3/4
  Installing : 3:docker-ce-19.03.13-3.el7.x86_64                                                                            4/4
  Verifying  : containerd.io-1.3.7-3.1.el7.x86_64                                                                           1/4
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                           2/4
  Verifying  : 1:docker-ce-cli-19.03.13-3.el7.x86_64                                                                        3/4
  Verifying  : 3:docker-ce-19.03.13-3.el7.x86_64                                                                            4/4
Installed:
  containerd.io.x86_64 0:1.3.7-3.1.el7       docker-ce.x86_64 3:19.03.13-3.el7       docker-ce-cli.x86_64 1:19.03.13-3.el7
Dependency Installed:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8
Complete!
[root@CentOS1 ~]#

Docker-engine will install with appropriate SE-Linux and security settings that are necessary for containers to work correctly in your environment.

$ sudo systemctl enable docker

This will create the link necessary for us to start our service

Sample output

[root@CentOS1 ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@CentOS1 ~]#

Step 6: Docker command to Start|Stop|Status

$ sudo systemctl start docker
$ sudo systemctl status docker
$ sudo systemctl stop docker

Now you can see docker demon will be running

Step 7: Command to check Docker version in Linux

$ docker --version

Sample output

[root@CentOS1 ~]# docker --version
Docker version 19.03.13, build 4484c46d9d
[root@CentOS1 ~]#

Step 8: Verify Docker Engine properly installed or not

$ sudo docker run hello-world

Hello-world image will be downloaded and run in container and it will print hello world message and exit.

Sample output

[root@CentOS1 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Status: Downloaded newer image for hello-world:latest

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.
(amd64)
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.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

[root@CentOS1 ~]#

Step 9: Command to Check/List Docker Images

$ docker images
As per best practice, it is recommended to run docker service with non-root user as per security perspective

How to configure and run docker with the Normal User or non-root user?

User1$ docker images #--here you may get permission denied issue as you are trying to list docker images
User1$ su –

root# cd /var/run/
root# ls –al docker*

you will see docker.pid file and docker.sock file.

Docker.sock file is the one we are able to connect to as a user, the docker.sock file which is own by user “root” and with group “docker”.

$ cat /etc/group | grep docker
Docker:X:1022:

Here you can see the docker group but there is no user associated with it.

$usermod –a –G docker user1
$cat /etc/group | grep docker
Docker:x:1022:user1
$exit

Now, re-login to the server and run the same command:

$ docker images

Now, as a user1 (normal user) can connect to the Docker daemon

Script to Install Docker on Amazon Linux

#!/bin/bash

echo "Updating your OS to latest OS..."
sudo yum update -y

echo "Installing docker package..."
sudo yum install -y docker

echo "starting Docker service.."
sudo service docker start

echo "Adding/providing ec2-user to docker group"
sudo usermod -a -G docker ec2-user

echo "Relogin to server so that ec2-user will update group"

echo "Install git package it is required in docker"
sudo yum install -y git

echo "Install all development tools in linux"
sudo yum -y groupinstall "Development Tools"

It is recommended to update your OS on the ec2 box, So depending on Amazon Linux/ RHEL / Centos version as the yum packages are taken from amazon’s repository. So you must consider updating your OS to the latest.

Refer to Docker version metrics

This is the end of Docker Tutorials, where you learn all the steps to install Docker on CentOS 7 or Amazon Linux and best practice.


Thanks for reading this article, you’ll also like to read below articles. If you have any quires comment us below.

See Also:

Docker Commands Tutorial for Beginners with Example

Steps to Install Docker on Ubuntu

Docker Blogs list for Beginners

Google Cloud Blogs

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs