“泊坞窗图像”命令不显示正确的大小

我已经在这个repo中使用Dockerfile构build了一个图像,并且由于它在opencv源文件上做了一个make ,所以图像大小增加了3.1GB 。 所以,为了减less图像的大小,我在安装OpenCV后删除了源代码和构build文件,如果检查图像的大小,它确实显示大小已经减小,但是当我使用docker images命令检查时,大小依然是相同。

以下是docker build的输出

 RUN echo "Size of image before installing OpenCV" ---> Using cache ---> 6408d5fde660 Step 11 : RUN echo `du -sh /` ---> Running in 70e945c95f0c 1.9G / ---> 60f80a0d27e6 Removing intermediate container 70e945c95f0c Step 12 : RUN cd ~/ && git clone...... Step 14 : RUN echo "Size of image AFTER installing OpenCV" ---> Using cache ---> 1e065271efa7 Step 15 : RUN echo `du -sh /` ---> Running in 0f82de699dd9 5.9G / ---> a90660ac1f25 Removing intermediate container 0f82de699dd9 Step 16 : RUN echo "Size of OpenCV source and build directories" ---> Running in 7412b1e468e8 Size of OpenCV source and build directories ---> 252b6d0ef5b7 Removing intermediate container 7412b1e468e8 Step 17 : RUN echo `du -sh ~/opencv*` ---> Running in 8fddb31f4770 3.8G /root/opencv 218M /root/opencv_contrib ---> 3f9c355fec15 Removing intermediate container 8fddb31f4770 Step 18 : RUN rm -rf ~/opencv* # This removes ---> Running in 8a6c823f5675 ---> 96af8b3d01bf Removing intermediate container 8a6c823f5675 Step 19 : RUN echo "Size of image AFTER removing OpenCV source and build directories" ---> Running in e292c233f8db Size of image AFTER removing OpenCV source and build directories ---> b08582019c1b Removing intermediate container e292c233f8db Step 20 : RUN echo `du -sh ~/opencv*` ---> Running in 9cb62f3be3b3 du: cannot access '/root/opencv*': No such file or directory ---> 74473a783265 Removing intermediate container 9cb62f3be3b3 Step 21 : RUN echo `du -sh /` ---> Running in 45c9fcedf650 1.9G / ---> 0350a13a1a3b Removing intermediate container 45c9fcedf650 Successfully built 0350a13a1a3b 

从最后一步看,图像大小应该是1.9GB,但是当我做docker images它显示6.298GB

 ➜ d1 docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu_py latest 0350a13a1a3b 11 seconds ago 6.298 GB 

即使容器尺寸正确显示,

 ➜ d1 docker run -it ubuntu_py /bin/bash root@b549d9716e19:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@b549d9716e19:/# du -sh 1.9G . root@b549d9716e19:/# 

Docker层是不可变的。 一旦创build,它就在那里。 如果你想build立一些东西,然后删除用于构build的东西,那么你需要在同一个RUN命令中做这些事情或者它只是将所有东西都分层。

编辑:事实上,它也将有一个稍微更糟​​糕的效果来删除文件,因为你正在添加一个额外的层,其中的Docker必须淘汰你想删除的文件,增加大小:p