How to install and setup Docker on RHEL 7/CentOS 7?

Docker is free and open-source software. It automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere. You can build a container with your app, and it can test run on your computer. It will scale in cloud, VM, VPS, bare-metal and more. There are two versions of docker. The first one bundled with RHEL/CentOS 7 distro and can be installed with the yum. The second version distributed by the Docker project called docker-ce (community free version) and can be installed by the official Docker project repo. The third version distributed by the Docker project called docker-ee (Enterprise paid version) and can be installed by the official Docker project repo. 


Install and use Docker on RHEL 7 or CentOS 7 (method 1)


The procedure to install Docker is as follows:

  1. Open the terminal application or log in to the remote box using ssh command:

    ssh user@remote-server-name
  2. Type the following command to install Docker via yum provided by Red Hat:

    sudo yum install docker
  3. Type the following command to install the latest version of Docker CE (community edition):

    sudo yum remove docker docker-common docker-selinux docker-engine
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install docker-ce

Install Docker on CentOS 7 / RHEL 7 using yum

Type the following yum command:


$ sudo yum install docker

Install Docker CE on CentOS 7 (method 2)

First, remove the older version of docker (if any):


$ sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce

Next, install needed packages:


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

Configure the docker-ce repo:


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



Finally, install docker-ce:

 

$ sudo yum install docker-ce

How to enable docker service

$ sudo systemctl enable docker.service


How to start/stop/restart docker service on CentOS7/RHEL7

Start docker

$ sudo systemctl start docker.service
 

Stop docker

$ sudo systemctl stop docker.service 



Restart docker

$ sudo systemctl restart docker.service  

 
Get status of docker

$ sudo systemctl status docker.service


How to find out info about Docker network bridge and IP addresses

Default network bridge named as docker0 and is assigned with an IP address. To find this info run the following ip command:


$ ip a
$ ip a list docker0

How to run docker commands

The syntax is:


docker command
docker command arg
docker [options] command arg
docker help | more


Get system-wide information about Docker

docker info


How to test your docker installation

Docker images are pulled from docker cloud/hub such as docker.io or registry.access.redhat.com and so on. Type the following command to verify that your installation working:


docker run hello-world

How to search for Docker images


Now you have working Docker setup. It is time to find out images. You can find images for all sort of open source projects and Linux distributions. To search the Docker Hub/cloud for nginx image run:


docker search nginx

How to install Docker nginx image

To pull an image named nginx from a registry, run:


docker pull nginx


How to run Docker nginx image

Now you pulled the image, it is time to run it:


docker run --name my-nginx-c1 --detach nginx

How to list running Docker containers

docker ps
docker ps -a

How to run a command in a running container

Run ls /etc/nginx command for my-nginx-c1 container


docker exec fe0cdbc0225a ls /etc/nginx

OR

docker exec my-nginx-c1 ls /etc/nginx


How to stop running containers

docker stop my-nginx-c1

OR
docker stopfe0cdbc0225a


How to remove docker containers

docker rm my-nginx-c1
docker ps -a
Was this answer helpful? 0 Users Found This Useful (0 Votes)