代码之家  ›  专栏  ›  技术社区  ›  elethan

如何破解Dockerfile的FROM行缓存

  •  0
  • elethan  · 技术社区  · 6 年前

    FROM python:2.7
    
    RUN echo "Hello World"
    

    当我第一次用 docker build -f Dockerfile -t test . ,或使用 --no-cache 选项,我得到这个输出:

    Sending build context to Docker daemon  40.66MB
    Step 1/2 : FROM python:2.7
     ---> 6c76e39e7cfe
    Step 2/2 : RUN echo "Hello World"
     ---> Running in 5b5b88e5ebce
    Hello World
    Removing intermediate container 5b5b88e5ebce
     ---> a23687d914c2
    Successfully built a23687d914c2
    

    我的echo命令执行。

    Sending build context to Docker daemon  40.66MB
    Step 1/2 : FROM python:2.7
     ---> 6c76e39e7cfe
    Step 2/2 : RUN echo "Hello World"
     ---> Using cache
     ---> a23687d914c2
    Successfully built a23687d914c2
    Successfully tagged test-requirements:latest
    

    缓存用于步骤2/2,并且 Hello World 不执行。我可以通过使用 --没有缓存 . 但是,每次,即使我在使用 它使用缓存 python:2.7 echo 命令是缓存的,它没有说 ---> Using cache ).

    FROM python:2.7 行吗?我知道我能行 FROM python:latest

    2 回复  |  直到 6 年前
        1
  •  2
  •   vivekyad4v    6 年前

    如果我对上下文理解正确,您可以使用 --pull 使用时 docker build 获取最新的基本图像-

    $ docker build -f Dockerfile.test -t test . --pull

    所以两者都用 --no-cache --拉动 将使用Dockerfile创建一个绝对新鲜的图像-

    $ docker build -f Dockerfile.test -t test . --pull --no-cache

    发行- https://github.com/moby/moby/issues/4238

        2
  •  1
  •   DazWilkin    6 年前

    FROM 从注册表中提取图像(本例中为DockerHub)。

    在图像被拉出来生成构建后,如果运行 docker images

    您可以通过运行 docker rmi python:2.7 .