docker镜像制作、及相关操作
持续完善中。。。
Dockerfile制作镜像
首先还是推荐官方网址:
https://docs.docker.com/engine/reference/builder/
BuildKit
从18.09开始,Docker支持一个新的、moby/buildkit项目提供的、执行构建的后端。与旧版本相比的优先有:
识别并跳过没用的构建步骤
并行构建独立的构建步骤
在构建中仅增量传递在构建上下文中的变化的文件
识别并跳过构建上下文中无用文件的传输
Use external Dockerfile implementations with many new features
Avoid side-effects with rest of the API (intermediate images and containers)
Prioritize your build cache for automatic pruning
需要提前设置环境变量:DOCKER_BUILDKIT=1
To learn about the experimental Dockerfile syntax available to BuildKit-based builds
Parser directives
可选,影响Dockerfile中下一句的处理方式。 不创建layer,也不会以构建步骤的形式出现,编写时以特定的注释出现 # directive=value
. A single directive may only be used once.
一旦处理了注释、空行或构建指令,Docker就不再查找parser directives。它将格式化为解析器指令的任何内容视为注释,不尝试验证它是否是解析器指令。因此,所有parser directives都必须位于dockerfile的最顶端。
分析器指令不区分大小写,按照惯例是它们是小写的。也约定parser directives后跟一个空行。parser directives中不支持行继续符。
FROM
除了选择现有镜像为基础镜像外,Docker 还存在一个特殊的镜像,名为 scratch 。这个镜像是虚拟的概念,并不实际存在,它表示一个空白的镜像。
官方的hub网站上有很多正式的镜像供使用:https://hub.docker.com/
FROM centos:7.3.1611
ADD
文件拷贝
ADD Centos-ali.repo /etc/yum.repos.d/docker.repo
与COPY的区别:https://blog.youkuaiyun.com/taiyangdao/article/details/73222601
RUN
执行指定命令
RUN yum clean all
RUN yum repolist
RUN yum -y install libaio
制作体积较小的Docker镜像
如何制作体积较小的Docker镜像:http://www.docker.org.cn/docker/176.html
谷歌的distroless
谷歌的distroless体积小,没有内部shell、安全性高,更适合生产环境部署:https://github.com/GoogleContainerTools/distroless
不需要libc的Statically compiled applications (Go)可以用 gcr.io/distroless/static
image,这个镜像包含:
- ca-certificates
- A /etc/passwd entry for a root user
- A /tmp directory
- tzdata
大多数其他应用 (and Go apps that require libc/cgo) 需要使用 gcr.io/distroless/base
。这个镜像包含了gcr.io/distroless/static
里的所有包,以及:
- glibc
- libssl
- openssl
Alpine
Alpine体积更小,但是用的是musl libc,兼容性有问题
好在,Alpine也有glibc版本的:https://hub.docker.com/r/frolvlad/alpine-glibc/tags
最佳实践
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
参考的
https://www.cnblogs.com/dazhoushuoceshi/p/7066041.html
https://blog.youkuaiyun.com/wo18237095579/article/details/80540571