当使用docker build构建镜像的时,默认会将Dockerfile所在目录下的全部文件或文件夹,添加到构建docker镜像的上下文中,若是该上下文中包含太多不须要的文件或文件夹,将致使构建速度变慢。如:构建node项目镜像,上下文包含node_modules文件夹,该文件夹中包含N多运行时不须要的文件或文件夹,严重下降docker build速度。java
[root@influx jdk1.8.0_191-centos7.6.1810]# ls -al total 187304 drwxr-xr-x 2 root root 4096 Jul 23 17:20 . drwxr-xr-x 14 root root 4096 Jul 23 17:29 .. -rwxr-xr-x 1 root root 392 Jul 15 15:32 build-push.sh -rwxr-xr-x 1 root root 153 Jul 15 15:32 build-run.sh -rwxr-xr-x 1 root root 138 Jul 15 15:32 build.sh -rwxr-xr-x 1 root root 125 Jul 15 16:01 config.sh -rw-r--r-- 1 root root 840 Jul 23 17:20 Dockerfile -rw-r--r-- 1 root root 51 Jul 15 15:32 .dockerignore -rwxr-xr-x 1 root root 5638 Jul 15 15:32 dump.sh -rw-r--r-- 1 root root 191753373 Jul 15 15:49 jdk-8u191-linux-x64.tar.gz [root@influx jdk1.8.0_191-centos7.6.1810]# ./build.sh Sending build context to Docker daemon 191.8MB # 构建时,建立上下文的步骤,此处可知上下文大小为191MB Step 1/12 : FROM thub.xxxx.com.cn/digo/centos7.6.1810:2019-07-23_15-00-00 ---> 4331b9283ea1 ......
默认与Dockerfile文件同级目录node
# 目的:提高docker编译速度 # 作法:排除全部,包含须要的 # Reference: https://stackoverflow.com/questions/28097064/dockerignore-ignore-everything-except-a-file-and-the-dockerfile # Ignore everything ** # Allow files and directories !source/template-provider/target/template.jar !Dockerfile