淮河人家 發表於 2018-2-24 15:56:00

Docker 容器入门

<h2>1.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器简介</span></h2>
<h3>1.1.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">什么是</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器</span></h3>
<p style="text-indent: 21pt">Linux<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器是与系统其他部分隔离开的一系列进程,从另一个镜像运行,并由该镜像提供支持进程所需的全部文件。容器提供的镜像包含了应用的所有依赖项,因而在从开发到测试再到生产的整个过程中,它都具有可移植性和一致性。</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180215265-876984278.png" alt=""></p>
<blockquote>
<p style="text-indent: 21pt">&nbsp;更加详细地来说,请您假定您在开发一个应用。您使用的是一台笔记本电脑,而且您的开发环境具有特定的配置。其他开发人员身处的环境配置可能稍有不同。您正在开发的应用依赖于您当前的配置,还要依赖于某些特定文件。与此同时,您的企业还拥有标准化的测试和生产环境,且具有自身的配置和一系列支持文件。您希望尽可能多在本地模拟这些环境,而不产生重新创建服务器环境的开销。</p>
<p style="text-indent: 21pt">&nbsp;因此,您要如何确保应用能够在这些环境中运行和通过质量检测,并且在部署过程中不出现令人头疼的问题,也无需重新编写代码和进行故障修复?答案就是使用容器。容器可以确保您的应用拥有必需的配置和文件,使得这些应用能够在从开发到测试、再到生产的整个流程中顺利运行,而不出现任何不良问题。这样可以避免危机,做到皆大欢喜。</p>
</blockquote>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">虽然这只是简化的示例,但在需要很高的可移植性、可配置性和隔离的情况下,我们可以利用</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器通过很多方式解决难题。无论基础架构是在企业内部还是在云端,或者混合使用两者,容器都能满足您的需求。</span></p>
<h3>1.1.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器不就是虚拟化吗</span></h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">是,但也不竟然。我们用一种简单方式来思考一下:</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">虚拟化使得许多操作系统可同时在单个系统上运行。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器则可共享同一个操作系统内核,将应用进程与系统其他部分隔离开。</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180225718-1372384956.png" alt=""></p>
<p style="text-align: center" align="center"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">图</span> - <span style="font-size: 10.5pt; font-family: 微软雅黑, sans-serif; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">普通虚拟化技术和</span><span style="font-size: 10.5pt; font-family: Verdana, sans-serif; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">Docker</span><span style="font-size: 10.5pt; font-family: 微软雅黑, sans-serif; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">的对比</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">这意味着什么?首先,让多个操作系统在单个虚拟机监控程序上运行以实现虚拟化,并不能达成和使用容器同等的轻量级效果。事实上,在仅拥有容量有限的有限资源时,您需要能够可以进行密集部署的轻量级应用。</span>Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器可从单个操作系统运行,在所有容器中共享该操作系统,因此应用和服务能够保持轻量级,并行快速运行。</span></p>
<h3>1.1.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器发展简史</span></h3>
<p style="text-align: center" align="center">&nbsp;<img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180240093-1881375898.png" alt=""></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">我们现在称为容器技术的概念最初出现在</span> 2000 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">年,当时称为</span> FreeBSD jail<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,这种技术可将</span> FreeBSD <span style="font-family: &quot;微软雅黑&quot;, sans-serif">系统分区为多个子系统(也称为</span> Jail<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)。</span>Jail <span style="font-family: &quot;微软雅黑&quot;, sans-serif">是作为安全环境而开发的,系统管理员可与企业内部或外部的多个用户共享这些</span> Jail<span style="font-family: &quot;微软雅黑&quot;, sans-serif">。</span></p>
<p style="text-indent: 21pt">Jail <span style="font-family: &quot;微软雅黑&quot;, sans-serif">的目的是让进程在经过修改的</span> <em>chroot </em><span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境中创建,而不会脱离和影响整个系统</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">—</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">在</span> chroot <span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境中,对文件系统、网络和用户的访问都实现了虚拟化。尽管</span> Jail <span style="font-family: &quot;微软雅黑&quot;, sans-serif">在实施方面存在局限性,但最终人们找到了脱离这种隔离环境的方法。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">但这个概念非常有吸引力。</span></p>
<p style="text-indent: 21pt">2001 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">年,通过</span> Jacques G<span style="font-family: &quot;微软雅黑&quot;, sans-serif">é</span>linas <span style="font-family: &quot;微软雅黑&quot;, sans-serif">的</span> VServer <span style="font-family: &quot;微软雅黑&quot;, sans-serif">项目,隔离环境的实施进入了</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">领域。正如</span> G<span style="font-family: &quot;微软雅黑&quot;, sans-serif">é</span>linas <span style="font-family: &quot;微软雅黑&quot;, sans-serif">所说,这项工作的目的是“在高度独立且安全的单一环境中运行多个通用</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">服务器</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">。”</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">在完成了这项针对</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">中多个受控制用户空间的基础性工作后,</span>Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器开始逐渐成形并最终发展成了现在的模样。</span></p>
<h2>1.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">什么是</span> Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">?</span></h2>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">“</span>Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">”</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">一词指代多种事物,包括开源社区项目、开源项目使用的工具、主导支持此类项目的公司</span> Docker Inc. <span style="font-family: &quot;微软雅黑&quot;, sans-serif">以及该公司官方支持的工具。技术产品和公司使用同一名称,的确让人有点困惑。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">我们来简单说明一下:</span></p>
<blockquote>
<p style="text-indent: 21pt">&nbsp;🎍&nbsp;IT&nbsp;软件中所说的&nbsp;“Docker”&nbsp;,是指容器化技术,用于支持创建和使用&nbsp;Linux&nbsp;容器。</p>
<p style="text-indent: 21pt">🎍&nbsp;开源&nbsp;Docker&nbsp;社区致力于改进这类技术,并免费提供给所有用户,使之获益。</p>
<p style="text-indent: 21pt">🎍&nbsp;Docker Inc.&nbsp;公司凭借&nbsp;Docker&nbsp;社区产品起家,它主要负责提升社区版本的安全性,并将改进后的版本与更广泛的技术社区分享。此外,它还专门对这些技术产品进行完善和安全固化,以服务于企业客户。</p>
</blockquote>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">借助</span> Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">,您可将容器当做重量轻、模块化的虚拟机使用。同时,您还将获得高度的灵活性,从而实现对容器的高效创建、部署及复制,并能将其从一个环境顺利迁移至另一个环境。</span></p>
<h3>1.2.1 Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">如何工作?</span></h3>
<p style="text-indent: 21pt">Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术使用</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">内核和内核功能(例如</span><em> Cgroups </em><em><span style="font-family: &quot;微软雅黑&quot;, sans-serif">和</span> namespaces</em><span style="font-family: &quot;微软雅黑&quot;, sans-serif">)来分隔进程,以便各进程相互独立运行。这种独立性正是采用容器的目的所在;它可以独立运行多种进程、多个应用程序,更加充分地发挥基础设施的作用,同时保持各个独立系统的安全性。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器工具(包括</span> Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)可提供基于镜像的部署模式。这使得它能够轻松跨多种环境,与其依赖程序共享应用或服务组。</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">还可在这一容器环境中自动部署应用程序(或者合并多种流程,以构建单个应用程序)。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">此外,由于这些工具基于</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器构建,使得</span> Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">既易于使用,又别具一格</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">——</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">它可为用户提供前所未有的高度应用程访问权限、快速部署以及版本控制和分发能力。</span></p>
<h3>1.2.2 Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术是否与传统的</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器相同?</span></h3>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">否。</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术最初是基于</span> LXC <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术构建(大多数人都会将这一技术与“传统的”</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器联系在一起),但后来它逐渐摆脱了对这种技术的依赖。</span></p>
<p style="text-indent: 21pt"><span style="text-decoration: underline"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">就轻量级</span> </span><span style="text-decoration: underline"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">虚拟化</span> </span><span style="text-decoration: underline"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">这一功能来看,</span>LXC </span><span style="text-decoration: underline"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">非常有用</span></span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">,但它无法提供出色的开发人员或用户体验。除了运行容器之外,</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术还具备其他多项功能,包括简化用于构建容器、传输镜像以及控制镜像版本的流程。</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180257031-1116592892.png" alt=""></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">传统的</span> Linux <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器使用</span> init <span style="font-family: &quot;微软雅黑&quot;, sans-serif">系统来管理多种进程。这意味着,所有应用程序都作为一个整体运行。与此相反,</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">技术鼓励应用程序各自独立运行其进程,并提供相应工具以实现这一功能。这种精细化运作模式自有其优势。</span></p>
<h3>1.2.3 docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的目标</span></h3>
<p style="text-indent: 7.1pt">docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的主要目标是</span>"Build,Ship and Run any App,Angwhere",<span style="font-family: &quot;微软雅黑&quot;, sans-serif">构建,运输,处处运行</span></p>
<p style="margin-left: 21pt; text-indent: 21pt"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">构建</span></strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">:做一个</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像</span></p>
<p style="margin-left: 21pt; text-indent: 21pt"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">运输</span></strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">:</span>docker pull</p>
<p style="margin-left: 21pt; text-indent: 21pt"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">运行</span></strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">:启动一个容器</span></p>
<p style="text-indent: 7.1pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">每一个容器,他都有自己的文件系统</span>rootfs.</p>
<h2>1.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装</span>Docker</h2>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境说明</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 需要两台几点进行安装</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/redhat-release </span>
CentOS Linux release 7.2.1511<span style="color: rgba(0, 0, 0, 1)"> (Core)
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> uname-r </span>
3.10.0-327<span style="color: rgba(0, 0, 0, 1)">.el7.x86_64
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> hostname -I</span>
10.0.0.100 172.16.1.100<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> hostname -I</span>
10.0.0.101 172.16.1.101</pre>
</div>
<p style="text-indent: 15.75pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在<strong>两个节点</strong>上都进行操作</span></p>
<div class="cnblogs_code">
<pre>wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-<span style="color: rgba(0, 0, 0, 1)">ce.repo
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g</span><span style="color: rgba(128, 0, 0, 1)">'</span> /etc/yum.repos.d/docker-<span style="color: rgba(0, 0, 0, 1)">ce.repo
yum install docker</span>-ce -y</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">修改在</span><span style="background: rgba(255, 255, 0, 1)">docker01</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">配置:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 修改启动文件,监听远程端口</span>
vim /usr/lib/systemd/system/<span style="color: rgba(0, 0, 0, 1)">docker.service
ExecStart</span>=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://10.0.0.100:2375<span style="color: rgba(0, 0, 0, 1)">
systemctl daemon</span>-<span style="color: rgba(0, 0, 0, 1)">reload
systemctl enable docker.service
systemctl restart docker.service
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ps -ef检查进行,是否启动</span></pre>
</div>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">在</span>docker02<span style="font-family: &quot;微软雅黑&quot;, sans-serif">测试</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker -H 10.0.0.100 info</span>
<span style="color: rgba(0, 0, 0, 1)">Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: </span>17.12.0-<span style="color: rgba(0, 0, 0, 1)">ce
Storage Driver: devicemapper
···</span></pre>
</div>
<h3>1.3.1 Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">基础命令操作</span></h3>
<p style="margin-left: 7.1pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">相关信息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">docker version</span>
<span style="color: rgba(0, 0, 0, 1)">Client:
Version:    </span>17.12.0-<span style="color: rgba(0, 0, 0, 1)">ce
API version:    </span>1.35<span style="color: rgba(0, 0, 0, 1)">
Go version:    go1.</span>9.2<span style="color: rgba(0, 0, 0, 1)">
Git commit:    c97c6d6
Built:    Wed Dec </span>27 20:10:14 2017<span style="color: rgba(0, 0, 0, 1)">
OS</span>/Arch:    linux/<span style="color: rgba(0, 0, 0, 1)">amd64
Server:
Engine:
Version:    </span>17.12.0-<span style="color: rgba(0, 0, 0, 1)">ce
API version:    </span>1.35 (minimum version 1.12<span style="color: rgba(0, 0, 0, 1)">)
Go version:    go1.</span>9.2<span style="color: rgba(0, 0, 0, 1)">
Git commit:    c97c6d6
Built:    Wed Dec </span>27 20:12:46 2017<span style="color: rgba(0, 0, 0, 1)">
OS</span>/Arch:    linux/<span style="color: rgba(0, 0, 0, 1)">amd64
Experimental:    false</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">配置</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像加速</span></p>
<div class="cnblogs_code">
<pre>vi /etc/docker/<span style="color: rgba(0, 0, 0, 1)">daemon.json
{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">registry-mirrors</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://registry.docker-cn.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
}    </span></pre>
</div>
<h3>1.3.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动第一个容器</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 80:80 nginx</span>
Unable to find image <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">nginx:latest</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> locally
latest: Pulling </span><span style="color: rgba(0, 0, 255, 1)">from</span> library/<span style="color: rgba(0, 0, 0, 1)">nginx
e7bb522d92ff: Pull complete
6edc05228666: Pull complete
cd866a17e81f: Pull complete
Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
Status: Downloaded newer image </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> nginx:latest
8d8f81da12b5c10af6ba1a5d07f4abc041cb95b01f3d632c3d638922800b0b4d
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 容器启动后,在浏览器进行访问测试</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">参数说明</span></p>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 40.62%; border-top: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: 1pt solid rgba(155, 187, 89, 1); border-right: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="40%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">参数</span></strong></p>
</td>
<td style="width: 59.38%; border-top: 1pt solid rgba(155, 187, 89, 1); border-right: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="59%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 40.62%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="40%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>run</strong></p>
</td>
<td style="width: 59.38%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="59%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建并运行一个容器</span></p>
</td>
</tr>
<tr>
<td style="width: 40.62%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="40%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-d</strong></p>
</td>
<td style="width: 59.38%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="59%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">放入后台</span></p>
</td>
</tr>
<tr>
<td style="width: 40.62%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="40%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p</strong></p>
</td>
<td style="width: 59.38%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="59%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">端口映射</span></p>
</td>
</tr>
<tr>
<td style="width: 40.62%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="40%">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>nginx</strong></p>
</td>
<td style="width: 59.38%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="59%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像名称</span></p>
</td>
</tr>
</tbody>
</table>
<h3>1.3.3 Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像生命周期</span></h3>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180313468-930180890.png" alt="">&nbsp;</p>
<h2>1.4 docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像相关操作</span></h2>
<h3>1.4.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">搜索官方仓库镜像</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">docker search centos</span>
<span style="color: rgba(0, 0, 0, 1)">NAME                      DESCRIPTION                  STARS    OFFICIAL               AUTOMATED
centos                  The official build of CentOS.</span>3992<span style="color: rgba(0, 0, 0, 1)">         
ansible</span>/centos7-ansible   Ansible on Centos7             105                              </pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">列表说明</span></p>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 39.26%; border-top: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: 1pt solid rgba(155, 187, 89, 1); border-right: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" valign="top" width="39%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">参数</span></strong></p>
</td>
<td style="width: 60.74%; border-top: 1pt solid rgba(155, 187, 89, 1); border-right: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="39%">
<p><strong>NAME</strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="60%">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像名称</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" valign="top" width="39%">
<p><strong>DESCRIPTION</strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" valign="top" width="60%">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像说明</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="39%">
<p><strong>STARS</strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="60%">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">点赞数量</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" valign="top" width="39%">
<p><strong>OFFICIAL</strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" valign="top" width="60%">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">是否是官方的</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="39%">
<p><strong>AUTOMATED</strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" valign="top" width="60%">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">是否是自动构建的</span></p>
</td>
</tr>
</tbody>
</table>
<h3>1.4.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">获取镜像</span></h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">根据镜像名称拉取镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker pull centos</span>
<span style="color: rgba(0, 0, 0, 1)">Using default tag: latest
latest: Pulling </span><span style="color: rgba(0, 0, 255, 1)">from</span> library/<span style="color: rgba(0, 0, 0, 1)">centos
af4b0a2388c6: Downloading</span>34.65MB/73.67MB</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看当前主机镜像列表</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image list </span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
centos            latest            ff426288ea90      </span>3<span style="color: rgba(0, 0, 0, 1)"> weeks ago         207MB
nginx               latest            3f8a4339aadd      </span>5 weeks ago         108MB</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">拉第三方镜像方法</span></p>
<div class="cnblogs_code">
<pre>docker pull index.tenxcloud.com/tenxcloud/httpd</pre>
</div>
<h3>1.4.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">导出镜像</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image list </span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
centos            latest            ff426288ea90      </span>3<span style="color: rgba(0, 0, 0, 1)"> weeks ago         207MB
nginx               latest            3f8a4339aadd      </span>5<span style="color: rgba(0, 0, 0, 1)"> weeks ago         108MB
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 导出</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image save centos &gt; docker-centos.tar.gz</span></pre>
</div>
<h3>1.4.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">删除镜像</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image rm centos:latest</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image list </span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
nginx               latest            3f8a4339aadd      </span>5 weeks ago         108MB</pre>
</div>
<h3>1.4.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">导入镜像</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image load -i docker-centos.tar.gz</span>
e15afa4858b6: Loading layer215.8MB/215<span style="color: rgba(0, 0, 0, 1)">.8MB
Loaded image: centos:latest
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image list </span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
centos            latest            ff426288ea90      </span>3<span style="color: rgba(0, 0, 0, 1)"> weeks ago         207MB
nginx               latest            3f8a4339aadd      </span>5 weeks ago         108MB</pre>
</div>
<h3>1.4.6 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看镜像的详细信息</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image inspect centos</span></pre>
</div>
<h2>1.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的日常管理</span></h2>
<h3>1.5.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的起</span>/<span style="font-family: &quot;微软雅黑&quot;, sans-serif">停</span></h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">最简单的运行一个容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run nginx</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建容器,两步走(不常用)</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker create centos:latest/bin/bash</span>
<span style="color: rgba(0, 0, 0, 1)">bb7f32368ecf0492adb59e20032ab2e6cf6a563a0e6751e58930ee5f7aaef204
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker start stupefied_nobel</span>
stupefied_nobel</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">快速启动容器方法</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker runcentos:latest/usr/bin/sleep 20;</span></pre>
</div>
<p>&nbsp;&nbsp; <strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 0, 0, 1)">容器内的第一个进程必须一直处于运行的状态,否则这个容器,就会处于退出状态!</span></strong></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看正在运行的容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker container ls</span>
<span style="color: rgba(0, 0, 0, 1)">    或
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker ps </span>
<span style="color: rgba(0, 0, 0, 1)">CONTAINER ID      IMAGE               COMMAND                  CREATED             STATUS            PORTS               NAMES
8708e93fd767      nginx               </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon of…</span><span style="color: rgba(128, 0, 0, 1)">"</span>   6 seconds ago       Up 4 seconds      80/tcp            keen_lewin</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看你容器详细信息</span>/ip</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker containerinspect容器名称/id</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看你所有容器(包括未运行的)</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker ps -a</span>
<span style="color: rgba(0, 0, 0, 1)">CONTAINER ID      IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
8708e93fd767      nginx               </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon of…</span><span style="color: rgba(128, 0, 0, 1)">"</span>   4 minutes ago       Exited (0) 59<span style="color: rgba(0, 0, 0, 1)"> seconds ago                     keen_lewin
f9f3e6af7508      nginx               </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon of…</span><span style="color: rgba(128, 0, 0, 1)">"</span>   5 minutes ago       Exited (0) 5<span style="color: rgba(0, 0, 0, 1)"> minutes ago                        optimistic_haibt
8d8f81da12b5      nginx               </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon of…</span><span style="color: rgba(128, 0, 0, 1)">"</span>   3 hours ago         Exited (0) 3 hours ago                        lucid_bohr</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">停止容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker stop 容器名称/id </span>
<span style="color: rgba(0, 0, 0, 1)">或
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker containerkill容器名称/id</span></pre>
</div>
<h3>1.5.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">进入容器方法</span></h3>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动时进去方法</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -it #参数:-it 可交互终端</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -it nginx:latest/bin/bash</span>
root@79241093859e:/<span style="color: rgba(0, 128, 0, 1)">#</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">退出</span>/<span style="font-family: &quot;微软雅黑&quot;, sans-serif">离开容器</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:Perl;gutter:true;">ctrl+p &amp; ctrl+q
</pre>
</div>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动后进入容器的方法</span></strong></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动一个</span>docker</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -it centos:latest </span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ps -ef </span>
<span style="color: rgba(0, 0, 0, 1)">UID         PID   PPIDC STIME TTY          TIME CMD
root          </span>1      00 15:47 pts/0    00:00:00 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
root         </span>13      10 15:47 pts/0    00:00:00 ps -ef</pre>
</div>
<p>attach<span style="font-family: &quot;微软雅黑&quot;, sans-serif">进入容器,使用</span>pts/0 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">,会让所用通过此方法进如放入用户看到同样的操作。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker attach 1bf0f43c4d2f</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ps -ef </span>
<span style="color: rgba(0, 0, 0, 1)">UID         PID   PPIDC STIME TTY          TIME CMD
root          </span>1      00 15:47 pts/0    00:00:00 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
root         </span>14      10 15:49 pts/0    00:00:00 ps -ef</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">自命名启动一个容器</span> --name</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker attach 1bf0f43c4d2f</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ps -ef </span>
<span style="color: rgba(0, 0, 0, 1)">UID         PID   PPIDC STIME TTY          TIME CMD
root          </span>1      00 15:47 pts/0    00:00:00 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
root         </span>14      10 15:49 pts/0    00:00:00 ps -ef</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">exrc </span></strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(255, 255, 0, 1)">进入容器方法(推荐使用)</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker exec -it clsn1/bin/bash </span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 重新分配一个终端</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ps -ef </span>
<span style="color: rgba(0, 0, 0, 1)">UID         PID   PPIDC STIME TTY          TIME CMD
root          </span>1      00 16:11 pts/0    00:00:00 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
root         </span>13      00 16:14 pts/1    00:00:00 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
root         </span>26   130 16:14 pts/1    00:00:00 ps -ef</pre>
</div>
<h3>1.5.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">删除所有容器</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker rm -f`docker ps -a -q`</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> -f 强制删除</span></pre>
</div>
<h3>1.5.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动时进行端口映射</span></h3>
<p style="text-indent: 7.1pt">-p<span style="font-family: &quot;微软雅黑&quot;, sans-serif">参数端口映射</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 8888:80nginx:latest </span>
287bec5c60263166c03e1fc5b0b8262fe76507be3dfae4ce5cd2ee2d1e8a89a9</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">不同指定映射方法</span></p>
<table style="border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 233.65pt; border-top: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: 1pt solid rgba(155, 187, 89, 1); border-right: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="312">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">参数</span></strong></p>
</td>
<td style="width: 289.15pt; border-top: 1pt solid rgba(155, 187, 89, 1); border-right: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="386">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 233.65pt; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="312">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p hostPort:containerPort&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
</td>
<td style="width: 289.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="386">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">端口映射</span>&nbsp; -p 8080:80</p>
</td>
</tr>
<tr>
<td style="width: 233.65pt; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="312">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p ip:hostPort:containerPort&nbsp; </strong></p>
</td>
<td style="width: 289.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="386">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">配置监听地址</span> -p 10.0.0.100:8080:80</p>
</td>
</tr>
<tr>
<td style="width: 233.65pt; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="312">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p ip::containerPort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
</td>
<td style="width: 289.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="386">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">随机分配端口</span> -p 10.0.0.100::80</p>
</td>
</tr>
<tr>
<td style="width: 233.65pt; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="312">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p hostPort:containerPort:udp </strong></p>
</td>
<td style="width: 289.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="386">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">指定协议</span> -p 8080:80:tcp</p>
</td>
</tr>
<tr>
<td style="width: 233.65pt; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="312">
<p style="text-align: justify; text-justify: inter-ideograph"><strong>-p 81:80 –p 443:443&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
</td>
<td style="width: 289.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="386">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">指定多个</span></p>
</td>
</tr>
</tbody>
</table>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">随机映射</span></p>
<div class="cnblogs_code">
<pre>docker run -P (大P)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 需要镜像支持</span></pre>
</div>
<h2>1.6 Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">数据卷的管理</span></h2>
<h3>1.6.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">挂载时创建卷</span></h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">挂载卷</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 80:80 -v /data:/usr/share/nginx/html nginx:latest</span>
079786c1e297b5c5031e7a841160c74e91d4ad06516505043c60dbb78a259d09</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器内站点目录</span>: /usr/share/nginx/html</p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在宿主机写入数据,查看</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "http://www.nmtui.com" &gt;/data/index.html</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.100</span>
http://www.nmtui.com</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">设置共享卷,使用同一个卷启动一个新的容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 8080:80 -v /data:/usr/share/nginx/html nginx:latest </span>
<span style="color: rgba(0, 0, 0, 1)">351f0bd78d273604bd0971b186979aa0f3cbf45247274493d2490527babb4e42
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.100:8080</span>
http://www.nmtui.com</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看卷列表</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker volume ls</span>
DRIVER            VOLUME NAME</pre>
</div>
<h3>1.6.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建卷后挂载</span></h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建一个卷</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker volume create </span>
<span style="color: rgba(0, 0, 0, 1)">f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker volume ls </span>
<span style="color: rgba(0, 0, 0, 1)">DRIVER            VOLUME NAME
local               f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">指定卷名</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker volume ls </span>
<span style="color: rgba(0, 0, 0, 1)">DRIVER            VOLUME NAME
local               clsn
local               f3b95f7bd17da220e63d4e70850b8d7fb3e20f8ad02043423a39fdd072b83521</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看卷路径</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker volume inspect clsn </span>
<span style="color: rgba(0, 0, 0, 1)">[
    {
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">CreatedAt</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2018-02-01T00:39:25+08:00</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Driver</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">local</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Labels</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {},
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Mountpoint</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/var/lib/docker/volumes/clsn/_data</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">clsn</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Options</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {},
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Scope</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">local</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    }
]</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用卷创建</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 9000:80 -v clsn:/usr/share/nginx/html nginx:latest </span>
<span style="color: rgba(0, 0, 0, 1)">1434559cff996162da7ce71820ed8f5937fb7c02113bbc84e965845c219d3503
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 宿主机测试</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo 'blog.nmtui.com' &gt;/var/lib/docker/volumes/clsn/_data/index.html </span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.100:9000</span>
blog.nmtui.com</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">设置卷</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-d-P--volumes-from 079786c1e297 nginx:latest </span>
b54b9c9930b417ab3257c6e4a8280b54fae57043c0b76b9dc60b4788e92369fb</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看使用的端口</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> netstat -lntup </span>
<span style="color: rgba(0, 0, 0, 1)">Active Internet connections (only servers)
Proto Recv</span>-Q Send-Q Local Address         Foreign Address         State       PID/<span style="color: rgba(0, 0, 0, 1)">Program name   
tcp      0      0 </span>0.0.0.0:22            0.0.0.0:*               LISTEN      1400/<span style="color: rgba(0, 0, 0, 1)">sshd         
tcp      0      0 </span>10.0.0.100:2375         0.0.0.0:*               LISTEN      26218/<span style="color: rgba(0, 0, 0, 1)">dockerd      
tcp6       0      0 :::</span>9000               :::*                  LISTEN      32015/docker-<span style="color: rgba(0, 0, 0, 1)">proxy
tcp6       0      0 :::</span>8080               :::*                  LISTEN      31853/docker-<span style="color: rgba(0, 0, 0, 1)">proxy
tcp6       0      0 :::</span>80                   :::*                  LISTEN      31752/docker-<span style="color: rgba(0, 0, 0, 1)">proxy
tcp6       0      0 :::</span>22                   :::*                  LISTEN      1400/<span style="color: rgba(0, 0, 0, 1)">sshd         
tcp6       0      0 :::</span>32769                :::*                  LISTEN      32300/docker-<span style="color: rgba(0, 0, 0, 1)">proxy
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.100:32769</span>
http://www.nmtui.com</pre>
</div>
<h3><span style="background: rgba(255, 255, 0, 1)">1.6.3 </span><span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(255, 255, 0, 1)">手动将容器保存为镜像</span></h3>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">本次是基于</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">官方</span>centos 6.8 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像创建</span></p>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">官方镜像列表:</span>https://hub.docker.com/explore/</p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动一个</span>centos6.8<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker pullcentos:6.8</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -it -p 1022:22 centos:6.8/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 在容器种安装sshd服务,并修改系统密码</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> yum installopenssh-server -y </span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "root:123456" |chpasswd</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">/etc/init.d/sshd start</span></pre>
</div>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动完成后镜像</span>ssh<span style="font-family: &quot;微软雅黑&quot;, sans-serif">连接测试</span></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">将容器提交为镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker commit brave_mcclintockcentos6-ssh</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用新的镜像启动容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d-p 1122:22centos6-ssh:latest/usr/sbin/sshd -D </span>
5b8161fda2a9f2c39c196c67e2eb9274977e7723fe51c4f08a0190217ae93094</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在容器安装</span>httpd<span style="font-family: &quot;微软雅黑&quot;, sans-serif">服务</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">yum install httpd -y</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">编写启动脚本脚本</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> catinit.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash </span>
/etc/init.d/<span style="color: rgba(0, 0, 0, 1)">httpd start
</span>/usr/sbin/sshd -<span style="color: rgba(0, 0, 0, 1)">D
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> chmod +x init.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 注意执行权限</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">再次提交为新的镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker commit5b8161fda2a9 centos6-httpd </span>
sha256:705d67a786cac040800b8485cf046fd57b1828b805c515377fc3e9cea3a481c1</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动镜像,做好端口映射。并在浏览器中测试访问</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d -p 1222:22 -p 80:80centos6-httpd /init.sh </span>
46fa6a06644e31701dc019fb3a8c3b6ef008d4c2c10d46662a97664f838d8c2c</pre>
</div>
<h2>1.7 Dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">自动构建</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像</span></h2>
<p style="margin-left: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">官方构建</span>dockerffile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">文件参考</span></p>
<p style="text-indent: 21pt">https://github.com/CentOS/CentOS-Dockerfiles</p>
<h3>1.7.1 Dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">指令集</span></h3>
<p>dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">主要组成部分:</span></p>
<blockquote>
<p>&nbsp;&nbsp; &nbsp; 基础镜像信息&nbsp;FROM centos:6.8</p>
<p>&nbsp;&nbsp; &nbsp; 制作镜像操作指令RUN yum insatll openssh-server -y</p>
<p>&nbsp;&nbsp; &nbsp; 容器启动时执行指令&nbsp;CMD ["/bin/bash"]</p>
</blockquote>
<p>dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">常用指令:</span></p>
<blockquote>
<p>&nbsp;&nbsp; &nbsp; FROM&nbsp;这个镜像的妈妈是谁?(指定基础镜像)</p>
<p>&nbsp;&nbsp; &nbsp; MAINTAINER&nbsp;告诉别人,谁负责养它?(指定维护者信息,可以没有)</p>
<p>&nbsp;&nbsp; &nbsp; RUN&nbsp;你想让它干啥(在命令前面加上RUN即可)</p>
<p>&nbsp;&nbsp; &nbsp; ADD&nbsp;给它点创业资金(COPY文件,会自动解压)</p>
<p>&nbsp;&nbsp; &nbsp; WORKDIR&nbsp;我是cd,今天刚化了妆(设置当前工作目录)</p>
<p>&nbsp;&nbsp; &nbsp; VOLUME&nbsp;给它一个存放行李的地方(设置卷,挂载主机目录)</p>
<p>&nbsp;&nbsp; &nbsp; EXPOSE&nbsp;它要打开的门是啥(指定对外的端口)</p>
<p>&nbsp;&nbsp; &nbsp; CMD&nbsp;奔跑吧,兄弟!(指定容器启动后的要干的事情)</p>
</blockquote>
<p>dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">其他指令:</span>&nbsp;</p>
<blockquote>
<p>&nbsp; &nbsp; COPY&nbsp;复制文件</p>
<p>&nbsp;&nbsp;&nbsp; ENV&nbsp;&nbsp;环境变量</p>
<p>&nbsp;&nbsp;&nbsp; ENTRYPOINT&nbsp;&nbsp;容器启动后执行的命令</p>
</blockquote>
<h3>1.7.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建一个</span>Dockerfile</h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建第一个</span>Dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">文件</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建目录</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cd /opt/base</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 创建Dcokerfile文件,注意大小写</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim Dockerfile</span>
FROM centos:6.8<span style="color: rgba(0, 0, 0, 1)">
RUN yum install openssh</span>-server -<span style="color: rgba(0, 0, 0, 1)">y
RUN echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root:123456</span><span style="color: rgba(128, 0, 0, 1)">"</span> |<span style="color: rgba(0, 0, 0, 1)">chpasswd
RUN </span>/etc/init.d/<span style="color: rgba(0, 0, 0, 1)">sshd start
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/usr/sbin/sshd</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-D</span><span style="color: rgba(128, 0, 0, 1)">"</span>]</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">构建</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker image build-t centos6.8-ssh . </span>
-t 为镜像标签打标签. 表示当前路径</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用自构建的镜像启动</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-d -p 2022:22 centos6.8-ssh-b </span>
dc3027d3c15dac881e8e2aeff80724216f3ac725f142daa66484f7cb5d074e7a</pre>
</div>
<h3>1.7.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用</span>Dcokerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装</span>kodexplorer</h3>
<p style="margin-left: 15.75pt">Dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">文件内容</span></p>
<div class="cnblogs_code">
<pre>FROM centos:6.8<span style="color: rgba(0, 0, 0, 1)">
RUN yum install wget unzip php php</span>-gd php-mbstring -y &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> yum clean all
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 设置工作目录,之后的操作都在这个目录中</span>
WORKDIR /var/www/html/<span style="color: rgba(0, 0, 0, 1)">
RUN wget </span>-c http://static.kodcloud.com/update/download/kodexplorer4.25<span style="color: rgba(0, 0, 0, 1)">.zip
RUN unzip kodexplorer4.</span>25.zip &amp;&amp; rm -f kodexplorer4.25<span style="color: rgba(0, 0, 0, 1)">.zip
RUN chown </span>-<span style="color: rgba(0, 0, 0, 1)">R apache.apache .
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/usr/sbin/apachectl</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-D</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">FOREGROUND</span><span style="color: rgba(128, 0, 0, 1)">"</span>]</pre>
</div>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">更多的</span>Dockerfile<span style="font-family: &quot;微软雅黑&quot;, sans-serif">可以参考官方方法。</span></p>
<h2>1.8 Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">中的镜像分层</span></h2>
<p style="margin-left: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">参考文档:</span>http://www.maiziedu.com/wiki/cloud/dockerimage/</p>
<p style="margin-left: 21pt">Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">支持通过扩展现有镜像,创建新的镜像。实际上,</span>Docker Hub <span style="font-family: &quot;微软雅黑&quot;, sans-serif">中</span> 99% <span style="font-family: &quot;微软雅黑&quot;, sans-serif">的镜像都是通过在</span> base <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像中安装和配置需要的软件构建出来的。</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180342031-1621628053.png" alt="">&nbsp;</p>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">从上图可以看到,新镜像是从</span> base <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层。</span></p>
<h3>1.8.1 Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像为什么分层</span></h3>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像分层最大的一个好处就是共享资源。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">比如说有多个镜像都从相同的</span> base <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像构建而来,那么</span> Docker Host <span style="font-family: &quot;微软雅黑&quot;, sans-serif">只需在磁盘上保存一份</span> base <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像;同时内存中也只需加载一份</span> base <span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">如果多个容器共享一份基础镜像,当某个容器修改了基础镜像的内容,比如</span> /etc <span style="font-family: &quot;微软雅黑&quot;, sans-serif">下的文件,这时其他容器的</span> /etc <span style="font-family: &quot;微软雅黑&quot;, sans-serif">是不会被修改的,修改只会被限制在单个容器内。这就是容器</span><strong><span style="text-decoration: underline"> Copy-on-Write </span></strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">特性。</span></p>
<h3>1.8.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">可写的容器层</span></h3>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180355656-1765414705.png" alt="">&nbsp;</p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">所有对容器的改动</span> - <span style="font-family: &quot;微软雅黑&quot;, sans-serif">无论添加、删除、还是修改文件都只会发生在容器层中。只有<strong><span style="color: rgba(255, 0, 0, 1)">容器层是可写的,容器层下面的所有镜像层都是只读的</span></strong>。</span></p>
<h3>1.8.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器层的细节说明</span></h3>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像层数量可能会很多,所有镜像层会联合在一起组成一个统一的文件系统。如果不同层中有一个相同路径的文件,比如</span> /a<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,上层的</span> /a <span style="font-family: &quot;微软雅黑&quot;, sans-serif">会覆盖下层的</span> /a<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,也就是说用户只能访问到上层中的文件</span> /a<span style="font-family: &quot;微软雅黑&quot;, sans-serif">。在容器层中,用户看到的是一个叠加之后的文件系统。</span></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">文件操作的</span></p>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 20.3%; border-top: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: 1pt solid rgba(155, 187, 89, 1); border-right: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="20%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">文件操作</span></strong></p>
</td>
<td style="width: 79.7%; border-top: 1pt solid rgba(155, 187, 89, 1); border-right: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="79%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">说明</span></strong></p>
</td>
</tr>
<tr style="height: 34.65pt">
<td style="width: 20.3%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt; height: 34.65pt" width="20%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">添加文件</span></strong></p>
</td>
<td style="width: 79.7%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt; height: 34.65pt" width="79%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在容器中创建文件时,新文件被<span style="color: rgba(255, 0, 0, 1)">添加到容器层</span>中。</span></p>
</td>
</tr>
<tr style="height: 50.05pt">
<td style="width: 20.3%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt; height: 50.05pt" width="20%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">读取文件</span></strong></p>
</td>
<td style="width: 79.7%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt; height: 50.05pt" width="79%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在容器中读取某个文件时,</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">会<span style="color: rgba(255, 0, 0, 1)">从上往下依次在各镜像层中查找</span>此文件。<span style="color: rgba(255, 0, 0, 1)">一旦找到,立即将其复制到容器层,然后打开并读入内存</span>。</span></p>
</td>
</tr>
<tr style="height: 49.2pt">
<td style="width: 20.3%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt; height: 49.2pt" width="20%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">修改文件</span></strong></p>
</td>
<td style="width: 79.7%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt; height: 49.2pt" width="79%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在容器中修改已存在的文件时,</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">会从<span style="color: rgba(255, 0, 0, 1)">上往下依次在各镜像层中查找</span>此文件。一旦找到,立即将其复制到容器层,然后修改之。</span></p>
</td>
</tr>
<tr>
<td style="width: 20.3%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="20%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">删除文件</span></strong></p>
</td>
<td style="width: 79.7%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="79%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在容器中删除文件时,</span>Docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">也是<span style="color: rgba(255, 0, 0, 1)">从上往下依次在镜像层中查找</span>此文件。找到后,会在容器层中<strong><span style="color: rgba(255, 0, 0, 1); background: rgba(255, 255, 0, 1)">记录下此删除操作</span></strong>。(只是记录删除操作)</span></p>
</td>
</tr>
</tbody>
</table>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">只有当需要修改时才复制一份数据,这种特性被称作</span> Copy-on-Write<span style="font-family: &quot;微软雅黑&quot;, sans-serif">。可见,容器层保存的是镜像变化的部分,不会对镜像本身进行任何修改。</span></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">这样就解释了我们前面提出的问题:容器层记录对镜像的修改,所有镜像层都是只读的,不会被容器修改,所以镜像可以被多个容器共享。</span></p>
<h2>1.9 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">运行</span>zabbix-server</h2>
<h3>1.9.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器间的互联</span></h3>
<p style="margin-left: 15.75pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在运行</span>zabbix<span style="font-family: &quot;微软雅黑&quot;, sans-serif">之前务必要了解容器间互联的方法</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个nginx容器</span>
docker run -d -p 80:80<span style="color: rgba(0, 0, 0, 1)"> nginx
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 创建容器,做link,并进入容器中</span>
docker run -it --link quirky_brown:web01 centos-ssh /bin/<span style="color: rgba(0, 0, 0, 1)">bash
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 在容器中访问nginx容器可以ping通</span>
ping web01    </pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">命令执行过程</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 启动apache容器</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -d httpd:2.4</span>
<span style="color: rgba(0, 0, 0, 1)">3f1f7fc554720424327286bd2b04aeab1b084a3fb011a785b0deab6a34e56955
</span>^[[A[root@docker01 docker ps -<span style="color: rgba(0, 0, 0, 1)">a
CONTAINER ID      IMAGE               COMMAND            CREATED             STATUS            PORTS               NAMES
3f1f7fc55472      httpd:</span>2.4         <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">httpd-foreground</span><span style="color: rgba(128, 0, 0, 1)">"</span>   6 seconds ago       Up 5 seconds      80/<span style="color: rgba(0, 0, 0, 1)">tcp            determined_clarke
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 拉取一个busybox 镜像</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker pull busybox </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 启动容器</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run -it--link determined_clarke:web busybox:latest   /bin/sh </span>
/ <span style="color: rgba(0, 128, 0, 1)">#</span> <span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 使用新的容器访问最初的web容器</span>
/ <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ping web </span>
PING web (172.17.0.2): 56<span style="color: rgba(0, 0, 0, 1)"> data bytes
</span>64 bytes <span style="color: rgba(0, 0, 255, 1)">from</span> 172.17.0.2: seq=0 ttl=64 time=0.058<span style="color: rgba(0, 0, 0, 1)"> ms
</span>^<span style="color: rgba(0, 0, 0, 1)">C
</span>--- web ping statistics ---
1 packets transmitted, 1 packets received, 0%<span style="color: rgba(0, 0, 0, 1)"> packet loss
round</span>-trip min/avg/max = 0.058/0.058/0.058 ms</pre>
</div>
<h3>1.9.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">启动</span>zabbix<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器</span></h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动一个</span>mysql<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的容器</span></p>
<div class="cnblogs_code">
<pre>docker run --name mysql-server -<span style="color: rgba(0, 0, 0, 1)">t \
      </span>-e MYSQL_DATABASE=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_USER=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_ROOT_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-d mysql:5.7<span style="color: rgba(0, 0, 0, 1)"> \
      </span>--character-set-server=utf8 --collation-server=utf8_bin</pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动</span>java-gateway<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器监控</span>java<span style="font-family: &quot;微软雅黑&quot;, sans-serif">服务</span></p>
<div class="cnblogs_code">
<pre>docker run --name zabbix-java-gateway -<span style="color: rgba(0, 0, 0, 1)">t \
      </span>-d zabbix/zabbix-java-gateway:latest</pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动</span>zabbix-mysql<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器使用</span>link<span style="font-family: &quot;微软雅黑&quot;, sans-serif">连接</span>mysql<span style="font-family: &quot;微软雅黑&quot;, sans-serif">与</span>java-gateway<span style="font-family: &quot;微软雅黑&quot;, sans-serif">。</span></p>
<div class="cnblogs_code">
<pre>docker run --name zabbix-server-mysql -<span style="color: rgba(0, 0, 0, 1)">t \
      </span>-e DB_SERVER_HOST=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mysql-server</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_DATABASE=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_USER=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_ROOT_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e ZBX_JAVAGATEWAY=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix-java-gateway</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>--link mysql-<span style="color: rgba(0, 0, 0, 1)">server:mysql \
      </span>--link zabbix-java-gateway:zabbix-java-<span style="color: rgba(0, 0, 0, 1)">gateway \
      </span>-p 10051:10051<span style="color: rgba(0, 0, 0, 1)"> \
      </span>-d zabbix/zabbix-server-mysql:latest</pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动</span>zabbix web<span style="font-family: &quot;微软雅黑&quot;, sans-serif">显示,使用</span>link<span style="font-family: &quot;微软雅黑&quot;, sans-serif">连接</span>zabbix-mysql<span style="font-family: &quot;微软雅黑&quot;, sans-serif">与</span>mysql<span style="font-family: &quot;微软雅黑&quot;, sans-serif">。</span></p>
<div class="cnblogs_code">
<pre>docker run --name zabbix-web-nginx-mysql -<span style="color: rgba(0, 0, 0, 1)">t \
      </span>-e DB_SERVER_HOST=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mysql-server</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_DATABASE=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_USER=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>-e MYSQL_ROOT_PASSWORD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root_pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
      </span>--link mysql-<span style="color: rgba(0, 0, 0, 1)">server:mysql \
      </span>--link zabbix-server-mysql:zabbix-<span style="color: rgba(0, 0, 0, 1)">server \
      </span>-p 80:80<span style="color: rgba(0, 0, 0, 1)"> \
      </span>-d zabbix/zabbix-web-nginx-mysql:latest</pre>
</div>
<h3>1.9.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">关于</span>zabbix API</h3>
<p style="margin-left: 7.1pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">关于</span>zabbix API<span style="font-family: &quot;微软雅黑&quot;, sans-serif">可以参考官方文档:</span>https://www.zabbix.com/documentation/3.4/zh/manual/api</p>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、获取</span>token<span style="font-family: &quot;微软雅黑&quot;, sans-serif">方法</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取token</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl -s -X POST -H 'Content-Type:application/json' -d '</span>
<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">jsonrpc</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2.0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">method</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">user.login</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">params</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Admin</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zabbix</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
},
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">id</span><span style="color: rgba(128, 0, 0, 1)">"</span>: 1<span style="color: rgba(0, 0, 0, 1)">
}</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> http://10.0.0.100/api_jsonrpc.php</span>
{<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">jsonrpc</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2.0</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">result</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">d3be707f9e866ec5d0d1c242292cbebd</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">id</span><span style="color: rgba(128, 0, 0, 1)">"</span>:1}</pre>
</div>
<h2>1.10 docker <span style="font-family: &quot;微软雅黑&quot;, sans-serif">仓库(</span>registry<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></h2>
<h3>1.10.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建一个普通仓库</span></h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、创建仓库</span></p>
<div class="cnblogs_code">
<pre>docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registryregistry </pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、修改配置文件,使之支持</span>http</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat/etc/docker/daemon.json </span>
<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">registry-mirrors</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://registry.docker-cn.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">],
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insecure-registries</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10.0.0.100:5000</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
}</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">重启</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">让修改生效</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> systemctl restartdocker.service</span></pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、修改镜像标签</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker tagbusybox:latest10.0.0.100:5000/clsn/busybox:1.0</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker images</span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY                      TAG               IMAGE ID            CREATED             SIZE
centos6</span>-ssh                     latest            3c2b1e57a0f5      18<span style="color: rgba(0, 0, 0, 1)"> hours ago      393MB
httpd                           </span>2.4               2e202f453940      6<span style="color: rgba(0, 0, 0, 1)"> days ago          179MB
</span>10.0.0.100:5000/clsn/busybox    1.0               5b0d59026729      8 days ago          1.15MB</pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、将新打标签的镜像上传镜像到仓库</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker push   10.0.0.100:5000/clsn/busybox</span></pre>
</div>
<h3>1.10.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">带</span>basic<span style="font-family: &quot;微软雅黑&quot;, sans-serif">认证的仓库</span></h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、安装加密工具</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> yum install httpd-tools-y</span></pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、设置认证密码</span></p>
<div class="cnblogs_code">
<pre>mkdir /opt/registry-var/auth/ -<span style="color: rgba(0, 0, 0, 1)">p
htpasswd</span>-Bbn clsn 123456&gt; /opt/registry-var/auth/htpasswd</pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动容器,在启动时传入认证参数</span></p>
<div class="cnblogs_code">
<pre>docker run -d -p 5000:5000 -v /opt/registry-var/auth/:/auth/ -e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">REGISTRY_AUTH=htpasswd</span><span style="color: rgba(128, 0, 0, 1)">"</span> -e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm</span><span style="color: rgba(128, 0, 0, 1)">"</span> -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry</pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、使用验证用户测试</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 登陆用户</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker login 10.0.0.100:5000 </span>
<span style="color: rgba(0, 0, 0, 1)">Username: clsn
Password: </span>123456<span style="color: rgba(0, 0, 0, 1)">
Login Succeeded
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 推送镜像到仓库</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker push 10.0.0.100:5000/clsn/busybox </span>
The push refers to repository
4febd3792a1f: Pushed
</span>1.0: digest: sha256:4cee1979ba0bf7db9fc5d28fb7b798ca69ae95a47c5fecf46327720df4ff352d size: 527
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">认证文件的保存位置</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat .docker/config.json </span>
<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">auths</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10.0.0.100:5000</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
            </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">auth</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Y2xzbjoxMjM0NTY=</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      },
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://index.docker.io/v1/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
            </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">auth</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Y2xzbjpIenNAMTk5Ng==</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      }
    },
    </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">HttpHeaders</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
      </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">User-Agent</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Docker-Client/17.12.0-ce (linux)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    }
}</span></pre>
</div>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">至此,一个简单的</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">镜像仓库搭建完成</span></p>
<h2>1.11 docker-compose<span style="font-family: &quot;微软雅黑&quot;, sans-serif">编排工具</span></h2>
<h3>1.11.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装</span>docker-compose</h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装</span>docker-compose</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 下载pip软件</span>
yum install -y python2-<span style="color: rgba(0, 0, 0, 1)">pip
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 下载 docker-compose</span>
pip install docker-compose</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">国内开启</span>pip <span style="font-family: &quot;微软雅黑&quot;, sans-serif">下载加速:</span>http://mirrors.aliyun.com/help/pypi</p>
<div class="cnblogs_code">
<pre>mkdir ~/.pip/<span style="color: rgba(0, 0, 0, 1)">
cat </span>&gt; ~/.pip/pip.conf &lt;&lt;<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">EOF</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
[</span><span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)">]
index</span>-url = https://mirrors.aliyun.com/pypi/simple/<span style="color: rgba(0, 0, 0, 1)">

trusted</span>-host=<span style="color: rgba(0, 0, 0, 1)">mirrors.aliyun.com
EOF</span></pre>
</div>
<h3>1.11.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">编排启动镜像</span></h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、创建文件目录</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mkdir /opt/my_wordpress/</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cd /opt/my_wordpress/</span></pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、编写编排文件</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim docker-compose.yml</span>
version: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
services:
   db:
   image: mysql:</span>5.7<span style="color: rgba(0, 0, 0, 1)">
   volumes:
       </span>- /data/db_data:/var/lib/<span style="color: rgba(0, 0, 0, 1)">mysql
   restart: always
   environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
   depends_on:
       </span>-<span style="color: rgba(0, 0, 0, 1)"> db
   image: wordpress:latest
   volumes:
       </span>- /data/web_data:/var/www/<span style="color: rgba(0, 0, 0, 1)">html
   ports:
       </span>- <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8000:80</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
   restart: always
   environment:
       WORDPRESS_DB_HOST: db:</span>3306<span style="color: rgba(0, 0, 0, 1)">
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress</span></pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker-compose up</span>
  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">启动方法:docker-compose up</span>
  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">后台启动方法:docker-compose up -d</span></pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、浏览器上访问http://10.0.0.100:8000</span></p>
<p>&nbsp;<strong>&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">进行</span>wordpress<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的安装即可</span></strong></p>
<h3>1.11.3 haproxy<span style="font-family: &quot;微软雅黑&quot;, sans-serif">代理后端</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器</span></h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、修改编排脚本</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat docker-compose.yml </span>
version: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
services:
   db:
   image: mysql:</span>5.7<span style="color: rgba(0, 0, 0, 1)">
   volumes:
       </span>- /data/db_data:/var/lib/<span style="color: rgba(0, 0, 0, 1)">mysql
   restart: always
   environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
   depends_on:
       </span>-<span style="color: rgba(0, 0, 0, 1)"> db
   image: wordpress:latest
   volumes:
       </span>- /data/web_data:/var/www/<span style="color: rgba(0, 0, 0, 1)">html
   ports:
       </span>- <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">80</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
   restart: always
   environment:
       WORDPRESS_DB_HOST: db:</span>3306<span style="color: rgba(0, 0, 0, 1)">
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress</span></pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、同时启动<strong>两台</strong></span>wordpress</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker-compose scale wordpress=2 </span>
WARNING: The scale command <span style="color: rgba(0, 0, 255, 1)">is</span> deprecated. Use the up command with the --<span style="color: rgba(0, 0, 0, 1)">scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done</span></pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、安装</span>haproxy</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> yum install haproxy -y</span></pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、修改</span>haproxy<span style="font-family: &quot;微软雅黑&quot;, sans-serif">配置文件</span></p>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">关于配置文件的详细说明,参考:</span>https://www.cnblogs.com/MacoLee/p/5853413.html</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">cp /etc/haproxy/haproxy.cfg{,.bak}</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim /etc/haproxy/haproxy.cfg</span>
<span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)">
    log         </span>127.0.0.1<span style="color: rgba(0, 0, 0, 1)"> local2
    chroot      </span>/var/lib/<span style="color: rgba(0, 0, 0, 1)">haproxy
    pidfile   </span>/var/run/<span style="color: rgba(0, 0, 0, 1)">haproxy.pid
    maxconn   </span>4000<span style="color: rgba(0, 0, 0, 1)">
    user      haproxy
    group       haproxy
    daemon
    stats socket </span>/var/lib/haproxy/stats level admin<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">支持命令行控制</span>
<span style="color: rgba(0, 0, 0, 1)">defaults
    mode                  http
    log                     </span><span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)">
    option                  httplog
    option                  dontlognull
    option http</span>-server-<span style="color: rgba(0, 0, 0, 1)">close
    option forwardfor       </span><span style="color: rgba(0, 0, 255, 1)">except</span> 127.0.0.0/8<span style="color: rgba(0, 0, 0, 1)">
    option                  redispatch
    retries               </span>3<span style="color: rgba(0, 0, 0, 1)">
    timeout http</span>-<span style="color: rgba(0, 0, 0, 1)">request    10s
    timeout queue         1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http</span>-keep-<span style="color: rgba(0, 0, 0, 1)">alive 10s
    timeout check         10s
    maxconn               </span>3000<span style="color: rgba(0, 0, 0, 1)">
listen stats
    mode http
    bind </span>0.0.0.0:8888<span style="color: rgba(0, 0, 0, 1)">
    stats enable
    stats uri   </span>/haproxy-<span style="color: rgba(0, 0, 0, 1)">status
    stats auth    admin:</span>123456<span style="color: rgba(0, 0, 0, 1)">
frontend frontend_www_example_com
    bind </span>10.0.0.100:8000<span style="color: rgba(0, 0, 0, 1)">
    mode http
    option httplog
    log </span><span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)">
    default_backend backend_www_example_com
backend backend_www_example_com
    option forwardfor header X</span>-REAL-<span style="color: rgba(0, 0, 0, 1)">IP
    option httpchk HEAD </span>/ HTTP/1.0<span style="color: rgba(0, 0, 0, 1)">
    balance roundrobin
    server web</span>-node110.0.0.100:32768 check inter 2000 rise 30 fall 15<span style="color: rgba(0, 0, 0, 1)">
    server web</span>-node210.0.0.100:32769 check inter 2000 rise 30 fall 15</pre>
</div>
<p>5<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、启动</span>haproxy</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">systemctl start haproxy
systemctl enable haproxy</span></pre>
</div>
<p>6<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、使用浏览器访问</span>hapeoxy<span style="font-family: &quot;微软雅黑&quot;, sans-serif">监听的</span>8000<span style="font-family: &quot;微软雅黑&quot;, sans-serif">端口可以看到负载的情况</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180443968-707439565.png" alt=""></p>
<p>7<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、使用浏览器访问</span> http://10.0.0.100:8888/haproxy-status</p>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">可以看到后端节点的监控状况,</span></p>
<p style="margin: 0 0 0.0001pt; text-align: center; layout-grid-mode: both" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180452109-1127111900.png" alt="">&nbsp;</p>
<h3>1.11.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装</span>socat <span style="font-family: &quot;微软雅黑&quot;, sans-serif">直接操作</span>socket<span style="font-family: &quot;微软雅黑&quot;, sans-serif">控制</span>haproxy</h3>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、安装软件</span></p>
<div class="cnblogs_code">
<pre>yum install socat.x86_64 -y</pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、查看帮助</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "help"|socat stdio /var/lib/haproxy/stats</span></pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、下线后端节点</span></p>
<div class="cnblogs_code">
<pre>echo <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">disable server backend_www_example_com/web-node2</span><span style="color: rgba(128, 0, 0, 1)">"</span>|socat stdio /var/lib/haproxy/stats</pre>
</div>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、上线后端节点</span></p>
<div class="cnblogs_code">
<pre>echo <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">enable server backend_www_example_com/web-node3</span><span style="color: rgba(128, 0, 0, 1)">"</span>|socat stdio /var/lib/haproxy/stats</pre>
</div>
<p>5<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、编写</span>php<span style="font-family: &quot;微软雅黑&quot;, sans-serif">测试页,放到</span>/data/web_data<span style="font-family: &quot;微软雅黑&quot;, sans-serif">下,在浏览器中访问可以查看当前的节点</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim check.php</span>
&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;PHP测试&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;?php<span style="color: rgba(0, 0, 255, 1)">echo</span> '&lt;p&gt;Hello World &lt;/p&gt;'; ?&gt;
      &lt;?php<span style="color: rgba(0, 0, 255, 1)">echo</span> "访问的服务器地址是:"."&lt;fontcolor=red&gt;".<span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['SERVER_ADDR']."&lt;/font&gt;"."&lt;br&gt;"<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">echo</span>"访问的服务器域名是:"."&lt;fontcolor=red&gt;".<span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['SERVER_NAME']."&lt;/font&gt;"."&lt;br&gt;"<span style="color: rgba(0, 0, 0, 1)">;
      </span>?&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<h2><span style="background: rgba(255, 255, 0, 1)">1.12 </span><span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(255, 255, 0, 1)">重启</span><span style="background: rgba(255, 255, 0, 1)">docker</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(255, 255, 0, 1)">服务,容器全部退出的解决办法</span></h2>
<h3>1.12.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">在启动是指定自动重启</span></h3>
<div class="cnblogs_code">
<pre>docker run--restart=always</pre>
</div>
<h3>1.12.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">修改</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">默认配置文件</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 添加上下面这行</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">live-restore</span><span style="color: rgba(128, 0, 0, 1)">"</span>: true</pre>
</div>
<p>docker server<span style="font-family: &quot;微软雅黑&quot;, sans-serif">配置文件</span>/etc/docker/daemon.json<span style="font-family: &quot;微软雅黑&quot;, sans-serif">参考</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat/etc/docker/daemon.json </span>
<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">registry-mirrors</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://registry.docker-cn.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">],
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">graph</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/opt/mydocker</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 修改数据的存放目录到/opt/mydocker/,原/var/lib/docker/</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insecure-registries</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10.0.0.100:5000</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">],
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">live-restore</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: true
}</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">重启生效,只对在此之后启动的容器生效</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> systemctl restartdocker.service</span></pre>
</div>
<h2>1.13 Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">网络类型</span></h2>
<p style="text-align: center" align="center">&nbsp;<img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180512750-529190946.png" alt=""></p>
<h3>1.13.1 docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的网络类型</span></h3>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 16.22%; border-top: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: 1pt solid rgba(155, 187, 89, 1); border-right: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="16%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">类型</span></strong></p>
</td>
<td style="width: 83.78%; border-top: 1pt solid rgba(155, 187, 89, 1); border-right: 1pt solid rgba(155, 187, 89, 1); border-bottom: 1pt solid rgba(155, 187, 89, 1); border-left: none; background: rgba(155, 187, 89, 1); padding: 0 5.4pt" width="83%">
<p style="text-align: center" align="center"><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif; color: rgba(255, 255, 255, 1)">说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 16.22%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="16%">
<p style="text-align: center" align="center"><strong>None</strong></p>
</td>
<td style="width: 83.78%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">不为容器配置任何网络功能,没有网络</span> --net=none</p>
</td>
</tr>
<tr>
<td style="width: 16.22%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="16%">
<p style="text-align: center" align="center"><strong>Container</strong></p>
</td>
<td style="width: 83.78%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">与另一个运行中的容器共享</span>Network Namespace<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,</span>--net=container:containerID</p>
</td>
</tr>
<tr>
<td style="width: 16.22%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="16%">
<p style="text-align: center" align="center"><strong>Host</strong></p>
</td>
<td style="width: 83.78%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); background: rgba(234, 241, 221, 1); padding: 0 5.4pt" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">与主机共享</span>Network Namespace<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,</span>--net=host</p>
</td>
</tr>
<tr>
<td style="width: 16.22%; border-right: 1pt solid rgba(194, 214, 155, 1); border-bottom: 1pt solid rgba(194, 214, 155, 1); border-left: 1pt solid rgba(194, 214, 155, 1); border-top: none; padding: 0 5.4pt" width="16%">
<p style="text-align: center" align="center"><strong>Bridge</strong></p>
</td>
<td style="width: 83.78%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(194, 214, 155, 1); border-right: 1pt solid rgba(194, 214, 155, 1); padding: 0 5.4pt" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph">Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">设计的</span>NAT<span style="font-family: &quot;微软雅黑&quot;, sans-serif">网络模型<strong><span style="color: rgba(146, 208, 80, 1)">(默认类型)</span></strong></span></p>
</td>
</tr>
</tbody>
</table>
<p style="text-indent: 21pt"><strong>Bridge</strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">默认</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">网络隔离基于网络命名空间,在物理机上创建</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器时会为每一个</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器分配网络命名空间,并且把容器</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">桥接到物理机的虚拟网桥上。</span></p>
<h3>1.13.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">不为容器配置网络功能</span></h3>
<p style="text-indent: 15.75pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">此模式下创建容器是不会为容器配置任何网络参数的,如:容器网卡、</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、通信路由等,全部需要自己去配置。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-it --network none busybox:latest/bin/sh </span>
/ <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ip a</span>
1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536<span style="color: rgba(0, 0, 0, 1)"> qdisc noqueue
    link</span>/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<span style="color: rgba(0, 0, 0, 1)">
    inet </span>127.0.0.1/8<span style="color: rgba(0, 0, 0, 1)"> scope host lo
       valid_lft forever preferred_lft forever</span></pre>
</div>
<h3>1.13.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">与其他容器共享网络配置</span>(Container<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></h3>
<p style="text-indent: 15.75pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">此模式和</span>host<span style="font-family: &quot;微软雅黑&quot;, sans-serif">模式很类似,只是此模式创建容器共享的是其他容器的</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">和端口而不是物理机,此模式容器自身是不会配置网络和端口,创建此模式容器进去后,你会发现里边的</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">是你所指定的那个容器</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">并且端口也是共享的,而且其它还是<strong><span style="color: rgba(0, 176, 240, 1)">互相隔离</span></strong>的,如进程等。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-it --network container:mywordpress_db_1busybox:latest/bin/sh </span>
/ <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ip a</span>
1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536<span style="color: rgba(0, 0, 0, 1)"> qdisc noqueue
    link</span>/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<span style="color: rgba(0, 0, 0, 1)">
    inet </span>127.0.0.1/8<span style="color: rgba(0, 0, 0, 1)"> scope host lo
       valid_lft forever preferred_lft forever
</span>105: eth0@if106: &lt;BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN&gt; mtu 1500<span style="color: rgba(0, 0, 0, 1)"> qdisc noqueue
    link</span>/ether 02:42:ac:12:00:03<span style="color: rgba(0, 0, 0, 1)"> brd ff:ff:ff:ff:ff:ff
    inet </span>172.18.0.3/16 brd 172.18.255.255 scope <span style="color: rgba(0, 0, 255, 1)">global</span><span style="color: rgba(0, 0, 0, 1)"> eth0
       valid_lft forever preferred_lft forever</span></pre>
</div>
<h3>1.13.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用宿主机网络</span></h3>
<p style="text-indent: 15.75pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">此模式创建的容器没有自己独立的网络命名空间,是和物理机共享一个</span>Network Namespace<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,并且共享物理机的所有端口与</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,并且这个<span style="text-decoration: underline">模式认为是不安全的</span>。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-it --network hostbusybox:latest/bin/sh</span></pre>
</div>
<h3>1.13.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">查看网络列表</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker network list </span>
<span style="color: rgba(0, 0, 0, 1)">NETWORK ID          NAME                  DRIVER            SCOPE
b15e8a720d3b      bridge                bridge            local
345d65b4c2a0      host                  host                local
bc5e2a32bb55      mywordpress_default   bridge            local
ebf76eea91bb      none                  null                local</span></pre>
</div>
<h3>1.13.6 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">用</span>PIPEWORK<span style="font-family: &quot;微软雅黑&quot;, sans-serif">为</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器配置独立</span>IP</h3>
<p style="margin-left: 42pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">参考文档:</span>http://blog.csdn.net/design321/article/details/48264825</p>
<p style="margin-left: 42pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">官方网站:</span>https://github.com/jpetazzo/pipework</p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">宿主环境:</span>centos7.2</p>
<p><strong>1</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">、安装</span>pipework</strong></p>
<div class="cnblogs_code">
<pre>wget https://github.com/jpetazzo/pipework/archive/<span style="color: rgba(0, 0, 0, 1)">master.zip
unzip master.zip
cp pipework</span>-master/pipework/usr/local/bin/<span style="color: rgba(0, 0, 0, 1)">
chmod </span>+x /usr/local/bin/pipework</pre>
</div>
<p><strong>2</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">、配置桥接网卡</span></strong></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">安装桥接工具</span></p>
<div class="cnblogs_code">
<pre>yum install bridge-utils.x86_64 -y</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">修改网卡配置,实现桥接</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 修改eth0配置,让br0实现桥接</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/sysconfig/network-scripts/ifcfg-eth0 </span>
TYPE=<span style="color: rgba(0, 0, 0, 1)">Ethernet
BOOTPROTO</span>=<span style="color: rgba(0, 0, 0, 1)">static
NAME</span>=<span style="color: rgba(0, 0, 0, 1)">eth0
DEVICE</span>=<span style="color: rgba(0, 0, 0, 1)">eth0
ONBOOT</span>=<span style="color: rgba(0, 0, 0, 1)">yes
BRIDGE</span>=<span style="color: rgba(0, 0, 0, 1)">br0
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/sysconfig/network-scripts/ifcfg-br0 </span>
TYPE=<span style="color: rgba(0, 0, 0, 1)">Bridge
BOOTPROTO</span>=<span style="color: rgba(0, 0, 0, 1)">static
NAME</span>=<span style="color: rgba(0, 0, 0, 1)">br0
DEVICE</span>=<span style="color: rgba(0, 0, 0, 1)">br0
ONBOOT</span>=<span style="color: rgba(0, 0, 0, 1)">yes
IPADDR</span>=10.0.0.100<span style="color: rgba(0, 0, 0, 1)">
NETMASK</span>=255.255.255.0<span style="color: rgba(0, 0, 0, 1)">
GATEWAY</span>=10.0.0.254<span style="color: rgba(0, 0, 0, 1)">
DNS1</span>=223.5.5.5
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 重启网络</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> /etc/init.d/network restart</span></pre>
</div>
<p><strong>3</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">、运行一个容器镜像测试:</span></strong></p>
<div class="cnblogs_code">
<pre>pipework br0 $(docker run -d -it -p 6880:80 --namehttpd_pw httpd) 10.0.0.220/24@10.0.0.254</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">在其他主机上测试端口及连通性</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.220</span>
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ping 10.0.0.220 -c 1</span>
PING 10.0.0.220 (10.0.0.220) 56(84<span style="color: rgba(0, 0, 0, 1)">) bytes of data.
</span>64 bytes <span style="color: rgba(0, 0, 255, 1)">from</span> 10.0.0.220: icmp_seq=1 ttl=64 time=0.043 ms</pre>
</div>
<p><strong>4</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">、再运行一个容器,设置网路类型为</span>none</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">:</span></strong></p>
<div class="cnblogs_code">
<pre>pipework br0 $(docker run -d -it --net=none --name test httpd:2.4) 10.0.0.221/24@10.0.0.254</pre>
</div>
<p>&nbsp;&nbsp; <span style="font-family: &quot;微软雅黑&quot;, sans-serif">进行访问测试</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> curl 10.0.0.221</span>
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre>
</div>
<p><strong>5</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">、重启容器后需要再次指定:</span></strong></p>
<div class="cnblogs_code">
<pre>pipework br0 testduliip172.16.146.113/24@172.16.146.1<span style="color: rgba(0, 0, 0, 1)">
pipework br0 testduliip01</span>172.16.146.112/24@172.16.146.1</pre>
</div>
<p style="text-align: right" align="right">Dcoker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">跨主机通信之</span>overlay<span style="font-family: &quot;微软雅黑&quot;, sans-serif">可以参考:</span></p>
<p style="text-align: right" align="right">http://www.cnblogs.com/CloudMan6/p/7270551.html</p>
<h3>1.13.7 Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">跨主机通信之</span>macvlan</h3>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建网络</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker networkcreate --driver macvlan--subnet 10.1.0.0/24 --gateway 10.1.0.254 -o parent=eth0macvlan_1</span>
33a1f41dcc074f91b5bd45e7dfedabfb2b8ec82db16542f05213839a119b62ca</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">设置网卡为混杂模式</span></p>
<div class="cnblogs_code">
<pre>ip link set eth0 promisc on</pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">创建使用</span>macvlan<span style="font-family: &quot;微软雅黑&quot;, sans-serif">网络容器</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker run-it --network macvlan_1--ip=10.1.0.222 busybox /bin/sh</span></pre>
</div>
<h2>1.14 docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">企业级镜像仓库</span>harbor</h2>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器管理</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> pwd</span>
/opt/<span style="color: rgba(0, 0, 0, 1)">harbor
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker-compose stop</span></pre>
</div>
<p>1<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、安装</span>docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、</span>docker-compose</p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">下载</span> harbor</p>
<div class="cnblogs_code">
<pre>cd /opt &amp;&amp; https://storage.googleapis.com/harbor-releases/harbor-offline-installer-v1.3.0<span style="color: rgba(0, 0, 0, 1)">.tgz
tar xf harbor</span>-offline-installer-v1.3.0.tgz</pre>
</div>
<p>2<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、修改主机及</span>web<span style="font-family: &quot;微软雅黑&quot;, sans-serif">界面密码</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim harbor.cfg </span>
<span style="color: rgba(0, 0, 0, 1)">    ···
    hostname </span>= 10.0.0.100<span style="color: rgba(0, 0, 0, 1)">
    harbor_admin_password </span>=<span style="color: rgba(0, 0, 0, 1)"> Harbor12345
    ···</span></pre>
</div>
<p>3<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、执行安装脚本</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ./install.sh</span></pre>
</div>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">浏览器访问</span> http://10.0.0.11</p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180540421-2093222305.png" alt="">&nbsp;</p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">添加一个项目</span></p>
<p style="text-align: center" align="center">&nbsp;<img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180546390-776847142.png" alt=""></p>
<p>4<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、镜像推送到仓库的指定项目</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> dockertag centos:6.810.0.0.100/clsn/centos6.8:1.0</span>
<span style="color: rgba(0, 128, 0, 1)">#</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker images </span>
<span style="color: rgba(0, 0, 0, 1)">REPOSITORY                  TAG               IMAGE ID            CREATED             SIZE
busybox                     latest            5b0d59026729      </span>8 days ago          1<span style="color: rgba(0, 0, 0, 1)">.15MB
</span>10.0.0.100/clsn/centos6.8   1.0               6704d778b3ba      2<span style="color: rgba(0, 0, 0, 1)"> months ago      195MB
centos                      </span>6.8               6704d778b3ba      2<span style="color: rgba(0, 0, 0, 1)"> months ago      195MB
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker login 10.0.0.100</span>
<span style="color: rgba(0, 0, 0, 1)">Username: admin
Password:
Login Succeeded</span></pre>
</div>
<p>5<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、推送镜像</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> docker push 10.0.0.100/clsn/centos6.8 </span>
The push refers to repository
e00c9229b481: Pushing</span>13.53MB/194.5MB</pre>
</div>
<p>6<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、在</span>web<span style="font-family: &quot;微软雅黑&quot;, sans-serif">界面里查看</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201802/1190037-20180203180555281-1909245260.png" alt="">&nbsp;</p>
<h3>1.14.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用容器的建议</span></h3>
<p style="margin-left: 42pt; text-indent: -21pt">1.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要以拆分方式进行应用程序发布</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">2.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要创建大型镜像</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">3.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要在单个容器中运行多个进程</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">4.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要再镜像内保存凭证,不要依赖</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">地址</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">5.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">以非</span>root<span style="font-family: &quot;微软雅黑&quot;, sans-serif">用户运行进程</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">6.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要使用“最新”标签</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">7.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要利用运行中的容器创建镜像</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">8.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要使用单层镜像</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">9.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">不要将数据存放在容器内</span></p>
<h3>1.14.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">关于</span>Docker<span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的监控</span></h3>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的基本信息</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">包括容器的数量、</span>ID<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、名称、镜像、启动命令、端口等信息</span></p>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的运行状态</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">统计各状态的容器的数量,包括运行中、暂停、停止及异常退出</span></p>
<p><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">容器的用量信息</span></strong></p>
<p style="text-indent: 21pt"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">统计容器的</span>CPU<span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用率、内存使用量、块设备</span>I/O<span style="font-family: &quot;微软雅黑&quot;, sans-serif">使用量、网络使用情况等资源的使用情况</span></p>
<h2>1.15 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">参考文献</span></h2>
<blockquote>
<p>&nbsp;&nbsp;https://www.redhat.com/zh/topics/containers/whats-a-linux-container</p>
<p>&nbsp;&nbsp;https://www.redhat.com/zh/topics/containers/what-is-docker</p>
<p><span style="background-color: initial">&nbsp;</span>http://blog.51cto.com/dihaifeng/1713512</p>
<p>&nbsp;&nbsp;https://www.cnblogs.com/Bourbon-tian/p/6867796.html</p>
<p>&nbsp;&nbsp;https://www.cnblogs.com/CloudMan6/p/6806193.html</p>
</blockquote>

</div>
<div id="MySignature" role="contentinfo">
    <div style="padding: 5px; background: #ff9; border: solid 1px #ccc">
作者:惨绿少年
<br>
出处:http://clsn.io
<br>
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
<br>
</div><br><br>
来源:https://www.cnblogs.com/clsn/p/8410309.html
頁: [1]
查看完整版本: Docker 容器入门