Read in 6 minutes

last updated 

How To Install and Use Docker on CentOS 7

Docker is a containerization technology that allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can run virtually everywhere.

In this tutorial we’ll go through how to install Docker CE on a CentOS 7 and explore the basic Docker concepts and commands.


Before proceeding with this tutorial, make sure that the following prerequisites are met:

  • CentOS 7 server
  • You have non-root user with sudo privileges. You check this guide about how create a new sudo user.

Install Docker

Although the Docker package is available in the official CentOS 7 repository it may not always be the latest version. The recommended approach is to install Docker from the Docker’s repositories.

Install the required dependencies:

sudo yum install yum-utils device-mapper-persistent-data lvm2

and add the stable Docker repository by typing:

sudo yum-config-manager --add-repo

Now that we have Docker repository enabled, we can install the latest version of Docker CE (Community Edition) using yum by typing:

sudo yum install docker-ce

Once the Docker package is installed, we start the Docker daemon with:

sudo systemctl start docker

To verify that the Docker service is running type:

sudo systemctl status docker

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-05-08 09:53:33 UTC; 5s ago
 Main PID: 2327 (dockerd)
    Tasks: 16
   Memory: 36.4M
   CGroup: /system.slice/docker.service
           ├─2327 /usr/bin/dockerd
           └─2331 docker-containerd --config /var/run/docker/containerd/containerd.toml

Enable the Docker service to be automatically started at boot time:

sudo systemctl enable docker

At the time of the writing of this article, the current stable version of Docker is 18.03.1, we can check our Docker version by typing:

docker -v
Docker version 18.03.1-ce, build 9ee9f40

Executing the Docker Command Without Sudo

By default managing Docker requires administrator privileges. If you want to run Docker commands as a non-root user without prepending sudo you need to add your user to the docker group which is created during the installation of the Docker CE package. You can do that by typing:

sudo usermod -aG docker $USER

Log out and log back in so that the group membership is refreshed.

To verify Docker is installed successfully and that you can run docker commands without sudo run the following command which will download a test image, run it in a container, print a “Hello from Docker” message and exit:

docker container run hello-world

The output should look like the following:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Docker command line interface

Now that we have Docker installed, let’s go over the basic syntax of the docker CLI:

docker [option] [subcommand] [arguments]

To list all available commands run docker with no parameters:


If you need more help on any [subcommand], just type:

docker [subcommand] --help

Docker Images

A Docker image is made up of a series of layers representing instructions in the image’s Dockerfile that make up an executable software application. An image is an immutable binary file including the application and all other dependences such as binaries, libraries and instructions necessary for running the application. In short a Docker image is essentially a snapshot of a Docker container.

The Docker Hub is cloud-based registry service which among other functionalities is used for keeping the Docker images either in a public or private repository.

To search the Docker Hub repository for an image just use the search subcommand. For example, to search for the CentOS image, run:

docker search centos

The output should look like the following:

NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4257                [OK]
ansible/centos7-ansible            Ansible on Centos7                              109                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_…   94                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   52                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              40                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      39

As you can see the search results prints a table with five columns, NAME, DESCRIPTION, STARS, OFFICIAL and AUTOMATED. Official image is an image that Docker develops in conjunction with upstream partners.

If we want to download the official build of CentOS 7, we can do that by using the image pull subcommand:

docker image pull centos
Using default tag: latest
latest: Pulling from library/centos
469cfcc7a4b3: Pull complete
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Downloaded newer image for centos:latest

Depending on your Internet speed, the download may take a few seconds or a few minutes. Once the image is downloaded we can list the images with:

docker image ls

The output should look something like the following:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              e38bc07ac18e        3 weeks ago         1.85kB
centos              latest              e934aafc2206        4 weeks ago         199MB

If for some reason you want to delete an image you can do that with the image rm [image_name] subcommand:

docker image rm centos
Untagged: centos:latest
Untagged: [email protected]:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Deleted: sha256:e934aafc22064b7322c0250f1e32e5ce93b2d19b356f4537f5864bd102e8531f
Deleted: sha256:43e653f84b79ba52711b0f726ff5a7fd1162ae9df4be76ca1de8370b8bbf9bb0

Docker Containers

An instance of an image is called a container. A container represents a runtime for a single application, process, or service.

It may not be the most appropriate comparison but if you are a programmer you can think of a Docker image as class and Docker container as an instance of a class.

We can start, stop, remove and manage a container with the docker container subcommand.

The following command will start a Docker container based on the CentoOS image. If you don’t have the image locally, it will download it first:

docker container run centos

At first sight it may seem to you that nothing happened at all. Well, that is not true. The CentOS container stops immediately after booting up because it does not have a long-running process and we didn’t provide any command, so the container booted up, ran an empty command and then exited.

The switch -it allows us to interact with the container via the command line. To start an interactive container type:

docker container run -it centos /bin/bash

As you can see from the output once the container is started the command prompt is changed which means that you’re now working from inside the container:

To list active containers, type:

docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
79ab8e16d567        centos              "/bin/bash"         22 minutes ago      Up 22 minutes                           ecstatic_ardinghelli

If you don’t have any running containers the output will be empty.

To view both active and inactive containers, pass it the -a switch:

docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
79ab8e16d567        centos              "/bin/bash"              22 minutes ago      Up 22 minutes                                   ecstatic_ardinghelli
c55680af670c        centos              "/bin/bash"              30 minutes ago      Exited (0) 30 minutes ago                       modest_hawking
c6a147d1bc8a        hello-world         "/hello"                 20 hours ago        Exited (0) 20 hours ago                         sleepy_shannon

To delete a container multiple containers just copy the container ID (or IDs) from above and paste them after the container rm subcommand:

docker container rm c55680af670c


You have learned how to install Docker on your CentOS 7 machine and how to download Docker images and manage Docker containers. This tutorial barely scratches the surface of the Docker ecosystem. In some of our next articles, we will continue to dive into other aspects of Docker.

You should also check out the official Docker documentation.

If you have any question or remark, please leave a comment below.