Search code examples
dockerdocker-image

why my docker image bigger than du -hd 1 /


when i use openjdk:8u171-alpine to build my java application.

The image size is bigger than what i think.

The openjdk:8u171-alpine is only 103MB, and my test jar file about 30MB.

But my new docker images is 160MB.

I don't know why.So I use docker run --rm -it openjdk:8u171-alpine du -hd 1 .The output is as follows

4.0K    ./home
4.0K    ./srv
1.6M    ./etc
4.0K    ./root
2.8M    ./lib
4.0K    ./mnt
95.8M   ./usr
16.0K   ./media
0       ./sys
0       ./dev
224.0K  ./sbin
784.0K  ./bin
4.0K    ./run
0       ./proc
4.0K    ./tmp
68.0K   ./var
101.3M  .

and when I run same in my docker images

4.0K    /home
4.0K    /srv
1.6M    /etc
8.0K    /root
2.8M    /lib
4.0K    /mnt
95.8M   /usr
16.0K   /media
0       /sys
0       /dev
224.0K  /sbin
784.0K  /bin
4.0K    /run
0       /proc
4.0K    /tmp
68.0K   /var
29.8M   /app
4.0K    /logs
131.1M  /

It only 131.1MB, but when I use docker images the output shows 160MB

d3ac9dcbe21c   20 minutes ago   165MB

This is my dockerfile

FROM openjdk:8u171-alpine
MAINTAINER zhujiaxin<[email protected]>
ENV LOGLEVEL=info
VOLUME ["/logs"]
RUN mkdir /app
WORKDIR /app
ARG JAR_FILE
COPY /target/$JAR_FILE app.jar
RUN chmod 775 app.jar
ENTRYPOINT ["java","-jar","app.jar"]
CMD []
EXPOSE 8080

My jar file only 30M


Solution

  • The docker layered filesystem is copy-on-write, and that copy is on a file level, not individual filesystem blocks. That means a change to a permission bit on a file with a chmod results in copying the entire file to a new layer:

    COPY /target/$JAR_FILE app.jar
    RUN chmod 775 app.jar
    

    The result is a 30MB file would take up 2x that space, once for each layer, with the different permissions. Ideally, you'd fix the file permissions at the source to avoid the need to chmod the file. Then you'd only need the COPY step without the added chmod in a RUN:

    COPY /target/$JAR_FILE app.jar