Reputation: 13
I'm now building docker image with a Dockerfile.
I download cmake and gcc source code, make and make install:
WORKDIR /root/source
RUN wget https://github.com/Kitware/CMake/releases/download/v3.12.4/cmake-3.12.4.tar.gz && tar xf cmake-3.12.4.tar.gz --remove-files
RUN git clone https://github.com/gcc-mirror/gcc.git -b gcc-6-branch --single-branch
RUN cd gcc && ./configure --prefix=/usr --disable-multilib && make -j
nproc
&& make installRUN cd cmake-3.12.4 && ./configure && gmake -j
nproc
&& make installWORKDIR /root
Now my docker image size is 11G, but source code used 8G. So I think source code is useless for me because I had make install
, so image size can be 3G if I rm source file?
This is my file system size:
[root@a8df8af9539e source]# du / -hl --max-depth=1
4.0K /lost+found
2.1G /usr
4.0K /media
107M /var
28K /tmp
0 /sys
4.0K /srv
4.0K /home
9.9M /etc
7.9G /root
0 /proc
4.0K /opt
4.0K /mnt
92K /run
0 /dev
4.0K /boot
11G /
I think yes.
Then I add RUN rm -rf /root/source
at the end of Dockerfile and rebuild this docker image, however image size is 11G also. List my file system size also:
[root@2a642ce974da ~]# du / -hl --max-depth=1
4.0K /lost+found
2.4G /usr
4.0K /media
78M /var
28K /tmp
0 /sys
4.0K /srv
4.0K /home
12M /etc
100K /root
0 /proc
4.0K /opt
4.0K /mnt
100K /run
0 /dev
4.0K /boot
2.5G /
Why? Where is my space and how can I free that 8G disk?
Upvotes: 1
Views: 593
Reputation: 1041
I think multi-stage build can help you here, since in the first stage you can build your code and then run it in the next stage.
# Stage 1: Build code
FROM <your image> as builder
RUN <your build instructions>
# Stage 2: Run code
FROM <your image>
# Copy artifacts from 'builder' stage
COPY --from=builder <src folder from builder> <target folder>
# Run your code
CMD [...]
You can find details in the documentation
Upvotes: 1
Reputation: 953
Every RUN
statement you add to Dockerfile creates a new layer and at the end all of the layers are merged (not directly merged, there are some rules) to build the final image. Even if you change/remove some file on an upper layer, it does not remove it from the lower layers, just shadows it somehow.
Here is an example Dockerfile:
FROM baseimage
RUN wget -O file http://...
RUN rm file
For the sake of simplicity, image that baseimage
is 100 MB, file
is 80 MB and there are three layers created for this build. First layer is 100MB, second one is 80 MB, last one just has some metadata to indicate the file from second layer is removed. So resulting image is 180 MB (consists if three layer) even if you remove 80 MB at last layer.
But if you get, install and remove on same layer, you will see the difference:
FROM baseimage
RUN wget -O file http://... && rm file
Now your first layer is 100 MB and second layer is ~ 0 MB (since you remove the file in same RUN
statement), and the your image is 100 MB.
In a nutshell, you should install and remove at the same RUN
statement.
Upvotes: 2