Install docker on CentOS 6 or 7

This article will show you how to install docker in Centos/RHEL 6 or 7

Installing docker is quite simple now a days. If you are working on centos or rhel install epel-release package and enable docker repo. That will be enough for you to install docker in centos or rhel.

1. Add docker repository for installing latest copy of Docker for RHEL Centos 6 or 7

Update, install epel repo then create a repo called docker.repo and fill in the information for docker repo
[root@docker7 ~]# yum update -y
[root@docker7 ~]# yum install epel-release
[root@docker7 ~]# vi /etc/yum.repos.d/docker.repo
name=Docker Repo

2. Install docker package, then enable the service to start upon reboot, also start the docker service.

Create a user user and change to user user, we will use user user to run docker from now onwards
[root@docker7 ~]# useradd user
[root@docker7 ~]# passwd user
Changing password for user user.
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password:
passwd: all authentication tokens updated successfully.

give sudo prevelage by going inside /etc/sudoers by using visudo command, add the line given below, then save and exit. Now you can change to user user.

[root@docker7 ~]# visudo
add the following line
user   ALL=(ALL)       NOPASSWD: ALL
Change to user user
[root@docker7 ~]# su - user

Now install docker-engine and enable docker to start at boot with systemctl. Then enable docker command and also start docker with systemctl start docker

[user@docker7 ~]$ sudo yum install -y docker-engine
[user@docker7 ~]$ sudo systemctl enable docker
[user@docker7 ~]$ sudo systemctl start docker
[user@docker7 ~]$ sudo systemctl status docker
              For centos 6 
[user@docker7 ~]$ sudo chkconfig docker on 
[user@docker7 ~]$ sudo service docker start 
[user@docker7 ~]$ sudo service docker status

[user@docker7 ~]$ ps aux | grep docker
root      2382  0.3  1.4 498424 26536 ?        Ssl  20:17   0:00 /usr/bin/dockerd
root      2385  0.0  0.2 263104  5568 ?        Ssl  20:17   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc

3.We will run docker as user user.So add the user user to docker group and very user user can run basic docker commands.

 To run docker as other user than root you need to add the user to a group called docker
[user@docker7 ~]$ sudo usermod -aG docker user
[user@docker7 ~]$ id user
uid=1000(user) gid=1000(user) groups=1000(user),994(docker)
[user@docker7 ~]$ sudo systemctl restart docker
[user@docker7 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
If this does-not work exit from user user and login as root user. Then stop docker service and again switch to user user, then restart docker service

4.  Download latest centos image and centos 6 image from public repository for CentOS. And show the downloaded images.

We will use docker pull command to get centos images from docker hub
[user@docker7 ~]$ docker pull centos:latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
[user@docker7 ~]$ docker pull centos:centos6
centos6: Pulling from library/centos
1c8f9aa56c90: Pull complete
Digest: sha256:b358c4a16ef77db3a07eaaaf62c707f51aa15bca820489392cc9d97046bc483a
Status: Downloaded newer image for centos:centos6
[user@docker7 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             b5e5ffb5cdea        3 weeks ago         194MB
centos              latest              5182e96772bf        3 weeks ago         200MB

5.Start the container from latest CentOS image that we downloaded. Start in interactive mode, the exit.

Here we will run docker using docker run -it  in interactive mode, then we will exit out of container using exit command
[user@docker7 ~]$ docker run -it centos:latest /bin/bash
[root@c50d573bcf8e /]# exit
Here docker ps will show running docker container, and docker ps -a will show running as well as stopped containers, we do not see centos:latest container running because we exited the container using exit command.
[user@docker7 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[user@docker7 ~]$ docker ps -a
CONTAINER ID   IMAGE          COMMAND             CREATED              STATUS                        PORTS               NAMES
c50d573bcf8e   centos:latest  "/bin/bash"         About a minute ago   Exited (130) 26 seconds ago                       brave_curie

6. Start the container using latest CentOS image in daemonized form, then list the running container.

We can run docker in daemonized form, by doing this we can create a container but we will not be logged inside the container, 
[user@docker7 ~]$ docker run -itd centos:latest /bin/bash
[user@docker7 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
61fbdf0e9a84        centos:latest       "/bin/bash"         52 seconds ago      Up 51 seconds                           silly_pasteur

[user@docker7 ~]$ docker ps -a
CONTAINER ID   IMAGE           COMMAND        CREATED         STATUS                            PORTS               NAMES
61fbdf0e9a84   centos:latest   "/bin/bash"    55 seconds ago  Up 54 seconds                                         silly_pasteur
c50d573bcf8e   centos:latest   "/bin/bash"    2 minutes ago   Exited (130) About a minute ago                       brave_curie
54648c130b36   centos:latest   "/bin/bash"    3 minutes ago   Exited (127) 3 minutes ago                            jovial_swanson
[user@docker7 ~]$

7.Attach the running container and detach it without killing the container.

Attach or get inside the running container

[user@docker7 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
61fbdf0e9a84        centos:latest       "/bin/bash"         2 hours ago         Up About an hour                        silly_pasteur
[user@docker7 ~]$ docker attach silly_pasteur
Now we are inside a container silly_pasteur,detach  we will get out of silly_pasteur without killing the container by pressing ctrl +p+q, after this container will still be running in the background
[user@docker7 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
61fbdf0e9a84        centos:latest       "/bin/bash"         2 hours ago         Up About an hour                        silly_pasteur
[user@docker7 ~]$

You can refer to the following links for more docker articles

Docker -quick run of docker

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.