我有一个包含很多层的 docker 文件。在文件的顶部我有一些参数,例如
FROM ubuntu:18.04
ARGS USER=test-user
ARGS UID=1000
#ARGS PW=test-user
# Then several Layers which does not use any ARGS. Example
LABEL version="1.0"
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN mkdir ~/mapped-volume
RUN apt-get update && apt-get install -y wget bzip2 ca-certificates build-essential curl git-core htop pkg-config unzip unrar tree freetds-dev vim \
sudo nodejs npm net-tools flex perl automake bison libtool byacc
# And so on
# And finally towards the end
# Setup User
RUN useradd -m -d /home/${USER} --uid ${UID} -G sudo -s /bin/bash ${USER}
# && echo "${USER}:${PW}" | chpasswd
# Couple of more commands to change dir, entry point etc. Example
Run Code Online (Sandbox Code Playgroud)
当我使用与上次构建不同的任何参数值构建此 docker 文件时和/或在最后两层进行小更改后,构建会再次构建所有内容。它不使用缓存层。我用来构建的命令是这样的
docker build --build-arg USER=new-user --build-arg UID=$UID -t my-image:1.0 .
Run Code Online (Sandbox Code Playgroud)
每次我更改这些值时,构建都会再次进行。顶部被截断,如下所示
UID -t my-image:1.0 .
Sending build context to Docker daemon 44.54kB
Step 1/23 : FROM ubuntu:18.04
---> ccc6e87d482b
Step 2/23 : ARG USER=ml-user
---> Using cache
---> 6c0c5d5c5056
Step 3/23 : ARG UID=1000
---> Using cache
---> b25867c282c7
Step 4/23 : LABEL version="1.0"
---> Running in 1ffff70d56c1
Removing intermediate container 1ffff70d56c1
---> 0f1277def3ca
Step 5/23 : ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
---> Running in 49d08c41b233
Removing intermediate container 49d08c41b233
---> f5b345573c1f
Step 6/23 : RUN mkdir ~/mapped-volume
---> Running in e4f8a5956450
Removing intermediate container e4f8a5956450
---> 1b22731d9051
Step 7/23 : RUN apt-get update && apt-get install -y wget bzip2 ca-certificates build-essential curl git-core htop pkg-config unzip unrar tree freetds-dev vim sudo nodejs npm net-tools flex perl automake bison libtool byacc
---> Running in ffc297de6234
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Run Code Online (Sandbox Code Playgroud)
因此,从第 7 步开始,它会继续执行所有步骤,而不使用该层的缓存,该层应该有一堆包,为什么?我怎样才能阻止这个?以前,当我没有参数时,该层和其他层通常从缓存中获取。
将您的参数移至您需要它们之前。Docker 在运行 RUN 命令之前不会替换它们中的参数。相反,args 作为环境变量传递,并由临时容器内的 shell 进行扩展。因此,对 arg 的更改就是对环境的更改,并且会丢失该步骤的构建缓存。一旦其中一个步骤错过了缓存,接下来的所有步骤都必须重新构建。
FROM ubuntu:18.04
# Then several Layers which does not use any ARGS. Example
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN mkdir ~/mapped-volume
RUN apt-get update && apt-get install -y wget bzip2 ca-certificates build-essential curl git-core htop pkg-config unzip unrar tree freetds-dev vim \
sudo nodejs npm net-tools flex perl automake bison libtool byacc
# And so on
# And finally towards the end
# Setup User
ARGS USER=test-user
ARGS UID=1000
RUN useradd -m -d /home/${USER} --uid ${UID} -G sudo -s /bin/bash ${USER}
# && echo "${USER}:${PW}" | chpasswd
# Couple of more commands to change dir, entry point etc. Example
LABEL version="1.0"
Run Code Online (Sandbox Code Playgroud)
此外,构建时不需要的标签、环境变量、暴露的端口和任何其他元数据通常最好保留在 Dockerfile 的末尾,因为它们对构建时间的影响最小,并且在构建时不需要错过缓存。他们改变了。
| 归档时间: |
|
| 查看次数: |
3332 次 |
| 最近记录: |