How to Install Docker in Linux

Dear Friends, in this tutorial we will see how to install Docker and manage container in CentOS 7. We will also see how to stop, start and restart container with Docker commands. Before installation and configuration let’s try to know what is Docker? Why we need Docker?

What is Docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly using your operating system.

Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, we can quickly deploy and scale applications into any environment and our code will be run on that application.

For more details about Docker you can Click-Here

Why we need Docker?

  1. Isolation: – Dependencies or settings within a container will not affect any installations or configurations on our computer, or on any other containers that may be running in our computer.
  2. Docker Hub: – Docker provide the lots of simple use cases push and pull to Docker Hub.
  3. Security: – In the security purpose, it also separating the different components of a large application into different containers, if our one container is compromised then others remain unaffected.

What is container?

A container is a standard unit of software that packages up code and all its dependencies, so that application runs quickly and reliably from one computing environment to another.

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

How to install docker?

Step 1: – Set repository to install docker

Step 2: – Install docker package in Centos7

Step 3: – Search docker image.

Step 4: – Download docker image

Step 5: – Launch container with downloaded image

Step 6: – Check the container running state

Let’s see step by step these process.

Step 1:- Set repository to install Docker

We need to set repository to download docker package. We can set repository to run below commands.

[root@urclouds ~]# 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@urclouds ~]#

Some support packages also we need to be installed like LVM2, device-mapper-persistent-data and yum-utils. We can install these packages with yum tool like below: –

[root@urclouds ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

Step 2: – Install docker package in Centos7

Now we can install docker software with yum tool after repository setting like below: –

How to install docker
[root@urclouds ~]# yum install docker-ce
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base: centos.crazyfrogs.org
extras: centos.quelquesmots.fr
updates: centos.crazyfrogs.org
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
(2/2): docker-ce-stable/x86_64/primary_db | 34 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:19.03.2-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-19.03.2-3.el7.x86_64
--> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-19.03.2-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-19.03.2-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.107-3.el7 will be installed
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
---> Package docker-ce-cli.x86_64 1:19.03.2-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Package Arch Version Repository Size
Installing:
docker-ce x86_64 3:19.03.2-3.el7 docker-ce-stable 24 M
Installing for dependencies:
container-selinux noarch 2:2.107-3.el7 extras 39 k
containerd.io x86_64 1.2.6-3.3.el7 docker-ce-stable 26 M
docker-ce-cli x86_64 1:19.03.2-3.el7 docker-ce-stable 39 M
Transaction Summary
Install 1 Package (+3 Dependent packages)
Total download size: 90 M
Installed size: 368 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): container-selinux-2.107-3.el7.noarch.rpm | 39 kB 00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.2-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.2-3.el7.x86_64.rpm is not installed
(2/4): docker-ce-19.03.2-3.el7.x86_64.rpm | 24 MB 00:00:06
(3/4): containerd.io-1.2.6-3.3.el7.x86_64.rpm | 26 MB 00:00:07
(4/4): docker-ce-cli-19.03.2-3.el7.x86_64.rpm | 39 MB 00:00:10
Total 5.4 MB/s | 90 MB 00:00:16
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.107-3.el7.noarch 1/4
Installing : containerd.io-1.2.6-3.3.el7.x86_64 2/4
Installing : 1:docker-ce-cli-19.03.2-3.el7.x86_64 3/4
Installing : 3:docker-ce-19.03.2-3.el7.x86_64 4/4
Verifying : 3:docker-ce-19.03.2-3.el7.x86_64 1/4
Verifying : 2:container-selinux-2.107-3.el7.noarch 2/4
Verifying : containerd.io-1.2.6-3.3.el7.x86_64 3/4
Verifying : 1:docker-ce-cli-19.03.2-3.el7.x86_64 4/4
Installed:
docker-ce.x86_64 3:19.03.2-3.el7
Dependency Installed:
container-selinux.noarch 2:2.107-3.el7 containerd.io.x86_64 0:1.2.6-3.3.el7 docker-ce-cli.x86_64 1:19.03.2-3.el7
Complete!
[root@urclouds ~]#

After successfully docker installation we can start docker service. Like below: –

[root@urclouds ~]# systemctl start docker
[root@urclouds ~]#

We can also enable docker service to start at boot time so that docker service will automatically start at the boot time.

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

You can verify your running docker service with systemctl status docker commands. You can see in below our docker service is running.

[root@urclouds ~]# systemctl start docker
[root@urclouds ~]#
=========
status picture
[root@urclouds ~]# systemctl status docker
? docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2019-09-21 12:47:28 CEST; 1min 3s ago
Docs: http://docs.docker.com
Main PID: 9033 (dockerd-current)
CGroup: /system.slice/docker.service
+-9033 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime…
+-9043 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-…
Sep 21 12:47:26 urclouds dockerd-current[9033]: time="2019-09-21T12:47:26.228639562+02:00" level=info msg="libcontainerd… 9043"
Sep 21 12:47:27 urclouds dockerd-current[9033]: time="2019-09-21T12:47:27.457578165+02:00" level=info msg="Graph migrati…conds"
Sep 21 12:47:27 urclouds dockerd-current[9033]: time="2019-09-21T12:47:27.459811695+02:00" level=info msg="Loading conta…tart."
Sep 21 12:47:27 urclouds dockerd-current[9033]: time="2019-09-21T12:47:27.530524524+02:00" level=info msg="Firewalld run… true"
Sep 21 12:47:27 urclouds dockerd-current[9033]: time="2019-09-21T12:47:27.948885999+02:00" level=info msg="Default bridg…dress"
Sep 21 12:47:28 urclouds dockerd-current[9033]: time="2019-09-21T12:47:28.301539431+02:00" level=info msg="Loading conta…done."
Sep 21 12:47:28 urclouds dockerd-current[9033]: time="2019-09-21T12:47:28.346778782+02:00" level=info msg="Daemon has co…ation"
Sep 21 12:47:28 urclouds dockerd-current[9033]: time="2019-09-21T12:47:28.346859825+02:00" level=info msg="Docker daemon…1.13.1
Sep 21 12:47:28 urclouds dockerd-current[9033]: time="2019-09-21T12:47:28.364433311+02:00" level=info msg="API listen on….sock"
Sep 21 12:47:28 urclouds systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@urclouds ~]#

Docker information

We can check docker version and software details with docker info command.

[root@urclouds ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 9c3c5f853ebf0ffac0d087e94daef462133b69c7 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
WARNING: You're not using the default seccomp profile
Profile: /etc/docker/seccomp.json
selinux
Kernel Version: 3.10.0-1062.1.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 6
Total Memory: 5.669 GiB
Name: urclouds
ID: J5LE:5C4J:J4NB:Q55W:FMXT:QZBR:FPCU:NMQ5:RCIZ:FMY5:35JX:73GO
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
[root@urclouds ~]#

Step 3: – Search docker image.

Now we can search docker images with “docker search image-name” and we will get all available images from docker hub. As like below: –

[stack@undercloud (stackrc) ~]$ docker search ubuntu
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/ubuntu Ubuntu is a Debian-based Linux operating s… 9967 [OK]
docker.io docker.io/dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interfac… 345 [OK]
docker.io docker.io/rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of… 231 [OK]
docker.io docker.io/consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC sessi… 187 [OK]
docker.io docker.io/ubuntu-upstart Upstart is an event-based replacement for … 99 [OK]
docker.io docker.io/ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 97 [OK]
docker.io docker.io/neurodebian NeuroDebian provides neuroscience research… 59 [OK]
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
docker.io docker.io/ubuntu-debootstrap debootstrap --variant=minbase --components… 40 [OK]
docker.io docker.io/i386/ubuntu Ubuntu is a Debian-based Linux operating s… 18
docker.io docker.io/1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]
docker.io docker.io/1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]
docker.io docker.io/ppc64le/ubuntu Ubuntu is a Debian-based Linux operating s… 13
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK]
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK]
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]
docker.io docker.io/1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK]
docker.io docker.io/darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK]
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK]
docker.io docker.io/1and1internet/ubuntu-16-apache-php-5.6-zend-2 ubuntu-16-apache-php-5.6-zend-2 2 [OK]
docker.io docker.io/pivotaldata/ubuntu A quick freshening-up of the base Ubuntu d… 2
docker.io docker.io/1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK]
docker.io docker.io/1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
docker.io docker.io/smartentry/ubuntu ubuntu with smartentry 1 [OK]
docker.io docker.io/pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
[stack@undercloud (stackrc) ~]$

Step 4: – Download docker image

We can download images from docker hub with the “docker pull image-name” to our local repository. Here I am going to pull Ubuntu image. You can see in below Ubuntu image successfully downloaded from docker hub.

[root@urclouds ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu …
latest: Pulling from docker.io/library/ubuntu
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
Digest: sha256:b88f8848e9a1a4e4558ba7cfc4acc5879e1d0e7ac06401409062ad2627e6fb58
Status: Downloaded newer image for docker.io/ubuntu:latest
[root@urclouds ~]#

Once image successfully downloaded then we can verify our downloaded image with “docker images” commands like below: –

[root@urclouds ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 2ca708c1c9cc 3 days ago 64.2 MB
[root@urclouds ~]#

Step 5: – Launch container with downloaded image

Now I am going to launch container with downloaded image Ubuntu. We can use “docker run -i -t ubuntu /bin/bash” command to launch a new container.

[root@urclouds ~]# docker run -i -t ubuntu /bin/bash
root@20fbe0356efc:/#

You can see in above screen shot we have successfully launched Ubuntu container.

We can use CTRL + P + Q to exit current container app like below: –

root@20fbe0356efc:/#
root@20fbe0356efc:/# [root@urclouds ~]#
[root@urclouds ~]#

Step 6: – Check the container running state

Once our container is successfully launched then we can check running list of our container with “docker ps” commands.

[root@urclouds ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 5 minutes ago Up 4 minutes frosty_nobel
[root@urclouds ~]#

If we want to display all our container list which is also currently not in running state, then we can use this below command to display all containers.

[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 6 minutes ago Up 6 minutes frosty_nobel
[root@urclouds ~]#

We can check running status of our container with “docker stats container-ID” commands.

[root@urclouds ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 8 minutes ago Up 8 minutes frosty_nobel
[root@urclouds ~]# docker stats 20fbe0356efc
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
20fbe0356efc 0.00% 416 KiB / 96.95 GiB 0.00% 1.3 kB / 648 B 0 B / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
20fbe0356efc 0.00% 416 KiB / 96.95 GiB 0.00% 1.3 kB / 648 B 0 B / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
^C
[root@urclouds ~]#

How to manage container?

Step 1 : – Stop the container

If we want to stop our running container we can use “docker stop container-ID” commands to stop the container.

[root@urclouds ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 11 minutes ago Up 11 minutes frosty_nobel
[root@urclouds ~]# docker stop 20fbe0356efc
20fbe0356efc
[root@urclouds ~]#
[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 12 minutes ago Exited (0) About a minute ago frosty_nobel
[root@urclouds ~]#

You can see in above output we have successfully stop our running container Ubuntu.

Step 2 : – Start the container

Now I am going to start our down Ubuntu container with “docker start container-ID” commands. You can see in below screen shot our Ubuntu container successfully started.

[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 12 minutes ago Exited (0) About a minute ago frosty_nobel
[root@urclouds ~]# docker start 20fbe0356efc
20fbe0356efc
[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 15 minutes ago Up 4 seconds frosty_nobel
[root@urclouds ~]#

We can also restart our running container using “docker restart container-ID” command.

[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 2 days ago Up 2 days frosty_nobel
[root@urclouds ~]# docker restart 20fbe0356efc
20fbe0356efc
[root@urclouds ~]#

Step 3 : – Enter into the container

If you want to go to your container and run some commands from container side, then you can use “docker exec -it container-id /bin/bash” commands to enter your container.

[root@urclouds ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 46 hours ago Up 46 hours frosty_nobel
[root@urclouds ~]# docker exec -it 20fbe0356efc /bin/bash
root@20fbe0356efc:/#

You can see in above output we have successfully entered in our Ubuntu container. So now we can use our Ubuntu as operating system. We can check all logs in our Ubuntu server as like below: –

root@20fbe0356efc:~# cd /var/log/
root@20fbe0356efc:/var/log# ls -ltr
total 268
-rw-rw-r--. 1 root utmp 0 Sep 12 20:53 wtmp
-rw-rw----. 1 root utmp 0 Sep 12 20:53 btmp
-rw-------. 1 root root 6464 Sep 12 20:53 tallylog
-rw-rw-r--. 1 root utmp 29492 Sep 12 20:53 lastlog
-rw-r--r--. 1 root root 3232 Sep 12 20:53 faillog
-rw-r--r--. 1 root root 35330 Sep 12 20:53 bootstrap.log
-rw-r--r--. 1 root root 185152 Sep 12 20:54 dpkg.log
-rw-r--r--. 1 root root 3788 Sep 12 20:54 alternatives.log
drwxr-xr-x. 2 root root 60 Sep 12 20:54 apt
root@20fbe0356efc:/var/log#

Step 3 : – Exit from containers

To exit from our Ubuntu container, we can use CTRL+P+Q

root@20fbe0356efc:/#
root@20fbe0356efc:/# [root@urclouds ~]#
[root@urclouds ~]#

Step 3 : – Delete container

Now I am going to delete my running container with “docker rm container-id” command. Like below.

[root@urclouds ~]# docker stop 20fbe0356efc
20fbe0356efc
[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fbe0356efc ubuntu "/bin/bash" 2 days ago Exited (0) 4 seconds ago frosty_nobel
[root@urclouds ~]# docker rm 20fbe0356efc
20fbe0356efc
[root@urclouds ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@urclouds ~]#

That’s all, we have seen in this tutorial how to install docker and manage container in CentOS 7.

Docker vs Virtual Machine: what are the differences

This Post Has 2 Comments

  1. zortilo nrel

    Usually I do not read post on blogs, but I would like to say that this write-up very forced me to take a look at and do it! Your writing taste has been surprised me. Thanks, very nice post.

  2. gralion torile

    You really make it appear really easy along with your presentation however I to find this matter to be really one thing which I believe I might by no means understand. It kind of feels too complicated and extremely huge for me. I’m taking a look forward in your subsequent put up, I’ll try to get the grasp of it!

Leave a Reply