本文翻译自:How to force Docker for a clean build of an image
I have build a Docker image from a Docker file using the below command. 我已经使用以下命令从Docker文件构建了一个Docker映像。
$ docker build -t u12_core -f u12_core .
When I am trying to rebuild it with the same command, it's using the build cache like: 当我尝试使用同一命令重建它时,它正在使用构建缓存,例如:
Step 1 : FROM ubuntu:12.04
---> eb965dfb09d2
Step 2 : MAINTAINER Pavan Gupta <pavan.gupta@gmail.com>
---> Using cache
---> 4354ccf9dcd8
Step 3 : RUN apt-get update
---> Using cache
---> bcbca2fcf204
Step 4 : RUN apt-get install -y openjdk-7-jdk
---> Using cache
---> 103f1a261d44
Step 5 : RUN apt-get install -y openssh-server
---> Using cache
---> dde41f8d0904
Step 6 : RUN apt-get install -y git-core
---> Using cache
---> 9be002f08b6a
Step 7 : RUN apt-get install -y build-essential
---> Using cache
---> a752fd73a698
Step 8 : RUN apt-get install -y logrotate
---> Using cache
---> 93bca09b509d
Step 9 : RUN apt-get install -y lsb-release
---> Using cache
---> fd4d10cf18bc
Step 10 : RUN mkdir /var/run/sshd
---> Using cache
---> 63b4ecc39ff0
Step 11 : RUN echo 'root:root' | chpasswd
---> Using cache
---> 9532e31518a6
Step 12 : RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
---> Using cache
---> 47d1660bd544
Step 13 : RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
---> Using cache
---> d1f97f1c52f7
Step 14 : RUN wget -O aerospike.tgz 'http://aerospike.com/download/server/latest/artifact/ubuntu12'
---> Using cache
---> bd7dde7a98b9
Step 15 : RUN tar -xvf aerospike.tgz
---> Using cache
---> 54adaa09921f
Step 16 : RUN dpkg -i aerospike-server-community-*/*.deb
---> Using cache
---> 11aba013eea5
Step 17 : EXPOSE 22 3000 3001 3002 3003
---> Using cache
---> e33aaa78a931
Step 18 : CMD /usr/sbin/sshd -D
---> Using cache
---> 25f5fe70fa84
Successfully built 25f5fe70fa84
The cache shows that aerospike is installed. 缓存显示已安装Aerospike。 However, I don't find it inside containers spawn from this image, so I want to rebuild this image without using the cache. 但是,我在从该图像生成的容器中找不到它,因此我想在不使用缓存的情况下重建该图像。 How can I force Docker to rebuild a clean image without the cache? 如何强制Docker在没有缓存的情况下重建干净的映像?
#1楼
参考:https://stackoom.com/question/2PLsh/如何强制Docker进行映像的干净构建
#2楼
There's a --no-cache
option: 有一个--no-cache
选项:
docker build --no-cache -t u12_core -f u12_core .
In older versions of Docker you needed to pass --no-cache=true
, but this is no longer the case. 在旧版本的Docker中,您需要传递--no-cache=true
,但现在不再如此。
#3楼
The command docker build --no-cache .
命令docker build --no-cache .
solved our similar problem. 解决了我们类似的问题。
Our Dockerfile was: 我们的Dockerfile是:
RUN apt-get update
RUN apt-get -y install php5-fpm
But should have been: 但是应该是:
RUN apt-get update && apt-get -y install php5-fpm
To prevent caching the update and install separately. 为防止缓存更新并单独安装。
See: Best practices for writing Dockerfiles 请参阅: 编写Dockerfile的最佳实践
#4楼
In some extreme cases, your only way around recurring build failures is by running: 在某些极端情况下,避免重复发生构建失败的唯一方法是运行:
docker system prune
The command will ask you for your confirmation: 该命令将要求您确认:
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N]
This is of course not a direct answer to the question, but might save some lives... It did save mine. 这当然不是问题的直接答案,但可以挽救一些生命……确实挽救了我的生命。
#5楼
I would not recommend using --no-cache
in your case. 我不建议您使用--no-cache
。
You are running a couple of installations from step 3 to 9 (I would, by the way, prefer using a one liner) and if you don't want the overhead of re-running these steps each time you are building your image you can modify your Dockerfile
with a temporary step prior to your wget
instruction. 您从步骤3到步骤9运行了几次安装(顺便说一句,我宁愿使用一个衬垫),如果您不希望每次构建映像时都需要重新运行这些步骤,则可以在wget
指令之前,通过一个临时步骤修改Dockerfile
。
I use to do something like RUN ls .
我曾经做过类似RUN ls .
事情RUN ls .
and change it to RUN ls ./
then RUN ls ./.
并将其更改为RUN ls ./
然后更改为RUN ls ./.
and so on for each modification done on the tarball retrieved by wget
对wget
检索到的压缩包进行的每个修改,依此类推
You can of course do something like RUN echo 'test1' > test && rm test
increasing the number in 'test1
for each iteration. 当然,您可以执行类似RUN echo 'test1' > test && rm test
增加每次迭代的'test1
的数字。
It looks dirty, but as far as I know it's the most efficient way to continue benefiting from the cache system of Docker, which saves time when you have many layers... 它看起来很脏,但是据我所知,这是继续受益于Docker缓存系统的最有效方法,当您拥有多个层时,它可以节省时间...
#6楼
You can manage the builder cache with docker builder
您可以使用docker builder
管理构建器缓存
To clean all the cache with no prompt: docker builder prune -af
要在不提示的情况下清除所有缓存: docker builder prune -af