How to Install and Use Docker on CentOS 7

How to Install and Use Docker on CentOS 7
How to Install and Use Docker on CentOS 7

Install and Use Docker on CentOS 7

Docker is an application used to manage application processes in containers. Containers run applications in resource-isolated process. By using docker you can build, test and deploy applications that can run anywhere as portable and self-sufficient containers. In this tutorial, you are going to learn how to install and use Docker on CentOS 7.

Prerequisites

Before you start to install Docker on CentOS 7. You must have the root user account credentials of your system.

Install Docker on CentOS

Here we will install Docker on CentOS from Latest Docker’s Repository. Follow the steps below to install Docker on CentOS.

Update the system and install required dependencies typing following command:

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

Next, add the repository to your system typing following in the terminal:

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

Install Docker Community Version typing following command:

sudo yum install docker-ce

Start and Enable Docker service typing following systemctl commands:

sudo systemctl start docker
sudo systemctl enable docker

You can check the Docker service status using the following systemctlcommand:

sudo systemctl status docker

The output should be:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-13-19 20:22:00 PDT; 6min ago
     Docs: https://docs.docker.com
 Main PID: 10647 (dockerd)
    Tasks: 21
   CGroup: /system.slice/docker.service

Check the version of Docker using following command:

docker -v

The output should be:

Docker version 18.09.0-ce, build 9ee9f40

Executing the Docker Command Without Sudo

By default, docker command can only run by sudo user or member of docker group which created while installing Docker. If you want to run docker command without sudo then you should add the user to docker group.

If you are already logged in then to add the user to docker group run below command:

sudo usermod -aG docker $USER

How to Use Docker Command

Following is the basic syntax for docker command:

docker [option] [subcommand] [arguments]

List all the available subcommands typing following command:

docker

Get help for any docker subcommand using the following command:

docker docker-subcommand --help

Working with Docker Images

Docker containers are built from Docker Images and these images are pulled from Docker Hub which is a registry managed by Docker Company. On the Docker Hub, anyone can host their images, because of this most of the app images and Linux distro’s are already available on Docker Hub.

Search Docker Images

You can search for an image on Docker Hub by using docker command with search sub-command.

Search an CentOS 7 image typing following in command line:

docker search centos

The output should be similar to:

NAME                                                      DESCRIPTION                                     NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   2224      [OK]       
jdeathe/centos-ssh              CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8...   22                   [OK]
jdeathe/centos-ssh-apache-php   CentOS-6 6.7 x86_64 / Apache / PHP / PHP M...   17                   [OK]
million12/centos-supervisor     Base CentOS-7 with supervisord launcher, h...   11                   [OK]
nimmis/java-centos              This is docker images of CentOS 7 with dif...   10                   [OK]
torusware/speedus-centos        Always updated official CentOS docker imag...   8                    [OK]
nickistre/centos-lamp           LAMP on centos setup                            3                    [OK]

...

You can see all images comes with version number still if the version number is not specified at that time docker picks the latest version available.

Download Docker Images

You can download Docker image by using pull subcommand. To download CentOS image you can following command.

docker pull centos

The output should be:

Using default tag: latest
latest: Pulling from library/centos
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for centos:latest

List Downloaded Docker Images

You can list downloaded Docker images by using the following command:

docker images

Remove Docker Images

If you want to remove downloaded docker image then you can do so by using the following command. Below command will remove CentOS image from your system.

docker image rm centos

Working with Docker Containers

Docker container is an instance of Docker Image. You can also interact with the container. Containers are the resource-friendly virtual machines. By using docker container command we can manage operations on a container.

Start a Container

By using container run subcommand you can run a container which is an instance of the image. If the container image is not available then it will first download and then the container will be started.

To start the container of CentOS you should run below command:

docker container run centos

In the above command, we didn’t provide any command to execute after starting CentOS container so it will boot then executes the empty command and then exits.

To interact with CentOS container after booting up, use -it switch. To do so run below command:

docker container run -it centos /bin/bash

The output should be:

[[email protected] /]#

As you can see above the command prompt changed. Now you can execute any command and directly interact with CentOS container.

List Active Docker Containers

You can List all active Docker containers using below command. It will list all the active containers otherwise the output will be empty:

docker container ls

If you want to List all Active and InActive containers type:

docker container ls -a

Remove Docker Container

If you want to remove Docker image for some reason you can use the following command:

docker container rm 748fh3304412

In the above command 748fh3304412 is a Container ID.

Conclusion

You have successfully learned how to install and Use Docker on CentOS 7. If you have any queries please don’t forget to comment below.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here