Reputation: 10483
I've built an image using the Dockerfile in this repo and since it does a make
on the opencv source files the image size increases by 3.1 GB.
So, in order to reduce the size of the image I removed the source and build files after installing OpenCV and if I check the size of the image it does show that the size has reduced but when I check using docker images
command the size remains the same.
Here's the output from the 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
From the last step it looks like the image size should be 1.9GB but when I do docker images
it shows me 6.298 GB
➜ d1 docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_py latest 0350a13a1a3b 11 seconds ago 6.298 GB
Even the container size is shown correctly,
➜ 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:/#
Upvotes: 1
Views: 318
Reputation: 18926
Docker layers are immutable. Once it's created, it's there to stay. If you want to build something then remove the cruft used to build it then you need to do those things in the same RUN
command or it's just going to layer everything.
Edit: In fact, it's also going to have a marginally worse effect to delete the files as you are, as you're adding an additional layer where Docker has to whiteout the files you want to delete, increasing the size :p
Upvotes: 2