Sunday, September 13, 2020

Install Docker on CentOS

Docker (container engine)


What is Docker?

Docker Engine is an open-source containerization technology for building and containerizing your applications. More details on docker here.

Let's get familiarized with few terminologies before we get started with installing docker.

  1. Layer:  A layer represents an instruction in the image’s Dockerfile. Each layer except the very last one is read-only.
  2. Image: A Docker image is built up from a series of layers. A read-only layer that is the base of your container. An image may have a parent image.
  3. Container: A runnable instance of the image that includes everything needed to run an application. Each container is an image readable/writable layers on top of a set of read-only layer
  4. Docker Hub: Central place where images live. It's a service provided by docker to create, manage, and distribute container applications.
  5. Docker machine: A tool to run Docker containers (Linux does this natively).
  6. Docker compose: A tool for defining and running multi-container docker applications.

Install Docker Engine on CentOS

The docker installation for CentOS 7 and 8 is similar. Although, we will use CentOS 8 to show the installation steps here.

Prerequisites to install Docker

To check the Linux distro you have use the following command.
[user@abhishekumrao ~]$ cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
If you are not running on a CentOS v7 box, ensure that your kernel is running version 3.10 or better by running the uname -r command:
[user@abhishekumrao ~]$ uname -r
3.10.0-862.9.1.el7.x86_64

Install the required packages.

  • yum-utils package provides the yum-config-manger utility used to add docker repo.
  • devicemapper requires the lvm2 and device-mapper-persistent-data packages to be installed.
  • Device Mapper is a kernel-based framework that underpins many advanced volume management technologies on Linux.
[user@abhishekumrao ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
[sudo] password for user:
Last metadata expiration check: 0:04:41 ago on Sun 13 Sep 2020 07:37:49 AM UTC. Package yum-utils-4.0.12-3.el8.noarch is already installed. Package device-mapper-persistent-data-0.8.5-3.el8.x86_64 is already installed. Package lvm2-8:2.03.08-3.el8.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete!

Setting up the Repository

Add the docker repository to your server to ensure that you have the latest version using the following command.

[user@abhishekumrao ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[sudo] password for user:
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo

Install Docker community edition.

Install docker with the yum install command:

[user@abhishekumrao ~]$ sudo yum install docker-ce

This goes fine in CentOS 7, but in CentOS 8 you may get an error like this

The latest docker engine at the time of writing this guide is 3:19.03.12-3.el7.x86_64 which requires containerd.io >= 1.2.2-3. Since this package is not available we would use --nobest flag to install the best available package of docker which doesn't require containerd.io >= 1.2.2-3. See below terminal output

Now enable docker service
[user@abhishekumrao ~]$ sudo systemctl enable docker
[sudo] password for user:
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

Start Docker with the systemctl start command:

[user@abhishekumrao ~]$ sudo systemctl start docker

You can verify the service status by using the following command

[user@abhishekumrao ~]$ systemctl status docker
[sudo] password for user:
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-09-13 10:49:40 UTC; 35s ago
     Docs: https://docs.docker.com
 Main PID: 39526 (dockerd)
    Tasks: 22
   Memory: 49.1M
   CGroup: /system.slice/docker.service
           ├─39526 /usr/bin/dockerd -H fd://
           └─39536 containerd --config /var/run/docker/containerd/containerd.toml --log-level info


We can now verify the docker installation by running a small container hello-world using the docker run command:

[user@abhishekumrao ~]$ sudo docker run hello-world
[sudo] password for user:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
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/

For best practices, we should avoid using root. Instead, you can add your user to the Docker group. For example, to user abhishek use the below command:

[user@abhishekumrao ~]$ sudo usermod -a -G docker abhishek

[sudo] password for user:

[user@abhishekumrao ~]$

Confirm the change with the grep command:

[user@abhishekumrao ~]$ cat /etc/group | grep docker
docker:x:973:abhishek

Once finished, log out and then log back in for changes to take effect.

No comments:

Post a Comment

Featured Post

Install Docker on CentOS

What is Docker? Docker Engine is an open-source containerization technology for building and containerizing your applications. More details ...

Most viewd