我使用Dockerfile in this repo构建了一个图像,因为它在opencv源文件上执行了make
,图像大小增加了3.1 GB。
因此,为了减小图像的大小,我在安装OpenCV后删除了源文件和构建文件,如果我检查图像的大小,它确实显示大小已经减少但是当我使用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.298 GB
➜ 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:/#
答案 0 :(得分:2)
Docker图层是不可变的。一旦它被创建,它就会留下来。如果你想构建一些东西,然后移除用于构建它的残余物,那么你需要在同一个RUN
命令中执行这些事情,或者只是将所有内容分层。
编辑:事实上,删除文件也会产生略差效果,因为你要添加一个额外的图层,其中Docker必须 whiteout 要删除的文件,增加大小:p