RafaelJan
RafaelJan

Reputation: 3606

docker multi-stage - why copy part of a folder resulting in bigger image than copy the entire folder?

I want to create the smallest image possible for my project, so I am using multi-stage docker with distroless. I copy only the files that needed for runtime. From several folders I copy only the .so files instead of copy all the folder and I can see that the created image get smaller.

But there is one folder that when I copy the entire folder I get smaller image significally than copy only the .so files.

With the following Dockerfile I copy the entire /usr/lib/x86_64-linux-gnu folder:

#stage 1
FROM some_image AS builder

ADD something /home/

RUN mkdir ...
    cmake ...

#stage 2
FROM gcr.io/distroless/cc-debian10

COPY --from=builder /home/some/folders
COPY --from=builder /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu

WORKDIR /home/

CMD ["/bin/bash"]

And the image size is 618MB.

But with the following Dockerfile I copy only the *.so files from /usr/lib/x86_64-linux-gnu folder:

#stage 1
FROM some_image AS builder

ADD something /home/

RUN mkdir ...
    cmake ...

#stage 2
FROM gcr.io/distroless/cc-debian10

COPY --from=builder /home/some/folders
COPY --from=builder /usr/lib/x86_64-linux-gnu/*.so /usr/lib/x86_64-linux-gnu/*.so.* /usr/lib/x86_64-linux-gnu/

WORKDIR /home/

CMD ["/bin/bash"]

And surprisingly the image size is now 683MB. Which is 65MB bigger.

Any idea what can cause such results?

Upvotes: 1

Views: 993

Answers (1)

RafaelJan
RafaelJan

Reputation: 3606

After long investigation I found that:

  • Copy of entire folder with symlinks preserves symlinks
  • Copy symlinks directly creates regular files

So in my case, copy of all *.so files cause each symlink to be copied as a regular file that this symlink is pointing on. This transition cause the increasing of my image size.

After I understand that, I google it and found a reference here

Upvotes: 3

Related Questions