如何正确删除图像的先前提交,以便从头开始构建它

时间:2017-01-05 21:24:20

标签: docker dockerfile

经过一些尝试和播放后,我有一个Node.JS项目,我想打包。这是我的dockerfile:

FROM node:latest
MAINTAINER me <ma@me.me>
COPY ./backend-codebase /app
RUN /bin/bash -c 'cd /app; npm install'
EXPOSE 80
ENV NODE_ENV=production
CMD [ "sh", "-c", "cd /app; npm run start" ]

在构建它之前,我删除了之前已经拉出并构建的所有图像:

docker rmi $(docker images -q) -f
一切都建立起来并且有效,但是当我推动它时,我意识到尺寸对于我的小应用来说太大了。所以我试图深入了解它:

Matteos-MBP-2: matteo$ docker history 98e2a25bffe7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
98e2a25bffe7        28 seconds ago      /bin/sh -c #(nop)  CMD ["sh" "-c" "cd /app; n   0 B
9d2ee235ec5d        28 seconds ago      /bin/sh -c #(nop)  ENV NODE_ENV=production      0 B
f80a1e12748f        29 seconds ago      /bin/sh -c #(nop)  EXPOSE 80/tcp                0 B
c8353dfcd198        30 seconds ago      /bin/sh -c /bin/bash -c 'cd /app; npm install   0 B
e286e75eb2d9        40 seconds ago      /bin/sh -c #(nop) COPY dir:601eb26de2e2876ab1   18.93 MB
2e81fb2f03ab        44 seconds ago      /bin/sh -c #(nop)  MAINTAINER Me Me   0 B
7c4d899628d5        21 hours ago        /bin/sh -c #(nop)  CMD ["node"]                 0 B
<missing>           21 hours ago        /bin/sh -c curl -SLO "https://nodejs.org/dist   47.39 MB
<missing>           21 hours ago        /bin/sh -c #(nop)  ENV NODE_VERSION=7.4.0       0 B

<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NPM_CONFIG_LOGLEVEL=in   0 B
<missing>           2 weeks ago         /bin/sh -c set -ex   && for key in     9554F0   108.3 kB
<missing>           2 weeks ago         /bin/sh -c groupadd --gid 1000 node   && user   335.1 kB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    322.6 MB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    122.6 MB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    44.31 MB
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:1d214d2782eaccc743   123.1 MB

这里是:有些先前的提交仍然存在,这与一些旧的提交相关,当我还在玩它时,docker会产生这些提交(参见apt-gets?)。当我推动它时,它试图以某种方式推动它,尽管它说&#34;缺少&#34;在outpus中我删除了它们。如何在构建之前清理平板,以便重新开始并在节点上进行下一次提交:最新?谢谢

2 个答案:

答案 0 :(得分:2)

您可以找到一个非常详细的答案,解释为什么您在某些图层的<missing>输出中看到docker history,而不是其他图块,

https://stackoverflow.com/a/35312577/3738611

并在答案中找到了很大一部分信息:

http://www.windsock.io/explaining-docker-image-ids/

答案 1 :(得分:0)

您的docker rmi可能有错误。 -f需要在您要删除的图片ID之前:

docker rmi -f $(docker images -q)

那就是说,你的历史看起来非常好。您用于构建容器的源图像不会被重建,它们会被拉出,并且会显示上次构建时的历史记录,数小时和数周之前。

从下面的评论中,似乎有些人不相信7c4d899628d5以下的所有图层都来自节点图像。这是对我自己的本地环境的测试:

$ docker pull node:latest
latest: Pulling from library/node
75a822cd7888: Already exists
57de64c72267: Already exists
4306be1e8943: Already exists
871436ab7225: Pull complete
0110c26a367a: Pull complete
1f04fe713f1b: Pull complete
723bac39028e: Pull complete
Digest: sha256:08d77f1984cf79739ba7c987636cb871fd69745754200e5891a0c7ee2d9965b0
Status: Downloaded newer image for node:latest

$ docker history node:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
7c4d899628d5        23 hours ago        /bin/sh -c #(nop)  CMD ["node"]                 0 B
<missing>           23 hours ago        /bin/sh -c curl -SLO "https://nodejs.org/dist   47.39 MB
<missing>           23 hours ago        /bin/sh -c #(nop)  ENV NODE_VERSION=7.4.0       0 B
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NPM_CONFIG_LOGLEVEL=in   0 B
<missing>           2 weeks ago         /bin/sh -c set -ex   && for key in     9554F0   108.3 kB
<missing>           2 weeks ago         /bin/sh -c groupadd --gid 1000 node   && user   335.1 kB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    322.6 MB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    122.6 MB
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install    44.31 MB
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:1d214d2782eaccc743   123.1 MB