Copyright statement: This article is an original article by Shaon Puppet. Please indicate the original address for reprinting. Thank you very much. https://blog.csdn.net/wh211212/article/details/53171611
Docker official website: http://www.docker.com/
Docker official document: https://docs.docker.com/
Docker Github address: https://github.com/docker/docker
Docker warehouse: https://hub.docker.com/
Docker Chinese Community: http://www.docker.org.cn/book/docker
Docker application scenario: https://www.zhihu.com/question/22969309
1.1、 What is Docker?
Docker is a cloud open source project based on the Go language. It was born in early 2013. The original initiator was dotcloud.
Docker is an open source engine that can easily create a lightweight, portable, and self-sufficient container for any application. Developers compiled and tested containers on their laptops that can be deployed in batches in the production environment, including VMs (virtual machines), bare metal, OpenStack clusters and other basic application platforms.
docker Chinese community
The main goal of Docker is'Build,Ship and Run Any App Anywhere',That is, through the life cycle management of application component packaging (Packing), distribution (Distribution), deployment (Deployment), operation (Runtime), etc., to achieve the application component level "one package, multiple runs". The application component here can be either a Web application, a set of database services, or even an operating system or editor.
1.2、 Why use Docker?
Docker is in the right place, at the right time, following the right trend--That is, efficient construction of applications.
Faster delivery and deployment.
More efficient use of resources
Easier update deployment
Simpler update management
Docker and virtual machine comparison
Docker container is very fast, start and stop can be realized in seconds, which is much faster than traditional virtual machine method
Docker containers require very little system resources, and thousands of Docker containers can run on a host
Docker uses a Git-like operation to facilitate users to obtain, distribute and update application images, with simple instructions and low learning costs
Docker supports flexible automatic creation and deployment mechanisms through Dockerfile configuration files to improve work efficiency
1.3、 Virtualization and Docker
Virtualization is a general concept, with different understandings in different fields. In the computer field, it generally refers to Computing Virtualization, or server virtualization.
The core of virtualization is to abstract resources. The goal is often to run multiple operating systems or applications on the same host, thereby improving the utilization of system resources, while reducing costs, facilitating 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 subcategories:
Fully virtualized. The virtual machine simulates the complete underlying hardware environment and the execution process of privileged instructions, and the guest operating system does not need to be modified. Such as VMware Workstation, VirtulBox, QEMU, etc.
Hardware assisted virtualization. Using hardware-assisted support to process sensitive instructions to achieve full virtualization, the client operating system does not need to be changed, such as Xen, KVM, etc.
Partially virtualized. Only some hardware resources are virtualized, and the client operating system needs to be changed.
Paravirtualization. Part of the hardware interface is provided to the guest operating system in the form of software, and the guest operating system needs to be changed.
Operating system level virtualization. The kernel isolates different processes by creating multiple virtual operating system instances, Docker technology.
Install 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 2016-10-2619:38:40 CST; 12s ago
Docs: http://docs.docker.com
Main PID:3762(docker-current)
CGroup:/system.slice/docker.service
└─3762/usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=journald
Oct 2619: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 2619: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 2619:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.018129400+08:00" level=info msg="Firewalld running: true"
Oct 2619: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 2619:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.567994904+08:00" level=info msg="Loading containers: start."
Oct 2619:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.568039413+08:00" level=info msg="Loading containers: done."
Oct 2619:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.568047306+08:00" level=info msg="Daemon has completed initialization"
Oct 2619: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.3
Oct 2619: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 2619:38:40 linuxprobe.org systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[ root@linuxprobe ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
08 d48e6f1cff: Pull complete
Digest: sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366c
Status: 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
[ root@linuxprobe ~]# docker run -i -t centos /bin/bash
[ root@82699d79557c /]# uname -a
Linux 82699d79557c 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 1922:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[ root@82699d79557c /]# exit
exit
[ root@linuxprobe ~]# #back
[ root@linuxprobe ~]# docker run -i -t centos /bin/bash
[ root@a05c7fd0a54f /]# [root@linuxprobe ~]#
[ root@linuxprobe ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a05c7fd0a54f 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 process
a05c7fd0a54f
[ root@linuxprobe ~]# docker ps #View running docker service
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[ root@linuxprobe ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.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 -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f36383194ad4 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 images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 0584b3d2cf6d Less than a second ago 196.5 MB
my_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 ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2 f0d06526d42 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 -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2 f0d06526d42 my_image/centos_httpd "/bin/bash"27 minutes ago Up 27 minutes 0.0.0.0:8081->80/tcp hopeful_gates
Client browser access
[1] The format of Dockerfile is [INSTRUCTION arguments], please refer to the following description of INSTRUCTION.
INSTRUCTION description
MAINTAINER It sets the author field of the generated image.
RUN When the Docker image is created, it will execute any commands.
CMD will execute any command when the Docker container will be executed.
LABEL It adds metadata to the image.
EXPOSE It informs the Docker container that it will listen on the specified network port at runtime.
ADD It copies a new file, directory or remote file URL.
COPY It copies a new file or directory. The difference with [ADD] is that it is impossible to specify a more URL, nor does it automatically extract archive files.
VOLUME It creates a mount point with a specified name and marks it as saving externally mounted volumes from the local host or other container. USER It sets the user name or UID.
WORKDIR It sets the working directory.
[ root@linuxprobe ~]# vim Dockerfile
# create newFROM centos
MAINTAINER linuxprobe <[email protected]>
RUN yum -y install httpd
RUN echo "Hello LinuxProbe DockerFile">/var/www/html/index.html
EXPOSE 80
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"][root@linuxprobe ~]# docker build -t web_server:latest .
Sending build context to Docker daemon 21.5 kB
Step 1: FROM centos
- - - > 0584 b3d2cf6d
Step 2: MAINTAINER linuxprobe <[email protected]>---> Running in 8064d0091e44
- - - > 940 c8fbe4161
Removing intermediate container 8064d0091e44
Step 3: RUN yum -y install httpd
- - - > Running in 3d37e4919fa9
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: mirrors.163.com
Resolving 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.4forpackage: httpd-2.4.6-40.el7.centos.4.x86_64
- - > Processing Dependency: system-logos >=7.92.1-1forpackage: httpd-2.4.6-40.el7.centos.4.x86_64
- - > Processing Dependency:/etc/mime.types forpackage: httpd-2.4.6-40.el7.centos.4.x86_64
- - > Processing Dependency: libaprutil-1.so.0()(64bit)forpackage: httpd-2.4.6-40.el7.centos.4.x86_64
- - > Processing Dependency: libapr-1.so.0()(64bit)forpackage: 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 Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M
Installing 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 k
Transaction Summary
================================================================================
Install 1Package(+5 Dependent packages)
Total download size:24 M
Installed size:31 M
Downloading 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: NOKEY
Public key for apr-util-1.5.2-6.el7.x86_64.rpm is not installed
Public 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-7
Importing GPG key 0xF4A80EB5:
Userid :"CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
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-7
Running transaction check
Running transaction test
Transaction test succeeded
Running 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!---> 3ce9abf4dfea
Removing intermediate container 3d37e4919fa9
Step 4: RUN echo "Hello LinuxProbe DockerFile">/var/www/html/index.html
- - - > Running in 297d8d666c8d
- - - > 3 d185363045b
Removing intermediate container 297d8d666c8d
Step 5: EXPOSE 80---> Running in 017db517e06a
- - - > 5 c855e478c3c
Removing intermediate container 017db517e06a
Step 6: CMD -D FOREGROUND
- - - > Running in 6add13fca3cb
- - - > 7 a219d9fa6e1
Removing intermediate container 6add13fca3cb
Step 7: ENTRYPOINT /usr/sbin/httpd
- - - > Running in da4671709ee1
- - - > c0d84e256068
Removing intermediate container da4671709ee1
Successfully built c0d84e256068
[ root@linuxprobe ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 0584b3d2cf6d Less than a second ago 196.5 MB
web_server latest c0d84e256068 36 seconds ago 338.3 MB
my_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 ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37d25a405a8 web_server "/usr/sbin/httpd -D F"6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp condescending_knuth
2 f0d06526d42 my_image/centos_httpd "/bin/bash" About an hour ago Up About an hour 0.0.0.0:8081->80/tcp hopeful_gat
Recommended Posts