首页 文章

如何强制Docker清理构建图像

提问于
浏览
386

我使用下面的命令从Docker文件构建了一个Docker镜像 .

$ docker build -t u12_core -f u12_core .

当我尝试使用相同的命令重建它时,它正在使用构建缓存,如:

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

缓存显示已安装aerospike . 但是,我没有在这个图像中产生的容器内找到它,所以我想重建这个图像而不使用缓存 . 如何在没有缓存的情况下强制Docker重建干净的图像?

4 回答

  • 712

    在你的情况下,我不建议使用 --no-cache .

    您正在运行从步骤3到9的几个安装(顺便说一句,我希望使用一个内衬),如果您不希望每次构建图像时重新运行这些步骤的开销,您可以在 wget 指令之前使用临时步骤修改 Dockerfile .

    我习惯做 RUN ls . 之类的事情并将其更改为 RUN ls ./ 然后 RUN ls ./. 依此类推,以便在 wget 检索到的tarball上完成的每个修改

    你当然可以像_208088那样增加每次迭代 'test1 中的数字 .

    它看起来很脏,但据我所知,它是继续受益于Docker缓存系统的最有效方式,当你有多层时节省时间......

  • 42

    在某些极端情况下,您可以通过运行以下方法来解决重复出现的构建失败:

    docker system prune
    

    该命令将要求您确认:

    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]
    

    这当然不是这个问题的直接答案,但可能会挽救一些生命......它确实拯救了我的生命 .

  • 7

    有一个 --no-cache 选项:

    docker build --no-cache -t u12_core -f u12_core .
    

    在较旧版本的Docker中,您需要传递 --no-cache=true ,但现在不再是这种情况了 .

  • 67

    命令 docker build --no-cache . 解决了我们类似的问题 .

    我们的Dockerfile是:

    RUN apt-get update
    RUN apt-get -y install php5-fpm
    

    但应该是:

    RUN apt-get update && apt-get -y install php5-fpm
    

    防止缓存更新并单独安装 .

    见:Best practices for writing Dockerfiles

相关问题