How to Install Docker on Centos 7 and Use It

How to Install and Setup Docker on Centos 7

In this tutorial, we will look at how we can install Docker on CentOS 7 and explore how we can run containers in the Docker environment.

Docker is a containerization technology that makes it simple and easy to build and run applications in containers. Docker containers run your applications in a resource-isolated process, using Docker we can build, test and deploy applications that run anywhere as portable and self-sufficient containers.

For a detailed introduction about what is Docker and how they are different from Virtual Machines, check out our post on What is Docker – A Brief Introduction.

Install Docker on CentOS – Prerequisites

Before installing Docker on CentOS 7, make sure your system meets the following requirements.

  • 64-bit based CentOS 7 machine
  • You should also have a non-root user with sudo privileges

How to Install Docker on Centos 7

You can find the Docker package in the official CentOS 7 repository, but it may not be always the latest version of Docker. I recommend installing Docker from the Docker’s repository.

Follow the steps below to install the latest release of Docker on CentOS 7.

Firstly, we will update the system packages and install the required dependencies using the following commands.

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

Next, we will run the below command which will add the Docker stable repository to your machine.

sudo yum-config-manager --add-repo

Now the Docker repository is available on your machine, install the latest version of Docker Community Edition using the following commands.

sudo yum install docker-ce

Once the Docker package is installed, start the Docker daemon and enable it to start at boot time.

sudo systemctl start docker
sudo systemctl enable docker

To verify that the Docker service is running properly on your machine, execute the following command.

sudo systemctl status docker

You should receive an output something like below.

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-12-23 20:51:20 UTC; 7s ago
 Main PID: 2492 (dockerd)
   CGroup: /system.slice/docker.service
           ├─2492 /usr/bin/dockerd
           └─2498 docker-containerd --config /var/run/docker/containerd/containerd.toml

To check the installed stable version of Docker, run the below command in your terminal.

docker -v

The output will be something like below.

Docker version 19.03.1, build 74b1e89e8a

For more detailed information, run the below command.

docker info

Executing the Docker Command Without Sudo

By default, Docker requires administrator privileges. If you want to execute the Docker commands as a non-root user (without using sudo) you need to add the user to the Docker group which is created during the installation of the Docker CE package.

Run the below command to add the user to the Docker group.

sudo usermod -aG docker $USER

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

Working with Docker Images

Docker containers are built from Docker images, a Docker image is like a recipe file that contains the layers representing instructions in the image’s Dockerfile which make up a software application.

An image is an immutable binary file including the application and all other dependencies such as binaries, libraries, and instructions to run the application.

All Docker images are hosted on the Docker Hub, which is a cloud-based public registry. You can also create your own images and host them on your own private repository.

To search the Docker Hub repository for an image, you can use the docker’s search command. For example, to search for Nginx images, run the below command.

docker search nginx

You will get the output something like below.

NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        12370               [OK]                
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1703                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   749                                     [OK]
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   83                                      
bitnami/nginx                     Bitnami nginx Docker Image                      73                                      [OK]
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   60                                      [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   35                                      [OK]
nginx/unit                        NGINX Unit is a dynamic web and application …   33                                      
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   32                                      
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        30                                      [OK]
nginx/nginx-ingress               NGINX Ingress Controller for Kubernetes         22                                      
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   19                                      [OK]
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   17                                      [OK]
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   12                                      
blacklabelops/nginx               Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  11                                      
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   11                                      
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter                       9                                       
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   6                                       [OK]
mailu/nginx                       Mailu nginx frontend                            5                                       [OK]
1science/nginx                    Nginx Docker images that include Consul Temp…   5                                       [OK]
pebbletech/nginx-proxy            nginx-proxy sets up a container running ngin…   2                                       [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          1                                       [OK]
centos/nginx-110-centos7          Platform for running nginx 1.10 or building …   0                                       
wodby/nginx                       Generic nginx                                   0                                       [OK]

You can see the search results prints a table with five columns, Name, DESCRIPTION, STARS, OFFICIAL and AUTOMATED.

If you want to download the official latest build of Ubuntu, you can do that by the image pull command.

docker pull ubuntu

The output will be:

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
Digest: sha256:b88f8848e9a1a4e4558ba7cfc4acc5879e1d0e7ac06401409062ad2627e6fb58
Status: Downloaded newer image for ubuntu:latest

If you want to list all images available on your machine, you can list them using the below command.

docker image ls

To remove an image from your machine, use the docker image rm command.

docker image rm ubuntu

Running a Docker Container

An executable instance of an image is called a container. A container represents a runtime for a single application.

Starting a Docker Container

To run/start a container, we can use the below command:

docker container run ubuntu

Note that ubuntu in our above command is the image name we pulled from the Docker repository. If the container image is not available on your machine, Docker will first download the image and then run it.

To interact with the Ubuntu container after booting up you have to use the argument -it like below:

docker container run -it ubuntu /bin/bash

List Docker Containers

To list all active containers, run the below command in the terminal. It will list all the active containers.

docker container ls

Removing a Docker Container

If you want to remove a Docker container, use the below command.

docker container rm 2ca790c1c0cc

The 2ca790c1c0cc is the container ID in above command. You can use only a few characters from the container ID to target it.

Final Words

In this post, you have learned how to install Docker on the CentOS system along with how to pull a Docker image and run a container out of it.

In the coming posts, we will dive deep into each concept of the Docker ecosystem. If you have any questions or suggestions to improve this post, please leave a comment below.

2 comments on “How to Install Docker on Centos 7 and Use It

Leave a Reply

Your email address will not be published. Required fields are marked *