After reading this, Docker you're a beginning

In computer technology today, Docker in full swing in the country's development, especially in the first-tier Internet companies, Docker use is very common, before understanding docker, we become familiar with the concepts, containers and virtual machines.

A container with a virtual machine

The traditional virtual machine VMware, VisualBox, they need to simulate the entire machine, including hardware, each virtual machine requires its own operating system, virtual machine Once opened, the pre-allocated their resources will all be occupied. Each virtual machine includes application, the necessary binaries and libraries, as well as a complete user operating system.

The container technology is our host and shared hardware resources and operating systems, dynamic allocation of resources can be achieved. Container comprising all applications and their dependencies, but share cores with other containers. Container in the host operating system, run in separate processes in a user control.

Container technology is a way to achieve operating system virtualization, allowing you to isolate the process by running the application resources and their dependencies. By using containers, we can easily packaged application code, configuration and dependencies, the building blocks of its program easy to use, enabling environment consistency, operational efficiency, developer productivity and version control, and many other targets. Container to help ensure that applications fast, reliable, consistent deployment, during which environmental impact from deployment. The container also gives us more resources to the fine control that allows for higher efficiency of our infrastructure, the difference between the two by the image below, we can very intuitive reflects.

Docker belongs Linux package container, the container provides an easy to use interface. It is the most popular Linux container solutions.

Linux container is not a complete simulation of the operating system, but the process of isolation, the equivalent of the normal process set out in a protective layer. For container inside the process, it is exposed to a variety of virtual resources, enabling the isolation of the underlying system.

Docker will depend on the application and the program, packaged in a file. This file is run, it will generate a virtual container, running in the virtual container, just like running on a real physical machine, with Docker, do not worry about environmental issues.

Overall, Docker fairly simple interface, users can easily create and use a container, put their applications into a container, the container can also be version management, copying, sharing, modifying, just as management code.

Two, Docker advantage

1, Docker start fast, is second level. Virtual machine typically takes several minutes to start.

2, requires fewer resources Docker, Docker operating system level virtualization, Docker container to the kernel, virtually no loss of performance, a performance advantage over  Hypervisor virtualization layer and the core layer,

3, Docker lighter, Docker architecture can share a shared kernel and application library, a very small share of memory. The same hardware environment, the number of mirrors Docker running well below the number of virtual machines, system utilization is very high

4, safety, security Docker also weaker. Docker tenants host root and root equivalents, once the user lift the vessel from the normal user to root authority, it has the direct host as root, and then an operation may be unlimited. the virtual machine virtual machine permissions root root authority tenants and host are separated, and the virtual-machine-using as the intel vt-d and the ring-1 vt-x hardware isolation techniques, such isolation can be prevented and another virtual machine break interaction, and the container has not yet been any kind of hardware isolation, which makes it vulnerable to attack vessel

5, manageability: Docker's centralized management tool is not too mature. Various virtualization technology has a mature management tools, such as VMware Vcentor provide a complete virtual machine management capabilities

6, high availability and recoverability: Docker high-availability support for business is achieved through rapid redeployment. Virtualization with load balancing, high availability, fault tolerance, data protection, migration and security mechanism through mature production-proven, VMware virtual machines can promise 99.9999% availability to ensure business continuity

7, delivery, deployment: virtual machine environment can achieve consistency delivered through a mirror, but a mirror of the distribution system can not. Docker recorded in Dockerfile container construction process, you can achieve rapid distribution and rapid deployment in a cluster

Third, the three basic concepts of Docker

We can see from the chart, Docker includes three basic concepts:

  • Image (镜像)
  • Container (container)
  • Repository (warehouse)

1, the mirror is a prerequisite for running Docker containers, warehouse storage sites mirror, the mirror is visible core Docker's.

So in the end mirroring what is it?

Docker镜像可以看作是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数。镜像不包含任何动态数据,其内容在构建之后也不会被改变。

  

2、Container (容器)

容器的定义和镜像几乎一模一样,也是一堆层的统一视角,唯一区别在于容器的最上面那一层是可读可写的。

由于容器的定义并没有提及是否要运行容器,所以实际上,容器 = 镜像 + 读写层。

3、Repository(仓库)

Docker仓库是集中存放镜像文件的场所,镜像构建完成后,可以很容易在当前宿主上运行,但是,如果需要在其他服务器上使用这个镜像,我们需要一个集中的存储、分发镜像的服务,Docker Registry(仓库注册服务器)就是这样的服务。 有时候会把仓库(Repository)和仓库注册服务器(Registry)混为一谈,并不严格区分。 实际上,一个Docker Registry中可以包含多个仓库(Repository),每个仓库可以包含多个标签 ,每个标签对应着一个镜像。所以说,镜像仓库是Docker用来集中存放镜像文件的地方类似于我们之前常用的代码仓库。

仓库又可以分为两种形式:

(1)public(共有仓库)

Docker Registry公有仓库是开放给用户使用、允许用户管理镜像的 Registry 服务。一般这类公开服务允许用户免费上传、下载公开的镜像,并可能提供收费服务供用户管理私有镜像。

(2)private(私有仓库)

Docker官方提供了Docker Registry镜像,可以直接使用做为私有Registry服务。当用户创建了自己的镜像之后就可以使用push命令将它上传到公有或者私有仓库,这样下次在另外一台机器上使用这个镜像时候,只需要从仓库上 pull下来就可以了。

四、Docker的架构

Docker使用C/S架构,即客户端/服务器体系结构。Docker客户端与Docker服务器进行交互,Docker服务端负责构建、运行和分发Docker镜像。Docker客户端与服务端可以运行在一台机器上,也可以通过RESTful、stock或网络接口与远程Docker服务端进行通信。

这张图展示了Docker客户端、服务端和Docker仓库(即Docker Hub和Docker Cloud ),默认情况下Docker会在Docker中央仓库寻找镜像文件,这种利用仓库管理镜像的设计理念类似于Git,当然这个仓库是可以通过修改配置来指定的,甚至我们可以创建我们自己的私有仓库。

Docker采用的是C/S架构,客户端向服务器发送请求,服务器负责构建、运行和分发容器。客户端和服务器可以运行在同一个Host上,客户端也可以通过socket或REST API与远程的服务器通信。

1、Docker Client

Docker客户端其实就是Docker提供命令行界面工具,是许多Docker 用户与Docker 进行交互的主要方式。客户端可以构建、运行和停止应用程序,还可以远程与Docker_HOST进行交互。 最常用的Docker客户端就是Docker命令,我们可以通过Docker命令很方便地在host上构建和运行Docker容器。

2、Docker Daemon

Docker Daemon是服务器组件,以Linux后台服务的方式运行,是Docker最核心的后台进程,我们也把它称为守护进程。它负责相应来自Docker Client的请求,然后将这些请求翻译成系统调用完成容器管理操作。该进程会在后台启动一个API Server,负责接收由Docker Client发送的请求,接收到的请求将通过Docker Daemon内部的一个路由分发调用,由具体的函数来执行请求。

Docker Daemon的架构如下所示:

Docker Daemon可以认为是通过Docker Server模块接受Docker Client的请求,并在Engine中处理请求,然后根据请求类型,创建出指定的Job并运行。Docker Daemon运行在Docker Host上,负责创建、运行、监控容器,构建、存储镜像。

运行过程的作用有以下几种可能:

  • 向Docker Registry获取镜像
  • 通过grapthdriver执行容器镜像的本地化操作
  • 通过networkdriver执行容器网络环境的配置
  • 通过execdriver执行容器内部运行的执行工作

由于Docker Daemon和Docker Client的启动都是通过可执行文件Docker来完成的,因此两者的启动流程非常相似。Docker可执行文件运行时,运行代码通过不同的命令行flag参数,区分两者,并最终运行两者各自相应的部分。

启动Docker Daemon时,一般可以使用一下命令来完成

docker --daemon = true
docker –d
docker –d = true

再由docker的main函数来解析以上命令的相应的flag参数,并最终完成Docker Daemon的启动。

Docker Daemon的启动流程:

默认配置下,  Docker Daemon只能相应来自本地host的客户端请求。如果要允许远程客户端请求,需要在配置文件中打开TCP监听。我们可以照着如下步骤进行配置:

(1)编辑配置文件 /etc/systemd/system/multi-user.target.wants/docker.service ,在环境变量 ExecStart后面添加 -H tcp://0.0.0.0,允许来自任意 IP 的客户端连接。

(2)重启 Docker Daemon

systemctl daemon-reload
systemctl restart docker.service

(3)我们通过以下命令即可实现与远程服务器通信

docker -H 服务器IP地址 info

-H 是用来指定服务器主机,info子命令用于查看docker服务器的信息

3、Docker Image

Docker镜像可以看作是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。我们可将Docker镜像看成只读模板,通过它可以创建Docker容器。

镜像有多种生成方法:

  • 从无到有开始创建镜像
  • 下载并使用别人创建好的现成的镜像
  • 在现有镜像上创建新的镜像

我们可以将镜像的内容和创建步骤描述在一个文本文件中,这个文本文件称作Dockerfile,通过执行docker build<docker-file>命令可以构建出docker镜像。

4、Docker Registry

Docker Registry是存储Docker Image的仓库,它在Docker生态环境中的位置如下图所示:

运行Docker Push、Docker pull、Docker search时,实际上是通过 Docker Daemon与  Docker registry通信。

5、Docker Container

Docker容器就是Docker镜像的运行实例,是真正运行项目程序、消耗系统资源、提供服务的地方。Docker  Container提供了系统硬件环境,我们可以使用Docker Image这些制作好的系统盘,再加上我们编写好的项目代码,run一下就可以提供服务了。

五、Docker组件如何协作运行容器

容器启动过程如下:

  • Docker客户端执行docker run命令
  • Docker Daemon发现本地没有hello-world镜像
  • Daemon从Docker Hub下载镜像
  • 下载完成,镜像hello-world被保存到本地
  • Docker Daemon启动容器

具体过程可以看如下这幅演示图:

我们可以通过Docker Image可以查看到hello-world已经下载到本地

六、Docker常用命令

我们可以通过docker -h去查看命令的详细的帮助文档。在这里我只会讲一些平常日常比赛或者生活中我们可能会用的比较多的一些命令。

例如,我们需要拉取一个docker镜像,我们可以用如下命令:

docker pull image_name

image_name为镜像的名称,而如果我们想从Docker Hub上去下载某个镜像,我们可以使用以下命令:

docker pull centos:latest

centos:lastest是镜像的名称,Docker daemon发现本地没有我们需要的镜像,会自动去Docker Hub上去下载镜像,下载完成后,该镜像被默认保存到/var/lib/docker目录下。

接着我们如果想查看下主机下存在多少镜像,我们可以用如下命令:

docker images

我们要想知道当前有哪些容器在运行,我们可以用如下命令:

docker ps -a

-a是查看当前所有的容器,包括未运行的

我们该如何去对一个容器进行启动,重启和停止呢?我们可以用如下命令:

docker start container_name/container_id
docker restart container_name/container_id
docker stop container_name/container_id

这个时候我们如果想进入到这个容器中,我们可以使用attach命令:

docker attach container_name/container_id

那如果我们想运行这个容器中的镜像的话,并且调用镜像里面的bash,我们可以使用如下命令:

docker run -t -i container_name/container_id /bin/bash

那如果这个时候,我们想删除指定镜像的话,由于image被某个container引用(拿来运行),如果不将这个引用的container销毁(删除),那image肯定是不能被删除。我们首先得先去停止这个容器:

然后我们用如下命令去删除这个容器:

docker ps
docker stop container_name/container_id

然后这个时候我们再去删除这个镜像:

docker rmi image_name

此时,常用的Docker相关的命令就讲到这里为止了,我们在后续的文章中还会反复地提到这些命令。

七、Dockerfile

Dockerfile是自动构建docker镜像的配置文件,用户可以使用Dockerfile快速创建自定义的镜像,Dockerfile中的命令非常类似于Linux下的shell命令。

我们可以通过下面这幅图来直观地感受下 Docker 镜像、容器和 Dockerfile 三者之间的关系。

我们从上图中可以看到,Dockerfile可以自定义镜像,通过Docker命令去运行镜像,从而达到启动容器的目的。

Dockerfile 是由一行行命令语句组成,并且支持已#开头的注释行。

一般来说,我们可以将Dockerfile分为四个部分:

  • 基础镜像(父镜像)信息指令FROM
  • 维护者信息指令MAINTAINER
  • 镜像操作指令RUN、EVN、ADD和WORKER等
  • 容器启动指令CMD、ENTRYPOINT和USER 等

下面是一段简单的Dockerfile的例子:

1、从Docker Hub上pull下python 2.7的基础镜像

2、显示维护者的信息

3、copy 当前目录到容器中的/app目录下 复制本地主机的<src>(Dockerfile所在目录的相对路径)到容器里<dest>

4、指定工作路径为/app

5、安装依赖包

6、暴露5000端口

7、启动app

这个例子是启动一个Python flask app的Dockerfile(flask是Python的一个轻量级的web框架),相信大家从这个例子中能够稍微理解了Dockerfile的组成以及指令的编写过程。

八、构建Dockerfile代码实例

mkdir static_web
cd static_web
touch Dockerfile
然后 vi Dockerfile  开始编辑该文件
输入 i 开始编辑
 
以下是我们构建的Dockerfile内容
``````````
FROM nginx
MAINTAINER Angel_Kitty <[email protected]>
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
``````````
 
编辑完后 按 esc 退出编辑
然后  :wq    写入 退出

我们在Dockerfile文件所在目录执行:

docker build -t angelkitty/nginx_web:v1 .

我们解释一下,-t是为新镜像设置仓库和名称,其中angelkitty为仓库名,nginx_web为镜像名,:v1为标签(不添加为默认latest)

我们构建完成之后,使用docker images命令查看所有镜像,如果存在REPOSTORY为nginx和TAG是v1的信息,就表示构建成功。

接下来使用docker run命令来启动容器

docker run --name nginx_web -d -p 8080:80   angelkitty/nginx_web:v1

这条命令会用nginx镜像启动一个容器,命名为nginx_web,并且映射了 8080 端口,这样我们可以用浏览器去访问这个nginx服务器:http:localhost:8080或者 http://本机的IP地址:8080/,页面返回信息:

这样一个简单使用Dockerfile构建镜像,运行容器的示例就完成了!

发布了110 篇原创文章 · 获赞 8 · 访问量 6913

Guess you like

Origin blog.csdn.net/guorui_java/article/details/104213743