<h3 id="docker-1" style="box-sizing: border-box; margin: 0.85rem 0px 1.7rem; text-rendering: optimizeLegibility; font-size: 2.1rem; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; line-height: 50.4px; letter-spacing: -1px; color: rgb(85, 85, 85);">Docker仓库</h3>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 使用类似 git 的方式管理镜像。通过基本的镜像可以定制创建出来不同种应用的 Docker 镜像。</span><a href="https://registry.hub.docker.com/" style="box-sizing: border-box; text-decoration: none; color: rgb(22, 148, 202); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Docker Hub</a><span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;"> 是 Docker 官方提供的镜像中心。在这里可以很方便地找到各类应用、环境的镜像。由于 Docker 使用联合文件系统,所以镜像就像是夹心饼干一样一层层构成,相同底层的镜像可以共享。所以 Docker 还是相当节约磁盘空间的。要使用一 个镜像,需要先从远程的镜像注册中心拉取,这点非常类似 git。 </span><span style="color: rgb(35, 119, 148); font-family: Consolas, Monaco, monospace; font-size: 1rem; line-height: 1.5em; letter-spacing: -0.48px; background-color: inherit;">docker pull ubuntu</span>
安装
CentOS7
1.<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">使用 yum 从软件仓库安装 Docker:</span><span style="color: rgb(35, 119, 148); font-family: Consolas, Monaco, monospace; font-size: 1rem; line-height: 1.5em; letter-spacing: -0.48px; background-color: inherit;">yum install docker</span>
2.<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">首先启动 Docker 的守护进程:</span><span style="color: rgb(35, 119, 148); font-family: Consolas, Monaco, monospace; font-size: 1rem; line-height: 1.5em; letter-spacing: -0.48px; background-color: inherit;">service docker start</span>
3.<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">如果想要 Docker 在系统启动时运行,执行:chkconfig docker on</span>
<span style="box-sizing: border-box; font-weight: 600; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 在 CentOS 上好像和防火墙有冲突,应用防火墙规则后可能导致 Docker 无法联网,重启 Docker 可以解决</span><span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">。</span>
<h3 id="-2" style="box-sizing: border-box; margin: 0.85rem 0px 1.7rem; text-rendering: optimizeLegibility; font-size: 2.1rem; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; line-height: 50.4px; letter-spacing: -1px; color: rgb(85, 85, 85);">运行一个容器</h3>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">使用 Docker 最关键的一步就是从镜像创建容器。有两种方式可以创建一个容器:使用 </span><code style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(156, 29, 61); padding: 0.2rem 0.4rem; border-radius: 3px; letter-spacing: -0.48px; line-height: 27.2px; background: rgb(249, 242, 244);">docker create</code><span class="copy-to-clipboard" title="Copy to clipboard" style="box-sizing: border-box; width: 27px; height: 1.4rem; top: -1px; display: inline-block; vertical-align: middle; position: relative; color: rgb(60, 60, 60); margin-left: -0.2rem; cursor: pointer; border-radius: 0px 2px 2px 0px; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px; background-image: url("../images/clippy.svg"); background-color: rgb(249, 242, 244); background-size: 16px 16px; background-position: 50% 50%; background-repeat: no-repeat;"></span><span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;"> 命令创建容器,或者使用 </span><code style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(156, 29, 61); padding: 0.2rem 0.4rem; border-radius: 3px; letter-spacing: -0.48px; line-height: 27.2px; background: rgb(249, 242, 244);">docker run</code><span class="copy-to-clipboard" title="Copy to clipboard" style="box-sizing: border-box; width: 27px; height: 1.4rem; top: -1px; display: inline-block; vertical-align: middle; position: relative; color: rgb(60, 60, 60); margin-left: -0.2rem; cursor: pointer; border-radius: 0px 2px 2px 0px; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px; background-image: url("../images/clippy.svg"); background-color: rgb(249, 242, 244); background-size: 16px 16px; background-position: 50% 50%; background-repeat: no-repeat;"></span><span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;"> 命令运行一个新容器。两个命令并没有太大差别,只是前者创建后并不会立即启动容器。</span>
<h3 id="-6" style="box-sizing: border-box; margin: 0.85rem 0px 1.7rem; text-rendering: optimizeLegibility; font-size: 2.1rem; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; line-height: 50.4px; letter-spacing: -1px; color: rgb(85, 85, 85);">创建镜像</h3>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 强大的威力在于可以把自己开发的应用随同各种依赖环境一起打包、分发、运行。要创建一个新的 Docker 镜像,通常基于一个已有的 Docker 镜像来创建。Docker 提供了两种方式来创建镜像:把容器创建为一个新的镜像、使用 Dockerfile 创建镜像。</span>
子主题
概念
镜像
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">我们可以理解为一个预配置的系统光盘,这个光盘插入电脑后就可以启动一个操作系统。当然由于是光盘,所以你无法修改它或者保存数据,每次重启都是一个原样全新的系统。Docker 里面镜像基本上和这个差不多。</span>
<span style="box-sizing: border-box; font-weight: 600; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">容器</span>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">同样一个镜像,我们可以同时启动运行多个,运行期间的产生的这个实例就是容器。把容器内的操作和启动它的镜像进行合并,就可以产生一个新的镜像。</span>
it's what?
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 是一种容器技术,它可以将应用和环境等进行打包,形成一个独立的,类似于 iOS 的 APP 形式的「应用」,这个应用可以直接被分发到任意一个支持 Docker 的环境中,通过简单的命令即可启动运行。Docker 是一种最流行的容器化实现方案。和虚拟化技术类似,它极大的方便了应用服务的部署;又与虚拟化技术不同,它以一种更轻量的方式实现了应用服务的打包。使用 Docker 可以让每个应用彼此相互隔离,在同一台机器上同时运行多个应用,不过他们彼此之间共享同一个操作系统。Docker 的优势在于,它可以在更细的粒度上进行资源的管理,也比虚拟化技术更加节约资源。</span>
<h3 id="-3" style="box-sizing: border-box; margin: 0.85rem 0px 1.7rem; text-rendering: optimizeLegibility; font-size: 2.1rem; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; line-height: 50.4px; letter-spacing: -1px; color: rgb(85, 85, 85);">存储</h3>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">在 Docker 容器运行期间,对文件系统的所有修改都会以增量的方式反映在容器使用的联合文件系统中,</span><span style="box-sizing: border-box; font-weight: 600; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">并不是真正的对只读层数据信息修改</span><span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">。每次运行容器对它的修改,都可以理解成对夹心饼干又添加了一层奶油。这层奶油仅供当前容器使用。当删除 Docker 容器,或通过该镜像重新启动时,之前的更改将会丢失。这样做并不便于我们持久化和共享数据。Docker 的数据卷这个东西可以帮到我们。</span>
<h3 id="-4" style="box-sizing: border-box; margin: 0.85rem 0px 1.7rem; text-rendering: optimizeLegibility; font-size: 2.1rem; font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; line-height: 50.4px; letter-spacing: -1px; color: rgb(85, 85, 85);">网络</h3>
<span style="color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 容器内的系统工作起来就像是一个虚拟机,容器内开放的端口并不会真正开放在主机上。可以使我们的容器更加安全,而且不会产生容器间端口的争用。想要将 Docker 容器的端口开放到主机上,可以使用类似端口映射的方式。</span>
<p style="box-sizing: border-box; margin: 1.7rem 0px; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">Docker 容器内的系统工作起来就像是一个虚拟机,容器内开放的端口并不会真正开放在主机上。可以使我们的容器更加安全,而且不会产生容器间端口的争用。想要将 Docker 容器的端口开放到主机上,可以使用类似端口映射的方式。</p><p style="box-sizing: border-box; margin: 1.7rem 0px; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">在 Docker 容器创建时,通过指定 <code style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(156, 29, 61); padding: 0.2rem 0.4rem; border-radius: 3px; background: rgb(249, 242, 244);">-p</code> 参数可以暴露容器的端口在主机上:</p><pre style="box-sizing: border-box; overflow: auto; margin-top: 2rem; margin-bottom: 2rem; padding: 1rem; position: relative; font-family: Consolas, Monaco, monospace; border: 1px solid rgb(221, 221, 221); border-radius: 2px; line-height: 1.15; font-size: 16px; color: rgb(85, 85, 85); letter-spacing: -0.48px; background: rgb(246, 246, 246);"><code class="language-shell" style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(35, 119, 148); padding: 0.2rem 0.4rem; border-radius: 3px; font-size: 1rem; line-height: 1.5em; background: inherit;">docker run -it -p 22 ubuntu sh -c '/bin/bash'</code><span class="copy-to-clipboard" title="Copy to clipboard" style="box-sizing: border-box; width: 27px; height: 1.4rem; top: 4px; display: inline-block; vertical-align: middle; position: absolute; color: rgb(60, 60, 60); margin-left: -0.2rem; cursor: pointer; border-radius: 2px; right: 4px; border-color: rgb(221, 221, 221); background-image: url("../images/clippy.svg"); background-color: rgb(238, 238, 238); background-size: 16px 16px; background-position: 50% 50%; background-repeat: no-repeat;"></span></pre><p style="box-sizing: border-box; margin: 1.7rem 0px; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">现在我们就将容器的 22 端口开放在了主机上,注意主机上对应端口是自动分配的。如果想要指定某个端口,可以通过 <code style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(156, 29, 61); padding: 0.2rem 0.4rem; border-radius: 3px; background: rgb(249, 242, 244);">-p [主机端口]:[容器端口]</code><span class="copy-to-clipboard" title="Copy to clipboard" style="box-sizing: border-box; width: 27px; height: 1.4rem; top: -1px; display: inline-block; vertical-align: middle; position: relative; color: rgb(60, 60, 60); margin-left: -0.2rem; cursor: pointer; border-radius: 0px 2px 2px 0px; background-image: url("../images/clippy.svg"); background-color: rgb(249, 242, 244); background-size: 16px 16px; background-position: 50% 50%; background-repeat: no-repeat;"></span> 参数指定。</p><p style="box-sizing: border-box; margin: 1.7rem 0px; color: rgb(85, 85, 85); font-family: "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif; letter-spacing: -0.48px; line-height: 27.2px;">容器和容器之间想要网络通讯,可以直接使用 <code style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(156, 29, 61); padding: 0.2rem 0.4rem; border-radius: 3px; background: rgb(249, 242, 244);">--link</code><span class="copy-to-clipboard" title="Copy to clipboard" style="box-sizing: border-box; width: 27px; height: 1.4rem; top: -1px; display: inline-block; vertical-align: middle; position: relative; color: rgb(60, 60, 60); margin-left: -0.2rem; cursor: pointer; border-radius: 0px 2px 2px 0px; background-image: url("../images/clippy.svg"); background-color: rgb(249, 242, 244); background-size: 16px 16px; background-position: 50% 50%; background-repeat: no-repeat;"></span> 参数将两个容器连接起来。例如 WordPress 容器对 some-mysql 的连接:</p><pre style="box-sizing: border-box; overflow: auto; margin-top: 2rem; margin-bottom: 2rem; padding: 1rem; position: relative; font-family: Consolas, Monaco, monospace; border: 1px solid rgb(221, 221, 221); border-radius: 2px; line-height: 1.15; font-size: 16px; color: rgb(85, 85, 85); letter-spacing: -0.48px; background: rgb(246, 246, 246);"><code class="language-shell" style="box-sizing: border-box; vertical-align: bottom; font-family: Consolas, Monaco, monospace; color: rgb(35, 119, 148); padding: 0.2rem 0.4rem; border-radius: 3px; font-size: 1rem; line-height: 1.5em; background: inherit;">docker run --name some-wordpress --link some-mysql:mysql -p 8080:80 -d wordpress</code></pre>