docker 01

深入浅出Docker

Docker概述

基于GO语言开发 Docker的思想来自于集装箱。Docker通过隔离机制,额可以将服务器利用到极致。 隔离:Docker核心思想,打包装箱,每个箱子都是相互隔离的。

Docker的历史

'dotcloud' --> 做一些pass云计算服务,LXC有关的容器技术,将自己的容器化技术命名 --> Docker. Docker十分的轻巧。在容器技术出现之前,一直都是虚拟机技术. 虚拟机:在window中安装一个Vmware,通过这个软件我们可以虚拟出来一台或多台电脑。笨重。 虚拟机也属于虚拟化技术,Docker容器技术亦然。 vm linux centos原生镜像 隔离 需要多个虚拟机 G为单位 docker 隔离 镜像(最核心的环境4m + jdk + mysql)十分小巧,运行镜像即可。 M为单位 秒级启动

到现在,所有的开发人员都必须会使用docker.

聊聊Docker

docker能干啥??

之前的虚拟机技术

image

虚拟机技术缺点:

  1. 资源占用十分多

  2. 冗余步骤多

  3. 启动很慢

容器化技术

容器化技术不是模拟一个完整的操作系统

image

比较Docker和虚拟机技术的不同:

  • 传统虚拟机,虚拟出一条硬件,运行一个完成的操作系统,然后在这个系统上安装和运行软件

  • 容器内应用直接运行在宿主机上,容器内是没有自己的内核的,也没有虚拟我们的硬件,所以轻便

  • 每个容器间互相隔离,每个容器内都有属于自己的文件系统,互不影响

DevOps(开发 运维)

应用更快速的的交付和部署 传统:阅读帮助文档,开发程序 Docker:打包镜像发布测试,一键运行 更便捷的升级和扩缩容 更简单的系统运维 开发测试环境高度一致 更高效的计算机资源利用 Docker是内核级别的虚拟化,可以在一个物理机上运行更多的容器示例,榨干服务器

Docker的安装

docker的基本组成

image.jpeg
Docker为什么比VM更快?

1、Docker有着比虚拟机更少的抽象层

2、docker利用宿主机的内核,vm 需要Guest OS

image.jpeg

[图片上传失败...(image-746d47-1600430461687)]

docker常用命令

帮助命令

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n49" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker verison
docker info #显示docker的系统信息,包括镜像和容器的数量</pre>

镜像命令

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n51" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker images #查看所有主机上的镜像

可选项

-a, --all  #列出所有镜像
-q, --quiet #只显示镜像的id

docker search [image]

可选项

-f, --filter=STARS=5000

docker pull [镜像名:tag]#下载 分层下载,image的核心,联合文件系统

docker rmi -f [id]
docker rmi -f [id id id id]
docker rmi -f $(docker images -aq) #删除全部的容器</pre>

容器命令

说明:有了镜像方能创建容器

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker pull ubuntu
docker run [args] image
--name="name"
-d #后台方式运行
-it #使用交互方式
-p #指定容器的端口
-p 主机端口:容器端口
-p 容器端口
-P #随意指定端口
docker ps #列出所有运行的容器
docker ps -a #列出曾经运行的容器+历史的
-n=? #显示最近运行的容器

exit #直接停止并推出
ctrl +P +Q #容器不停止并退出

docker rm id
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器

docker start id
docker restart id
docker stop id
docker kill id</pre>

常用其他命令

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n56" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -d [image] #后台运行时,需要前台进程,没有前台服务会自动停止

docker logs #查看日志

编写一个shell脚本

docker run -d ubuntu:latest /bin/sh -c "while true;do echo summer;sleep 1;done"

docker logs -tf --tail 10 id

查看容器中进程的信息

docker top id

docker inspect id #查看容器的信息

进入当前正在运行的容器

docker exec -it id /bin/bash #进入容器后开启一个新的终端
docker attach id #进入正在执行的命令行,不会启动新的进程

从容器内拷贝到主机上

docker cp id:容器内路径 目标路径

拷贝是一个手动过程,后期会通过 -v 卷实现自动同步</pre>

基础命令小结

[图片上传失败...(image-a0f697-1600430461687)]

Docker安装nginx

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#搜索nginx
docker search nginx

下载

docker pull nginx

运行并测试

docker run -d --name nginx01 -p:3344:80 nginx
curl localhost:3344

docker exec -it nginx01 /bin/bash</pre>

端口暴露

[图片上传失败...(image-169bff-1600430461687)]

Docker安装tomcat

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n67" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 官方使用 一般用来测试 用完就删除
docker run -it --rm tomcat:9.0

我们之前的启动都是后台,停止之后还是可以查到的

docker run -d -p 3355:8080 --name tomcat01 tomcat

测试访问没有问题 但是页面为404 这是因为阉割版的tomcat

linux命令少了 缺少webapp的内容

解决方法 将webapps.dist cpoy

cp -r /webapps.dist/* /webapps

访问成功</pre>

如何在容器外部修改,避免每次进入容器

部署es + kibana

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n71" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># es 暴露的端口很多 十分的耗内存

下载并启动

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

查看CPU状态

docker stats

修改配置文件 增加内存限制

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPT="-Xms64m -Xmx512m" Selasticsearch:7.6.2</pre>

Docker网络原理

[图片上传失败...(image-21f2ab-1600430461687)]

可视化

  • portainer

  • Rancher(CI/CD)

什么是portainer?

docker图形化界面管理工具,提供一个后台面板供我们操作

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n83" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer</pre>

image.png
image.png

Docker镜像讲解

所有应用,直接打包docker镜像,就可以直接跑起来

Docker镜像加载原理

UnionFS(联合文件系统)

UnionFS(联合文件系统):一种分层/轻量级并且高性能的文件系统,它支持对文件系统的修改来作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下。UnionFS文件系统是Docker镜像的基础。镜像可以通过分层来继承,基于基础镜像,可以制作各种具体的应用镜像。

下载时出现的层级关系就是如此.

特性:一次同时加载多个文件系统,但是从外面只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录

Docker镜像加载原理

docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS

bootfs(boot file system)主要包含bootloader和kernel,bootloader主要引导加载kernal,Linux刚启动时会加载bootfs文件系统,在docker镜像的最底层时bootfs,这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核,当boot加载完成之后整个内核就在内存之中,此时内存的使用权已有bootfs转交给内核,此时系统也会卸载bootfs

rootfs,在bootfs之上,包含的就是典型的Linux系统中的/dev,/proc,/bin,/etc等标准目录和文件.rootfs就是各种不同的操作系统发行版,比如ubuntu,CentOS等等

[图片上传失败...(image-206d26-1600430461686)]

对于精简的OS,rootfs可以很小,只需要包含最基本的命令,工具和程序就可以了,因为底层直接用Host的kernel,自己只需要提供rootfs就可以了.由此可见对于不同的linux发行版,bootfs基本一致,rootfs会有差别,因此不同的发行版本可以公用bootfs

虚拟机是分钟级 容器时秒级

分层理解

已经下载的就不用下了

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n105" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker image inspect redis:latest #查看RootFS</pre>

image.png
commit镜像

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n108" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker commit image
docker commit -m="message" -a="summer" id [image:tag]</pre>

image.png

容器数据卷

什么是容器数据卷

容器之间需要有一个数据共享的技术!Docker容器中产生的数据,同步到本地!

这就是卷技术!目录的挂载!

方式一:使用命令挂载

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n117" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -it -v 主机目录:容器目录

docker inspect #查看容器配置信息</pre>

image.png

实战MySQL

数据持久化问题

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n122" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker pull mysql

运行时做数据挂载

docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123123 --name mysql01 mysql</pre>

[图片上传失败...(image-fa4c3d-1600430461686)]

具名和匿名挂载

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n125" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 匿名挂载
-v 容器内路径名
docker run -d -P --name nginx01 -v /etc/nginx nginx

查看所有卷的情况

docker volume ls

具名挂载就是指定挂载目录的地址

-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载

设置容器权限

docker run -d -P --name nginx02 -v summer-nginx:/etc/nginx:ro nginx #内容只能通过宿主机来操作 容器是无法操作的
docker run -d -P --name nginx02 -v summer-nginx:/etc/nginx:ro nginx</pre>

image.png
初识DockerFile

Dockerfile就是用来构建docker镜像的构建文件

通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个个的命令,每个命令都是一层

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="dockerfile" cid="n130" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># create a dockerfile
FROM ubuntu
VOLUME ["volume01","volume02"]
CMD echo "---end---"
CMD /bin/bash

docker build -f ./dockerfile -t summer/ubuntu:1.0 .</pre>

image.png
image.png
image.png
数据卷容器

两个mysql同步数据 --volumes-from

[图片上传失败...(image-f1b4ec-1600430461686)]

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n138" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -it --name docker03 --volumes-from docker01 image:tag

删除docker01后,docker03依旧能够访问挂载文 件

容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。</pre>

Dockerfile

  1. 编写一个dockerfile文件

  2. docker build 构建成为一个镜像

  3. docker run 运行镜像

  4. docker push发布镜像(DockerHub,阿里云镜像仓库)

DockerFile指令

[图片上传失败...(image-81718c-1600430461686)]

实战测试

创建一个centos 视频示例

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="dockerfile" cid="n154" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">FROM centos
MAINTAINER summer591735912@qq.com
ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash</pre>

爷的示例 Ubuntu中安装vim和inetutils-ping

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="dockerfile" cid="n157" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">FROM ubuntu
MAINTAINER summer591735912@qq.com
ENV MYPATH /usr/local #pwd展示的目录,工作目录

RUN apt-get update #先更新源
RUN apt-get -y install vim
RUN apt-get -y install inetutils-ping

EXPOSE 8028

CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash

依旧有debconf:delaying package configuration,since apt-utils is not installed</pre>

image.png
image.png
image.png
image.png
image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n163" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker history id #查看安装的工作历史</pre>

CMD 和 ENTRYPOINT的区别 (追加命令测试)

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n166" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令</pre>

测试cmd

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="dockerfile" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">FROM ubuntu
CMD ["ls","-a"]</pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n170" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 想追加一个 'l' 构成 ls -al 此时需要使用ENTRYPOINT
docker run id ls -al</pre>

Tomcat镜像文件
  1. 准备镜像文件tomcat的压缩包,jdk压缩包

  2. 编写dockerfile文件

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="dockerfile" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">FROM ubuntu
MAINTAINER summer591735912@qq.com

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u212-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-7.0.105.tar.gz /usr/local/

RUN apt-get update
RUN apt-get -y install vim

ENV MYPATH /usr/local/
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_212
ENV CLASSPATH JAVA_HOME/lib/dt.jar:JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.105
ENV CATALINA_BASH /usr/local/apache-tomcat-7.0.105
ENV PATH PATH:JAVA_HOME/bin:CATALINA_HOME/lib:CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-7.0.105/bin/startup.sh && tail -F /usr/local/apache-tomcat-7.0.105/logs/catalina.out #此处有问题 导致无法启动tomcat
CMD echo "----end----"</pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="powershell" cid="n178" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(base) summer@summer-ubuntu:/home/docker-test-volumedocker build -f ./dockerfile -t summercat:1.0 . Sending build context to Docker daemon 204.7MB Step 1/17 : FROM ubuntu ---> 4e2eef94cd6b Step 2/17 : MAINTAINER summer<591735912@qq.com> ---> Using cache ---> 5de7e49b847b Step 3/17 : COPY readme.txt /usr/local/readme.txt ---> Using cache ---> 29b32482b9bb Step 4/17 : ADD jdk-8u212-linux-x64.tar.gz /usr/local/ ---> f7c5ed2e25a4 Step 5/17 : ADD apache-tomcat-7.0.105.tar.gz /usr/local/ ---> c95224a17d04 Step 6/17 : RUN apt-get update ---> Running in b78e7c1e2013 Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB] Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB] Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [221 kB] Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB] Get:6 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [67.5 kB] Get:7 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB] Get:8 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [42.4 kB] Get:9 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [1078 B] Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB] Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB] Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB] Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [207 kB] Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [42.5 kB] Get:15 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [17.3 kB] Get:16 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [444 kB] Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [3206 B] Fetched 14.5 MB in 19s (775 kB/s) Reading package lists... Removing intermediate container b78e7c1e2013 ---> afa6f14fe10c Step 7/17 : RUN apt-get -y install vim ---> Running in 26da24655306 Reading package lists... Building dependency tree... Reading state information... The following additional packages will be installed: alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim-common vim-runtime xxd xz-utils Suggested packages: libasound2-plugins alsa-utils libcanberra-gtk0 libcanberra-pulse gpm readline-doc ctags vim-doc vim-scripts The following NEW packages will be installed: alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim vim-common vim-runtime xxd xz-utils 0 upgraded, 30 newly installed, 0 to remove and 6 not upgraded. Need to get 14.9 MB of archives. After this operation, 70.4 MB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libmagic-mgc amd64 1:5.38-4 [218 kB] Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libmagic1 amd64 1:5.38-4 [75.9 kB] Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 file amd64 1:5.38-4 [23.3 kB] Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libexpat1 amd64 2.2.9-1build1 [73.3 kB] Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 libmpdec2 amd64 2.4.2-3 [81.1 kB] Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 libssl1.1 amd64 1.1.1f-1ubuntu2 [1318 kB] Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-minimal amd64 3.8.2-1ubuntu1.2 [705 kB] Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 mime-support all 3.64ubuntu1 [30.6 kB] Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 readline-common all 8.0-4 [53.5 kB] Get:10 http://archive.ubuntu.com/ubuntu focal/main amd64 libreadline8 amd64 8.0-4 [131 kB] Get:11 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libsqlite3-0 amd64 3.31.1-4ubuntu0.2 [549 kB] Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-stdlib amd64 3.8.2-1ubuntu1.2 [1665 kB] Get:13 http://archive.ubuntu.com/ubuntu focal/main amd64 xxd amd64 2:8.1.2269-1ubuntu5 [50.1 kB] Get:14 http://archive.ubuntu.com/ubuntu focal/main amd64 vim-common all 2:8.1.2269-1ubuntu5 [85.1 kB] Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 xz-utils amd64 5.2.4-1ubuntu1 [82.5 kB] Get:16 http://archive.ubuntu.com/ubuntu focal/main amd64 alsa-topology-conf all 1.2.2-1 [7364 B] Get:17 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 alsa-ucm-conf all 1.2.2-1ubuntu0.2 [24.8 kB] Get:18 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libasound2-data all 1.2.2-2.1ubuntu2 [20.1 kB] Get:19 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libasound2 amd64 1.2.2-2.1ubuntu2 [335 kB] Get:20 http://archive.ubuntu.com/ubuntu focal/main amd64 libltdl7 amd64 2.4.6-14 [38.5 kB] Get:21 http://archive.ubuntu.com/ubuntu focal/main amd64 libtdb1 amd64 1.4.2-3build1 [44.1 kB] Get:22 http://archive.ubuntu.com/ubuntu focal/main amd64 libogg0 amd64 1.3.4-0ubuntu1 [24.0 kB] Get:23 http://archive.ubuntu.com/ubuntu focal/main amd64 libvorbis0a amd64 1.3.6-2ubuntu1 [87.0 kB] Get:24 http://archive.ubuntu.com/ubuntu focal/main amd64 libvorbisfile3 amd64 1.3.6-2ubuntu1 [16.1 kB] Get:25 http://archive.ubuntu.com/ubuntu focal/main amd64 sound-theme-freedesktop all 0.8-2ubuntu1 [384 kB] Get:26 http://archive.ubuntu.com/ubuntu focal/main amd64 libcanberra0 amd64 0.30-7ubuntu1 [38.1 kB] Get:27 http://archive.ubuntu.com/ubuntu focal/main amd64 libgpm2 amd64 1.20.7-5 [15.1 kB] Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8 amd64 3.8.2-1ubuntu1.2 [1620 kB] Get:29 http://archive.ubuntu.com/ubuntu focal/main amd64 vim-runtime all 2:8.1.2269-1ubuntu5 [5873 kB] Get:30 http://archive.ubuntu.com/ubuntu focal/main amd64 vim amd64 2:8.1.2269-1ubuntu5 [1238 kB] debconf: delaying package configuration, since apt-utils is not installed Fetched 14.9 MB in 15s (1020 kB/s) Selecting previously unselected package libmagic-mgc. (Reading database ... 4122 files and directories currently installed.) Preparing to unpack .../00-libmagic-mgc_1%3a5.38-4_amd64.deb ... Unpacking libmagic-mgc (1:5.38-4) ... Selecting previously unselected package libmagic1:amd64. Preparing to unpack .../01-libmagic1_1%3a5.38-4_amd64.deb ... Unpacking libmagic1:amd64 (1:5.38-4) ... Selecting previously unselected package file. Preparing to unpack .../02-file_1%3a5.38-4_amd64.deb ... Unpacking file (1:5.38-4) ... Selecting previously unselected package libexpat1:amd64. Preparing to unpack .../03-libexpat1_2.2.9-1build1_amd64.deb ... Unpacking libexpat1:amd64 (2.2.9-1build1) ... Selecting previously unselected package libmpdec2:amd64. Preparing to unpack .../04-libmpdec2_2.4.2-3_amd64.deb ... Unpacking libmpdec2:amd64 (2.4.2-3) ... Selecting previously unselected package libssl1.1:amd64. Preparing to unpack .../05-libssl1.1_1.1.1f-1ubuntu2_amd64.deb ... Unpacking libssl1.1:amd64 (1.1.1f-1ubuntu2) ... Selecting previously unselected package libpython3.8-minimal:amd64. Preparing to unpack .../06-libpython3.8-minimal_3.8.2-1ubuntu1.2_amd64.deb ... Unpacking libpython3.8-minimal:amd64 (3.8.2-1ubuntu1.2) ... Selecting previously unselected package mime-support. Preparing to unpack .../07-mime-support_3.64ubuntu1_all.deb ... Unpacking mime-support (3.64ubuntu1) ... Selecting previously unselected package readline-common. Preparing to unpack .../08-readline-common_8.0-4_all.deb ... Unpacking readline-common (8.0-4) ... Selecting previously unselected package libreadline8:amd64. Preparing to unpack .../09-libreadline8_8.0-4_amd64.deb ... Unpacking libreadline8:amd64 (8.0-4) ... Selecting previously unselected package libsqlite3-0:amd64. Preparing to unpack .../10-libsqlite3-0_3.31.1-4ubuntu0.2_amd64.deb ... Unpacking libsqlite3-0:amd64 (3.31.1-4ubuntu0.2) ... Selecting previously unselected package libpython3.8-stdlib:amd64. Preparing to unpack .../11-libpython3.8-stdlib_3.8.2-1ubuntu1.2_amd64.deb ... Unpacking libpython3.8-stdlib:amd64 (3.8.2-1ubuntu1.2) ... Selecting previously unselected package xxd. Preparing to unpack .../12-xxd_2%3a8.1.2269-1ubuntu5_amd64.deb ... Unpacking xxd (2:8.1.2269-1ubuntu5) ... Selecting previously unselected package vim-common. Preparing to unpack .../13-vim-common_2%3a8.1.2269-1ubuntu5_all.deb ... Unpacking vim-common (2:8.1.2269-1ubuntu5) ... Selecting previously unselected package xz-utils. Preparing to unpack .../14-xz-utils_5.2.4-1ubuntu1_amd64.deb ... Unpacking xz-utils (5.2.4-1ubuntu1) ... Selecting previously unselected package alsa-topology-conf. Preparing to unpack .../15-alsa-topology-conf_1.2.2-1_all.deb ... Unpacking alsa-topology-conf (1.2.2-1) ... Selecting previously unselected package alsa-ucm-conf. Preparing to unpack .../16-alsa-ucm-conf_1.2.2-1ubuntu0.2_all.deb ... Unpacking alsa-ucm-conf (1.2.2-1ubuntu0.2) ... Selecting previously unselected package libasound2-data. Preparing to unpack .../17-libasound2-data_1.2.2-2.1ubuntu2_all.deb ... Unpacking libasound2-data (1.2.2-2.1ubuntu2) ... Selecting previously unselected package libasound2:amd64. Preparing to unpack .../18-libasound2_1.2.2-2.1ubuntu2_amd64.deb ... Unpacking libasound2:amd64 (1.2.2-2.1ubuntu2) ... Selecting previously unselected package libltdl7:amd64. Preparing to unpack .../19-libltdl7_2.4.6-14_amd64.deb ... Unpacking libltdl7:amd64 (2.4.6-14) ... Selecting previously unselected package libtdb1:amd64. Preparing to unpack .../20-libtdb1_1.4.2-3build1_amd64.deb ... Unpacking libtdb1:amd64 (1.4.2-3build1) ... Selecting previously unselected package libogg0:amd64. Preparing to unpack .../21-libogg0_1.3.4-0ubuntu1_amd64.deb ... Unpacking libogg0:amd64 (1.3.4-0ubuntu1) ... Selecting previously unselected package libvorbis0a:amd64. Preparing to unpack .../22-libvorbis0a_1.3.6-2ubuntu1_amd64.deb ... Unpacking libvorbis0a:amd64 (1.3.6-2ubuntu1) ... Selecting previously unselected package libvorbisfile3:amd64. Preparing to unpack .../23-libvorbisfile3_1.3.6-2ubuntu1_amd64.deb ... Unpacking libvorbisfile3:amd64 (1.3.6-2ubuntu1) ... Selecting previously unselected package sound-theme-freedesktop. Preparing to unpack .../24-sound-theme-freedesktop_0.8-2ubuntu1_all.deb ... Unpacking sound-theme-freedesktop (0.8-2ubuntu1) ... Selecting previously unselected package libcanberra0:amd64. Preparing to unpack .../25-libcanberra0_0.30-7ubuntu1_amd64.deb ... Unpacking libcanberra0:amd64 (0.30-7ubuntu1) ... Selecting previously unselected package libgpm2:amd64. Preparing to unpack .../26-libgpm2_1.20.7-5_amd64.deb ... Unpacking libgpm2:amd64 (1.20.7-5) ... Selecting previously unselected package libpython3.8:amd64. Preparing to unpack .../27-libpython3.8_3.8.2-1ubuntu1.2_amd64.deb ... Unpacking libpython3.8:amd64 (3.8.2-1ubuntu1.2) ... Selecting previously unselected package vim-runtime. Preparing to unpack .../28-vim-runtime_2%3a8.1.2269-1ubuntu5_all.deb ... Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime' Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime' Unpacking vim-runtime (2:8.1.2269-1ubuntu5) ... Selecting previously unselected package vim. Preparing to unpack .../29-vim_2%3a8.1.2269-1ubuntu5_amd64.deb ... Unpacking vim (2:8.1.2269-1ubuntu5) ... Setting up libexpat1:amd64 (2.2.9-1build1) ... Setting up libgpm2:amd64 (1.20.7-5) ... Setting up libogg0:amd64 (1.3.4-0ubuntu1) ... Setting up mime-support (3.64ubuntu1) ... Setting up alsa-ucm-conf (1.2.2-1ubuntu0.2) ... Setting up libmagic-mgc (1:5.38-4) ... Setting up libtdb1:amd64 (1.4.2-3build1) ... Setting up libssl1.1:amd64 (1.1.1f-1ubuntu2) ... debconf: unable to initialize frontend: Dialog debconf: (TERM is not set, so the dialog frontend is not usable.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.) debconf: falling back to frontend: Teletype Setting up libsqlite3-0:amd64 (3.31.1-4ubuntu0.2) ... Setting up libmagic1:amd64 (1:5.38-4) ... Setting up file (1:5.38-4) ... Setting up xxd (2:8.1.2269-1ubuntu5) ... Setting up libasound2-data (1.2.2-2.1ubuntu2) ... Setting up vim-common (2:8.1.2269-1ubuntu5) ... Setting up xz-utils (5.2.4-1ubuntu1) ... update-alternatives: using /usr/bin/xz to provide /usr/bin/lzma (lzma) in auto mode update-alternatives: warning: skip creation of /usr/share/man/man1/lzma.1.gz because associated file /usr/share/man/man1/xz.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/unlzma.1.gz because associated file /usr/share/man/man1/unxz.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzcat.1.gz because associated file /usr/share/man/man1/xzcat.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzmore.1.gz because associated file /usr/share/man/man1/xzmore.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzless.1.gz because associated file /usr/share/man/man1/xzless.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzdiff.1.gz because associated file /usr/share/man/man1/xzdiff.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzcmp.1.gz because associated file /usr/share/man/man1/xzcmp.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzgrep.1.gz because associated file /usr/share/man/man1/xzgrep.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzegrep.1.gz because associated file /usr/share/man/man1/xzegrep.1.gz (of link group lzma) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/lzfgrep.1.gz because associated file /usr/share/man/man1/xzfgrep.1.gz (of link group lzma) doesn't exist Setting up libvorbis0a:amd64 (1.3.6-2ubuntu1) ... Setting up libltdl7:amd64 (2.4.6-14) ... Setting up alsa-topology-conf (1.2.2-1) ... Setting up sound-theme-freedesktop (0.8-2ubuntu1) ... Setting up libasound2:amd64 (1.2.2-2.1ubuntu2) ... Setting up libmpdec2:amd64 (2.4.2-3) ... Setting up vim-runtime (2:8.1.2269-1ubuntu5) ... Setting up readline-common (8.0-4) ... Setting up libpython3.8-minimal:amd64 (3.8.2-1ubuntu1.2) ... Setting up libreadline8:amd64 (8.0-4) ... Setting up libvorbisfile3:amd64 (1.3.6-2ubuntu1) ... Setting up libpython3.8-stdlib:amd64 (3.8.2-1ubuntu1.2) ... Setting up libcanberra0:amd64 (0.30-7ubuntu1) ... Setting up libpython3.8:amd64 (3.8.2-1ubuntu1.2) ... Setting up vim (2:8.1.2269-1ubuntu5) ... update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode update-alternatives: warning: skip creation of /usr/share/man/da/man1/vi.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/de/man1/vi.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/fr/man1/vi.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/it/man1/vi.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ja/man1/vi.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/pl/man1/vi.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ru/man1/vi.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/vi.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group vi) doesn't exist update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode update-alternatives: warning: skip creation of /usr/share/man/da/man1/view.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/de/man1/view.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/it/man1/view.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ja/man1/view.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/pl/man1/view.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ru/man1/view.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/view.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group view) doesn't exist update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode update-alternatives: warning: skip creation of /usr/share/man/da/man1/ex.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/de/man1/ex.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/fr/man1/ex.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/it/man1/ex.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ja/man1/ex.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/pl/man1/ex.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ru/man1/ex.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/ex.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group ex) doesn't exist update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode update-alternatives: warning: skip creation of /usr/share/man/da/man1/editor.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/de/man1/editor.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/fr/man1/editor.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/it/man1/editor.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/pl/man1/editor.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist Processing triggers for libc-bin (2.31-0ubuntu9) ... Removing intermediate container 26da24655306 ---> 619307d0c637 Step 8/17 : ENV MYPATH /usr/local/ ---> Running in b06c2a2a04d4 Removing intermediate container b06c2a2a04d4 ---> 34d5e1132a36 Step 9/17 : WORKDIRMYPATH
---> Running in 25a3b17f9cd8
Removing intermediate container 25a3b17f9cd8
---> 1ae1442a37f0
Step 10/17 : ENV JAVA_HOME /usr/local/jdk1.8.0_212
---> Running in fa66f8f1ac7c
Removing intermediate container fa66f8f1ac7c
---> a0ef0558e23f
Step 11/17 : ENV CLASSPATH JAVA_HOME/lib/dt.jar:JAVA_HOME/lib/tools.jar
---> Running in 6e1d969e6c70
Removing intermediate container 6e1d969e6c70
---> efbeb08d1c25
Step 12/17 : ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.105
---> Running in 402474d2c486
Removing intermediate container 402474d2c486
---> 5ac873f2766a
Step 13/17 : ENV CATALINA_BASH /usr/local/apache-tomcat-7.0.105
---> Running in f61cd128c180
Removing intermediate container f61cd128c180
---> 14d213898d39
Step 14/17 : ENV PATH PATH:JAVA_HOME/bin:CATALINA_HOME/lib:CATALINA_HOME/bin
---> Running in 12da6c314b92
Removing intermediate container 12da6c314b92
---> 6dd48e24f077
Step 15/17 : EXPOSE 8080
---> Running in 629beda656c7
Removing intermediate container 629beda656c7
---> 4893cb8ce3e6
Step 16/17 : CMD /usr/local/apache-tomcat-7.0.105/startup.sh && tail -F /usr/local/apache-tomcat-7.0.105/bin/logs/catalina.out
---> Running in 33822deeddf8
Removing intermediate container 33822deeddf8
---> 5a66e5f51ea2
Step 17/17 : CMD echo "----end----"
---> Running in aacb08595dd0
Removing intermediate container aacb08595dd0
---> 6ea98729bf64
Successfully built 6ea98729bf64
Successfully tagged summercat:1.0</pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n179" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 创建容器并挂载
docker run -it --name summercat:1.0 -p 9090:8080 -v /home/summer/build/tomcat/test:/usr/local/apache-tomcat-7.0.105/webapps/test -v /home/summer/build/tomcat/logs:/usr/local/apache-tomcat-7.0.105/logs summercat:1.0 /bin/bash</pre>

发布项目(由于做了挂载,我们直接在本地编写项目就可以发布)

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="xml" cid="n181" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app></pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="jsp" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello summer</title>
</head>
<body>
Hello docker!

<%
out.println("你的 IP 地址 " + request.getRemoteAddr());
%>
</body>
</html></pre>

原来这就是发布啊!!

发布自己的镜像dockerhub
  1. https://hub.docker.com/ 账号

  2. 登录

  3. 提交镜像

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n193" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker login -u name
docker push summer/images:1.0</pre>

发布自己的镜像到阿里云
  1. 登陆阿里云,找到容器服务

  2. 创建命名空间

  3. 创建容器镜像

  4. 看阿里云教程

Docker网络

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#启动tomcat
docker run -d -P --name mycat tomcat

查看容器内地址 docker分配了一个eth0@if2

docker exec -it mycat ip addr</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#容器外
ping 172.17.0.2 #success</pre>

原理

  1. 我们每启动一个docker容器,docker就会给docker容器分配一个ip,我们只要安装docker,就会有一个网卡docker0桥接模式,使用的技术是evth-pair技术!

  2. 容器带来的网卡都是一对对出现的,evth-pair就是一对虚拟设备接口,一段连着协议,一段彼此相连,充当一个桥梁,连接各种虚拟网络设备

结论:容器与容器之间是能够ping通的

[图片上传失败...(image-21a7a4-1600430461686)]

结论:docker0是一个公用的路由器

所有容器在不指定网络的情况下,docker0会给他们分配一个默认的可用IP

Docker使用的是linux的桥接,宿主机中是一个Docker容器的网桥docker0

docker中所有的网络接口都是虚拟的,虚拟的转发效率高(内网贼快的好吧)

[图片上传失败...(image-f33e77-1600430461686)]

--link

思考一个场景,编写一个微服务,database url = ip,项目不重启,数据库IP换掉,我们希望可以通过名字来进行访问容器。

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n227" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker exec -it mycat02 ping mycat</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -it -P --name mycat03 --link mycat02 tomcat bash

但是方向无法ping通</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="powershell" cid="n231" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker network ls
docker network inspect id

(base) summer@summer-ubuntu:~/build/tomcat/logs$ docker network inspect 1a4c1965e38b
[
{
"Name": "bridge",
"Id": "1a4c1965e38bec706e5510466cd7e926f6bac2543f07919f2b6a8a58317621ca",
"Created": "2020-09-08T21:01:49.380468428+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"8d20d8c0d1bc3af2a31ba3e46eba90981815c38755a0f2be5acaee44f4030339": {
"Name": "mycat02",
"EndpointID": "2368a70e8b1148c852f612c71ad25191ef1d2efa5630885324882e90a30b393f",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"b78c76859a085802c80be34b5fde497a2b5cc005ea63ead1b646e21ac9f5a452": {
"Name": "mycat",
"EndpointID": "ff13e65d8197c73f0556db6f91172d51abb278b1276c3509677ad76e91f80e13",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"e119826d2a769803ce6fbea2db2f907e7a44d07e4eed0b3a2c845cf8bd63c074": {
"Name": "mycat03",
"EndpointID": "d3bedf123842c0edb561e28a853f35289c271de6ca8e012cc164058eb49f37b3",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]</pre>

image.png

本质探究: --link使得hosts文件中增加一行配置信息

现在不推荐使用 --link

自定义网络不适用docker0

docker0的问题:不支持容器名连接访问

自定义网络
网络模式:

bridge :桥接docker(默认)

none :不配置网络

host :和宿主机共享网络

container :容器网络联通(用得少,局限大)

测试

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="powershell" cid="n245" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># --net bridge 就是docker0,默认,域名不能访问,--link能狗打通链接
docker run -d -P --name tomcat01 --net birdge tomcat

--subnet 192.168.0.0/16 192.168.0.1 192.168.255.255

docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n247" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 这样就可以ping名字了
docker -d -P --name tomcat-net-01 --net mynet tomcat
docker network inspect mynet</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n249" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 新建容器 测试名字访问</pre>

image.png

自定义的网络不使用--link也能连接成功

网络联通

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n254" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#一个容器两个地址
docker network connect mynet tomcat01</pre>

实战:部署Redis集群

[图片上传失败...(image-d7293d-1600430461686)]

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n257" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#1. 创建网卡
docker network create redis --subnet 172.38.0.0/16

2. 通过脚本创建六个redis配置

for port in (seq 1 6); \ do \ mkdir -p /mydata/redis/node-port/conf
touch /mydata/redis/node-port/conf/redis.conf cat << EOF >/mydata/redis/node-port/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1$port
cluster-announce-port 6379
cluster-announce-port 16379
appendonly yes
EOF
done

3. 启动

docker run -p 6371:6379 -p 16371:16379 --name redis-1
-v /mydata/redis/node-1/data:/data
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6372:6379 -p 16372:16379 --name redis-2
-v /mydata/redis/node-2/data:/data
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

如上整六个</pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n258" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 别如上了 看下面
for p in (seq 1 6); \ do \ docker run -p 637p:6379 -p 1637p:16379 --name redis-p -v /mydata/redis/node-p/data:/data -v /mydata/redis/node-p/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.1$p redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done

启动成功 看下图</pre>

image.png

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n260" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#进入容器瞅一瞅 不截图了
docker exec -it redis-1 /bin/sh #没有bash 只能sh

4. 创建集群

redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1</pre>

redis错误定位
image.png

这报错看不懂呀 学完redis再来吧

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="shell" cid="n265" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#
redis-cli -s
cluster info
cluster nodes</pre>

SpringBoot微服务打包为docker镜像

  1. 架构springboot项目

  2. 打包应用

  3. 编写dockerfile

  4. 构建镜像

  5. 发布运行

看看就好了🐾

docker基础篇到此为止。那么问题来了,如果有很多镜像,上百个,这该怎么办,且听下回分解。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269