Install Docker in CentOS 7
Docker Introduction
- Docker is an open-source application container engine that allows developers to package their applications and dependencies to a portable container and then publish them to any popular Linux machine, you can also achieve virtualization. Containers fully use the sandbox mechanism and do not have any interfaces between them.
Official Docker Website: http://www.docker.com/
Docker official documentation: https://docs.docker.com/
Docker Github: https://github.com/docker/docker
Docker Repository: https://hub.docker.com/
Docker community: http://www.docker.org.cn/book/docker
Docker Application Scenario: https://www.zhihu.com/question/22969309
1.1. What is Docker?
Docker is an open-source Cloud project based on the Go language. It was born in the early 2013 S and initially launched by dotcloud. Docker is an open-source engine that allows you to easily create a lightweight, portable, and self-sufficient container for any application. Containers compiled and tested on a notebook can be deployed in a production environment in batches, including VMs, bare metal, OpenStack clusters, and other basic application platforms. The main goal of docker in the Chinese community of Docker is 'Build, Ship and Run Any App Anywhere ', that is, through Packing, Distribution, and Deployment of application components) runtime and other lifecycle management, to achieve the application component level of "One encapsulation, multiple run ". The application component can be either a Web application or a database service, or even an operating system or editor.
1.2. Why use Docker?
Docker conforms to the correct trend at the right place and at the right time-that is, building applications efficiently. Fast delivery and deployment. More efficient resource utilization, easier update deployment, simpler update management, comparison between Docker and virtual machines, and fast Docker containers. Enabling and stopping can be achieved in seconds, compared with traditional virtual machines, Docker containers require little system resources, A host can run thousands of Docker containers on it. Through Git-like operations, Docker can easily obtain, distribute, and update application images. The commands are simple, low learning costs Docker supports flexible automatic creation and deployment mechanisms through the Dockerfile configuration file to improve work efficiency
1.3. virtualization and Docker
Virtualization is a general concept and has different understandings in different fields. In the computer field, Computing Virtualization or server Virtualization is generally used. The core of virtualization is to abstract resources. The goal is to run multiple operating systems or applications on the same host to improve the utilization of system resources and reduce costs, ease of management and fault tolerance. Virtualization can be divided into hardware-based virtualization and software-based virtualization. Software-based virtualization is divided into application virtualization and platform virtualization. Platform virtualization is subdivided into the following sub-categories: full virtualization. The Virtual Machine simulates the complete execution process of the underlying hardware environment and privileged commands, and the customer's operating system does not need to be modified. For example, VMware Workstation, VirtulBox, QEMU and other hardware-aided virtualization. Hardware AIDS Support processing sensitive commands to achieve full virtualization. The client operating system does not need to be changed, such as Xen, KVM, and other virtualization features. Only some hardware resources are virtualized, and the client operating system needs to be changed. Super virtualization. Some hardware interfaces are provided to the client operating system in the form of software, and the client operating system needs to be changed. Operating system virtualization. The kernel isolates different processes by creating multiple virtual operating system instances, Docker technology.
Docker Installation
Installing Docker is an operating system-level virtualization tool that can automatically deploy applications in Containers.
[Root @ linuxprobe ~] # Yum-y install docker [root @ linuxprobe ~] # Systemctl start docker [root @ linuxprobe ~] # Systemctl enable docker [root @ linuxprobe ~] # Systemctl status docker # view docker status ● docker. service-Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker. service; enabled; vendor preset: disabled) Active: active (running) since Wed 19:38:40 CST; 12 s ago Docs: http://docs.docker.com Main PID: 3762 (docker-current) CGroup: /system. slice/docker. service restart-3762/usr/bin/docker-current daemon -- exec-opt native. cgroupdriver = systemd -- selinux-enabled -- log-driver = journaldOct 26 19:38:39 linuxprobe.org docker-current [3762]: time = "2016-10-26T19: 38: 39.844803185 + 08: 00 "level = info msg =" devmapper: Successfully created filesystem xfs on device docker-253: 0-104354176-base "Oct 26 19:38:39 linuxprobe.org docker-current [3762]: time = "2016-10-26T19: 38: 39.994708787 + 08: 00" level = info msg = "Graph migration to content-addressability took 0.00 seconds" Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time = "2016-10-26T19: 38: 40.018129400 + 08: 00" level = info msg = "Firewalld running: true" Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time = "2016-10-26T19: 38: 40.294869786 + 08: 00 "level = info msg =" Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. daemon option -- bip can... red IP address "Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time =" 2016-10-26T19: 38: 40.567994904 + 08: 00 "level = info msg =" Loading containers: start. "Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time =" 2016-10-26T19: 38: 40.568039413 + 08: 00 "level = info msg =" Loading containers: done. "Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time =" 2016-10-26T19: 38: 40.568047306 + 08: 00 "level = info msg =" Daemon has completed initialization "Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time =" 2016-10-26T19: 38: 40.568058527 + 08: 00 "level = info msg =" Docker daemon "commit = cb079f6-unsupported execdriver = native-0.2 graphdriver = devicemapper version = 1.10.3Oct 26 19:38:40 linuxprobe.org docker-current [3762]: time =" 2016-10-26T19: 38: 40.572491688 + 08: 00 "level = info msg =" API listen on/var/run/docker. sock "Oct 26 19:38:40 linuxprobe.org systemd [1]: Started Docker Application Container Engine. hint: Some lines were ellipsized, use-l to show in full.
- Download the official image and create a Container, and output "Welcome to the Docker World" in the Container"
[root@linuxprobe ~]# docker pull centosUsing default tag: latestTrying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos08d48e6f1cff: Pull complete Digest: sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366cStatus: Downloaded newer image for docker.io/centos:latest[root@linuxprobe ~]# docker run centos /bin/echo "Welcome to the Docker World"Welcome to the Docker World
- Use the "I" and "t" options to connect to the Container interaction session, as shown below. If you exit from the Container Session, the Container process is completed.
[root@linuxprobe ~]# docker run -i -t centos /bin/bash[root@82699d79557c /]# uname -aLinux 82699d79557c 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux[root@82699d79557c /]# exitexit[root@linuxprobe ~]# #back
- If you exit from the container session and keep the container process, press Ctrl + p and Ctrl + q
[Root @ linuxprobe ~] # Docker run-I-t centos/bin/bash [root @ a05c7fd0a54f/] # [root @ linuxprobe ~] # [Root @ linuxprobe ~] # Docker psCONTAINER id image command created status ports NAMESa05c7fd0a54f centos "/bin/bash" 24 seconds ago Up 23 seconds trusting_fermat [root @ linuxprobe ~] # Docker attach a05c7fd0a54f # connect docekr process [root @ a05c7fd0a54f/] # [root @ linuxprobe ~] # Docker kill a05c7fd0a54f # kill docker processa05c7fd0a54f [root @ linuxprobe ~] # Docker ps # view the running docker service CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Docker: add an image
- Add an image file to the container
- For example, use the updated official image to install httpd and add it as a new image of the container. This container is generated every time you run the docker run Command. Therefore, add the latest executed container as follows:
[root@linuxprobe ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos latest 0584b3d2cf6d Less than a second ago 196.5 MB# start a Container and install httpd[root@linuxprobe ~]# docker run centos /bin/bash -c "yum -y update; yum -y install httpd"[root@linuxprobe ~]# docker ps -a | head -2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf36383194ad4 centos "/bin/bash -c 'yum -y" 2 minutes ago Exited (0) 45 seconds ago jolly_cray elegant_wright[root@linuxprobe ~]# docker commit f36383194ad4 my_image/centos_httpd[root@linuxprobe ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos latest 0584b3d2cf6d Less than a second ago 196.5 MBmy_image/centos_httpd latest b0be2940865a 7 seconds ago 338.3 MB
Access container
root@linuxprobe ~]# docker run -it -p 8081:80 my_image/centos_httpd /bin/bash[root@2f0d06526d42 /]# /usr/sbin/httpd &[1] 14[root@2f0d06526d42 /]# AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[1]+ Done /usr/sbin/httpd[root@2f0d06526d42 /]# echo "httpd on Docker Container" > /var/www/html/index.html # exit with Ctrl+p, Ctrl+q[root@2f0d06526d42 /]# [root@linuxprobe ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2f0d06526d42 my_image/centos_httpd "/bin/bash" 54 seconds ago Up 52 seconds 0.0.0.0:8081->80/tcp hopeful_gates[root@linuxprobe ~]# docker ps -a | head -2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2f0d06526d42 my_image/centos_httpd "/bin/bash" 27 minutes ago Up 27 minutes 0.0.0.0:8081->80/tcp hopeful_gates
Client browser access
Docker: Use Dockerfile
- Use Dockerfile and automatically create a Docker Image
[1] The Dockerfile format is [INSTRUCTION arguments]. See the following description of INSTRUCTION. INSTRUCTION indicates MAINTAINER which sets the author field of the generated image. RUN: When a Docker image is created, it runs any command. CMD it will execute any command when the Docker container will be executed. LABEL to add metadata to the image. EXPOSE: It notifies the Docker container to listen on the specified network port at runtime. ADD to copy a new file, directory, or Remote File URL. COPY it to COPY a new file or directory. [ADD] The difference is that it cannot specify the remore URL or automatically extract the archive file. VOLUME creates a mount point with the specified name and marks it as a volume user that saves external loads from the local host or other containers. It sets the USER name or UID. WORKDIR: sets the working directory.
- For example, create a dockerfileto install httpd index.html and start httpd using port 80.
[root@linuxprobe ~]# vim Dockerfile # create newFROM centosMAINTAINER linuxprobe <admin@linuxprobe.org>RUN yum -y install httpdRUN echo "Hello LinuxProbe DockerFile" > /var/www/html/index.htmlEXPOSE 80CMD ["-D", "FOREGROUND"]ENTRYPOINT ["/usr/sbin/httpd"][root@linuxprobe ~]# docker build -t web_server:latest . Sending build context to Docker daemon 21.5 kBStep 1 : FROM centos ---> 0584b3d2cf6dStep 2 : MAINTAINER linuxprobe <admin@linuxprobe.org> ---> Running in 8064d0091e44 ---> 940c8fbe4161Removing intermediate container 8064d0091e44Step 3 : RUN yum -y install httpd ---> Running in 3d37e4919fa9Loaded plugins: fastestmirror, ovlDetermining fastest mirrors * base: mirrors.163.com * extras: ftp.sjtu.edu.cn * updates: mirrors.163.comResolving Dependencies--> Running transaction check---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Running transaction check---> Package apr.x86_64 0:1.4.8-3.el7 will be installed---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================ Package Arch Version Repository Size================================================================================Installing: httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 MInstalling for dependencies: apr x86_64 1.4.8-3.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k mailcap noarch 2.1.41-2.el7 base 31 kTransaction Summary================================================================================Install 1 Package (+5 Dependent packages)Total download size: 24 MInstalled size: 31 MDownloading packages:warning: /var/cache/yum/x86_64/7/base/packages/apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYPublic key for apr-util-1.5.2-6.el7.x86_64.rpm is not installedPublic key for httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm is not installed--------------------------------------------------------------------------------Total 382 kB/s | 24 MB 01:05 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : apr-1.4.8-3.el7.x86_64 1/6 Installing : apr-util-1.5.2-6.el7.x86_64 2/6 Installing : httpd-tools-2.4.6-40.el7.centos.4.x86_64 3/6 Installing : centos-logos-70.0.6-3.el7.centos.noarch 4/6 Installing : mailcap-2.1.41-2.el7.noarch 5/6 Installing : httpd-2.4.6-40.el7.centos.4.x86_64 6/6 Verifying : apr-1.4.8-3.el7.x86_64 1/6 Verifying : httpd-tools-2.4.6-40.el7.centos.4.x86_64 2/6 Verifying : apr-util-1.5.2-6.el7.x86_64 3/6 Verifying : httpd-2.4.6-40.el7.centos.4.x86_64 4/6 Verifying : mailcap-2.1.41-2.el7.noarch 5/6 Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7 Complete! ---> 3ce9abf4dfeaRemoving intermediate container 3d37e4919fa9Step 4 : RUN echo "Hello LinuxProbe DockerFile" > /var/www/html/index.html ---> Running in 297d8d666c8d ---> 3d185363045bRemoving intermediate container 297d8d666c8dStep 5 : EXPOSE 80 ---> Running in 017db517e06a ---> 5c855e478c3cRemoving intermediate container 017db517e06aStep 6 : CMD -D FOREGROUND ---> Running in 6add13fca3cb ---> 7a219d9fa6e1Removing intermediate container 6add13fca3cbStep 7 : ENTRYPOINT /usr/sbin/httpd ---> Running in da4671709ee1 ---> c0d84e256068Removing intermediate container da4671709ee1Successfully built c0d84e256068[root@linuxprobe ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos latest 0584b3d2cf6d Less than a second ago 196.5 MBweb_server latest c0d84e256068 36 seconds ago 338.3 MBmy_image/centos_httpd latest b0be2940865a 4 hours ago 338.3 MB[root@linuxprobe ~]# docker run -d -p 80:80 web_server c37d25a405a8e0599bf54fe77d78c807a520242a21ccb15b18d6b6ee4d13415b[root@linuxprobe ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc37d25a405a8 web_server "/usr/sbin/httpd -D F" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp condescending_knuth2f0d06526d42 my_image/centos_httpd "/bin/bash" About an hour ago Up About an hour 0.0.0.0:8081->80/tcp hopeful_gat