This post explains that how to install and use Docker Container from the default CentOS or RHEL Yum repository. How do I install the latest version of Docker community edition from the official Docker repository on CentOS Linux 7 server. How to use Docker to create your first CentOS container on CentOS 7 Linux.
- What is Docker?
- Installing Docker from the Default YUM Repository
- Installing Docker from the Official Docker Repository
- Installing Docker using the Convenience Script
- Searching Docker Images
- Downloading Docker Images
- Running a Docker Container
- Managing Docker Containers
- Running Docker Command as a Non-root User
What is Docker?
Docker is a computer program that performs operating-system-level virtualization. It was first released in 2013 and is developed by Docker, Inc. Docker is used to run software packages called containers.
Docker is an open source project and it can be create a lightweight container to run applications, it is like virtual machines, and it is more portable, more resource-friendly and more dependent on the host operating system. Now it is maintained by the docker community and Dock Inc.
Installing Docker from the Default YUM Repository
You can use the default yum repository to install Docker on your CentOS Linux 7 or RHEL Linux 7 server, just type the following command:
$ sudo yum install docker -y
The output should be similar to the following:
[root@devops ~]# yum install docker
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-94.gitb2f74b2.el7.centos will be installed
--> Processing Dependency: docker-common = 2:1.13.1-94.gitb2f74b2.el7.centos for package: 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: docker-client = 2:1.13.1-94.gitb2f74b2.el7.centos for package: 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: subscription-manager-rhsm-certificates for package: 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package: 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Running transaction check
---> Package docker-client.x86_64 2:1.13.1-94.gitb2f74b2.el7.centos will be installed
---> Package docker-common.x86_64 2:1.13.1-94.gitb2f74b2.el7.centos will be installed
--> Processing Dependency: skopeo-containers >= 1:0.1.26-2 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: oci-umount >= 2:2.3.3-3 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: oci-systemd-hook >= 1:0.1.4-9 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: oci-register-machine >= 1:0-5.13 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: container-storage-setup >= 0.9.0-1 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: container-selinux >= 2:2.51-1 for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
--> Processing Dependency: atomic-registries for package: 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be installed
---> Package subscription-manager-rhsm-certificates.x86_64 0:1.21.10-3.el7.centos will be installed
--> Running transaction check
---> Package atomic-registries.x86_64 1:1.22.1-26.gitb507039.el7.centos will be installed
--> Processing Dependency: python-yaml for package: 1:atomic-registries-1.22.1-26.gitb507039.el7.centos.x86_64
--> Processing Dependency: python-pytoml for package: 1:atomic-registries-1.22.1-26.gitb507039.el7.centos.x86_64
---> Package container-selinux.noarch 2:2.74-1.el7 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.74-1.el7.noarch
---> Package container-storage-setup.noarch 0:0.11.0-2.git5eaf76c.el7 will be installed
---> Package containers-common.x86_64 1:0.1.31-8.gitb0b750d.el7.centos will be installed
---> Package oci-register-machine.x86_64 1:0-6.git2b44233.el7 will be installed
---> Package oci-systemd-hook.x86_64 1:0.1.18-3.git8787307.el7_6 will be installed
--> Processing Dependency: libyajl.so.2()(64bit) for package: 1:oci-systemd-hook-0.1.18-3.git8787307.el7_6.x86_64
---> Package oci-umount.x86_64 2:2.3.4-2.git87f9237.el7 will be installed
--> Running transaction check
---> Package PyYAML.x86_64 0:3.10-11.el7 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: PyYAML-3.10-11.el7.x86_64
---> Package policycoreutils-python.x86_64 0:2.5-29.el7_6.1 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libcgroup for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
---> Package python-pytoml.noarch 0:0.1.14-1.git7dea353.el7 will be installed
---> Package yajl.x86_64 0:2.0.4-4.el7 will be installed
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.4-4.el7 will be installed
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-20.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> Package libyaml.x86_64 0:0.1.4-11.el7_0 will be installed
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Installing:
docker x86_64 2:1.13.1-94.gitb2f74b2.el7.centos extras 18 M
Installing for dependencies:
PyYAML x86_64 3.10-11.el7 base 153 k
atomic-registries x86_64 1:1.22.1-26.gitb507039.el7.centos extras 35 k
audit-libs-python x86_64 2.8.4-4.el7 base 76 k
checkpolicy x86_64 2.5-8.el7 base 295 k
container-selinux noarch 2:2.74-1.el7 extras 38 k
container-storage-setup noarch 0.11.0-2.git5eaf76c.el7 extras 35 k
containers-common x86_64 1:0.1.31-8.gitb0b750d.el7.centos extras 21 k
docker-client x86_64 2:1.13.1-94.gitb2f74b2.el7.centos extras 3.9 M
docker-common x86_64 2:1.13.1-94.gitb2f74b2.el7.centos extras 96 k
libcgroup x86_64 0.41-20.el7 base 66 k
libseccomp x86_64 2.3.1-3.el7 base 56 k
libsemanage-python x86_64 2.5-14.el7 base 113 k
libyaml x86_64 0.1.4-11.el7_0 base 55 k
oci-register-machine x86_64 1:0-6.git2b44233.el7 extras 1.1 M
oci-systemd-hook x86_64 1:0.1.18-3.git8787307.el7_6 extras 34 k
oci-umount x86_64 2:2.3.4-2.git87f9237.el7 extras 32 k
policycoreutils-python x86_64 2.5-29.el7_6.1 updates 456 k
python-IPy noarch 0.75-6.el7 base 32 k
python-pytoml noarch 0.1.14-1.git7dea353.el7 extras 18 k
setools-libs x86_64 3.3.8-4.el7 base 620 k
subscription-manager-rhsm-certificates x86_64 1.21.10-3.el7.centos updates 207 k
yajl x86_64 2.0.4-4.el7 base 39 k
Transaction Summary
====================================================================================================================================
Install 1 Package (+22 Dependent packages)
Total download size: 25 M
Installed size: 87 M
Is this ok [y/d/N]:y
After the installation has been completed, you can start and enable Docker service with the systemctl command, type:
$ sudo systemctl start docker.service $ sudo systemctl enable docker.service
Outputs:
[root@devops ~]# systemctl enable docker.service Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
Then you can check the docker version with the following docker command:
$ docker --version
Outputs:
[root@devops ~]# docker --version
Docker version 1.13.1, build b2f74b2/1.13.1
You would notice that the current docker version is 1.13.1. and it is a very old version.
Installing Docker from the Official Docker Repository
If you wish to install the latest version of Docker on your CentOS system, you can install it from the official Docker Repository. Just do the following steps:
#1 you need to remove all default Docker packages from your system before installing Docker-CE on your system. Run the following command to remove unnecessary Docker packages:
$ sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce
#2 installing needed dependencies for Docker CE with the following command:
$ yum install yum-utils device-mapper-persistent-data lvm2
Outputs:
[root@devops ~]# yum install yum-utils device-mapper-persistent-data lvm2 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package device-mapper-persistent-data-0.7.3-3.el7.x86_64 already installed and latest version Package 7:lvm2-2.02.180-10.el7_6.3.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package yum-utils.noarch 0:1.1.31-50.el7 will be installed --> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-50.el7.noarch --> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-50.el7.noarch --> Running transaction check ---> Package libxml2-python.x86_64 0:2.9.1-6.el7_2.3 will be installed ---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed --> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch --> Running transaction check ---> Package python-chardet.noarch 0:2.2.1-1.el7_1 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================== Installing: yum-utils noarch 1.1.31-50.el7 base 121 k Installing for dependencies: libxml2-python x86_64 2.9.1-6.el7_2.3 base 247 k python-chardet noarch 2.2.1-1.el7_1 base 227 k python-kitchen noarch 1.1.1-5.el7 base 267 k Transaction Summary ==================================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 861 k Installed size: 4.3 M Is this ok [y/d/N]:y
#3 adding the Docker stable repository to your YUM repo on your CentOS system, type:
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Outputs:
[root@devops ~]# sudo 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
One repo file called docker-ce.repo is saved to /etc/yum.repos.d/ directory.
[root@devops ~]# ls /etc/yum.repos.d/docker-ce.repo /etc/yum.repos.d/docker-ce.repo
Or you can list all enabled repositories to check if docker-ce repo is enabled or not, type:
$ yum repolist
Outputs:
[root@devops ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
docker-ce-stable | 3.5 kB 00:00:00
(1/2): docker-ce-stable/x86_64/primary_db | 25 kB 00:00:01
(2/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:01
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,019
docker-ce-stable/x86_64 Docker CE Stable - x86_64 36
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,016
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 382
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,459
repolist: 24,912
You can see that the Docker repository is enabled from the above outptuts. And you can start to install Docker CE now.
#4 To install the latest version of Docker community edition on your CentOS system using yum command, type:
$ sudo yum install docker-ce
Outputs:
[root@devops ~]# yum install docker-ce Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package docker-ce.x86_64 3:18.09.3-3.el7 will be installed --> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.3-3.el7.x86_64 --> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-18.09.3-3.el7.x86_64 --> Processing Dependency: docker-ce-cli for package: 3:docker-ce-18.09.3-3.el7.x86_64 --> Running transaction check ---> Package container-selinux.noarch 2:2.74-1.el7 will be installed ---> Package containerd.io.x86_64 0:1.2.4-3.1.el7 will be installed ---> Package docker-ce-cli.x86_64 1:18.09.3-3.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================== Installing: docker-ce x86_64 3:18.09.3-3.el7 docker-ce-stable 19 M Installing for dependencies: container-selinux noarch 2:2.74-1.el7 extras 38 k containerd.io x86_64 1.2.4-3.1.el7 docker-ce-stable 22 M docker-ce-cli x86_64 1:18.09.3-3.el7 docker-ce-stable 14 M Transaction Summary ==================================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 55 M Installed size: 236 M Is this ok [y/d/N]:y
#5 After Docker-ce installation has completed, you need to start the Docker Daemon and enable it to start at every server reboot automatically. type:
$ sudo systemctl start docker $ sudo systemctl enable docker
Then verify the Docker service if it is running, type:
$ sudo systemctl status docker
The output should be similar to the following:
[root@devops ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-03-26 10:04:23 EDT; 8s ago
Docs: https://docs.docker.com
Main PID: 3624 (dockerd)
CGroup: /system.slice/docker.service
└─3624 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
From the above outputs, you would notice that the docker.service is active and running.
#6 Let’s check the current Docker version, type:
$ docker --version
Outputs:
[root@devops ~]# docker --version
Docker version 18.09.3, build 774a1f4
From the above outputs, you should know that the current stable version of Docker Community Edition is 18.09.3.
Installing Docker using the Convenience Script
Docker provides a convenience scripts at get.docker.com and test.docker.com for installing testing versions of Docker CE into your development environments quickly and non-interactively. You can run the following curl command to download the installation scripts and then execute it. Aand it will add the official Docker repository to your system, and download and install the latest version of Docker CE on your CentOS or RHEL Linux server. type:
$ curl -fsSL https://get.docker.com -o get-docker.sh $ sudo sh get-docker.sh
After the Docker installation has completed on your system, you need to start and enable the Docker service, type:
$ sudo systemctl start docker.service $ sudo systemctl enable docker.service
Searching Docker Images
You can use docker search command to search for all available Docker container images from Docker hub, for example, you want to create a docker container with CentOS os, you can search for the CentOS image using docker command with the search subcommand,type:
$ sudo docker search centos
Outputs:
[root@devops ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 5270 [OK] ansible/centos7-ansible Ansible on Centos7 121 [OK] jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86… 108 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 83 [OK] imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 52 [OK]
From the above outputs, you should notice that docker search command will crawl Docker hub and return a listing of all matching images. Then you can choose one CentOS image that you would like to download using docker pull command.
Downloading Docker Images
Once we get the image name from the Docker Hub, you can execute the following docker command with the pull subcommand to download the CentOS image to your local disk. type:
$ sudo docker pull centos
Outputs:
[root@devops ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos 8ba884070f61: Pull complete Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861 Status: Downloaded newer image for centos:latest
If you wish to see if the CentOS image is downloaded successfully to your system, using the following command:
$ sudo docker images
Outputs:
[root@devops ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 9f38484d220f 11 days ago 202MB
Running a Docker Container
To create a new container using the CetnOS image downloaded above on your system, you can use docker run or docker create command, type:
$ sudo docker run -i -t centos /bin/bash
Or
$ sudo docker create centos
Note: docker create command will only create a new container but not start it. And docker run command will create and run a container on your CentOS system. You should see that your command prompt should change and you are working inside the newly created CentOS container.
Outputs:
[root@devops ~]# docker run -i -t centos /bin/bash [root@b28fed735ec8 /]#
Managing Docker Containers
If you want to exit from the running docker container, you can type CTRL + P + Q to exit but the container is still running. And if you want to shutdown or exit the running container, you just run exit command inside the container.
To see the running Containers, you can use the following command:
$ sudo docker ps
Outputs:
[root@devops ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b28fed735ec8 centos "/bin/bash" 2 minutes ago Up 2 minutes infallible_khorana
To see all Containers including Active and inactive, just run the following docker ps command with -a option, type:
$ sudo docker ps -a
Outputs:
[root@devops ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b5ca846b355 centos "/bin/bash" 8 seconds ago Exited (0) 5 seconds ago goofy_bassi b28fed735ec8 centos "/bin/bash" 3 minutes ago Up 3 minutes infallible_khorana
To view the latest container you created, type the following command:
$ sudo docker ps -l
Outputs:
[root@devops ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b5ca846b355 centos "/bin/bash" 42 seconds ago Exited (0) 39 seconds ago goofy_bassi
To stop a Container without deleting it, using the following command:
$ sudo docker stop CONTAINER_ID
To start a stopped container its container id is 2b5ca846b355, using docker start command following by the container ID, type:
$ sudo docker start 2b5ca846b355
Outputs:
devops@devops-osetc:~$ sudo docker start 2b5ca846b355 2b5ca846b355 [root@devops ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b5ca846b355 centos "/bin/bash" About a minute ago Up 5 seconds goofy_bassi b28fed735ec8 centos "/bin/bash" 4 minutes ago Up 4 minutes infallible_khorana
If you do not need a inactive container, you can remove it using docker rm command followed by the container ID. You can use the docker ps -a command to find the container ID. type:
$ sudo docker rm 2b5ca846b355
Outputs:
devops@devops-osetc:~$ sudo docker rm 2b5ca846b355 2b5ca846b355 [root@devops ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b28fed735ec8 centos "/bin/bash" 6 minutes ago Up 6 minutes infallible_khorana
Running Docker Command as a Non-root User
The docker command can only be run by the root user or by a user in the docker group. After Docker is installed, and the docker group has been created automatically. So if you want to run a docker command without sudo prefix, you can add the current login user to the docker group. Type the following command to add devops user to the docker group:
$ sudo usermod -aG docker devops
Or
$ sudo usermod -aG docker ${USER}
Conclusion
You should know that how to install and use Docker tool on CentOS Linux 7 or RHEL Linux 7 from this guide, and you also know how to create a docker container with CentOS os in CentOS 7 system. If you want to see more information about Docker, you can go the official web site of Docker directly.