docker镜像精简

精简思路:

  1. 选择合适底层镜像,比如alpine,还有一种空白镜像,叫scratch
  2. Union FS有最大层数限制,每用一个RUN就是一层镜像,所以应该尽量用一个 RUN 涵盖所有的操作命令
  3. 注意执行build命令时所在的目录,不要包含无用文件

关于 3 的补充解释:docker build 的时候会把上下文内容(context)一起带着发给 daemon,如果执行命令所在的目录文件非常多,docker daemon 处理会很慢(对于镜像大小有没有影响?)。 

关于docker context 的官方解释

 The PATH specifies where to find the files for the “context” of the build on the Docker daemon. Remember that the daemon could be running on a remote machine and that no parsing of the Dockerfile happens at the client side (where you’re running docker build). That means that all the files at PATH get sent, not just the ones listed to ADD in the Dockerfile.

PATH 会把所有路径下的files发给server端,由server端去处理dockerfile的内容,而不是client端,files不仅仅是ADD命令里面提到的file,是路径下的所有file。

 翻译一下:对于docker build [OPTIONS] PATH | URL | -这个命令(一般情况下我们直接把 PATH写成 . ), docker 会把 context 的内容都发给daemon 去处理,这就是为什么 ADD COPY 写 linux 绝对路径不好使的原因,因为daemon只收到 context的内容 ,并不知道Linux 绝对路径下有哪些东西,因此会报 file not found 的错误。因此我们要注意,build的时候,尤其是选 . (点)这个目录的时候,文件夹里面不要放太多无关的东西。

常用命令场景

docker pull [选项] [Docker Registry 地址[:端口]/]仓库名[:标签]

docker run -it --rm [镜像ID] [命令]

docker system df

docker system prune

docker logs [container ID]

docker attach [container ID] 进入已经running的容器内部,类似于virsh console

docker build . -t

常用安装包

ps procps

netstat -net-tools

ping inetutils-ping

原文地址:https://www.cnblogs.com/handsomehuo/p/15247332.html