Skip to content

Commit a6cffa0

Browse files
authored
[RFC] Shrink musl docker image size by ~50% (#1670)
Here is a proof of concept patchset which reduces the size of the musl images from ~2.4G to ~1.2G, on the two architectures I tried (x86_64 and aarch64). Using aarch64 as a primary testbed, I got these results: * Baseline (edge): image size 2.39G (794M gzipped) * Patch 1 (strip the binaries in musl.sh): 1.55G * Add patch 2 (flatten the output image): 1.31G * Add patch 3 (additional tidyup script): 1.22G (348M gzipped). A space/bandwidth saving of 47% uncompressed, 56% compressed. I've tested the result with my use cases (x86_64 and aarch64). The crate I am cross-compiling depends on `ring` which has a C component. There is almost certainly more squeezing that could be done, but the Law of Diminishing Returns kicks in somewhere. For example, I spotted copies of identical binaries in /usr/local/bin which could be symlinked together to save a few more MB, but coming up with a safe and robust solution there might take a little thought. The majority of these savings are specific to the musl builds. I haven't analysed any of the non-musl images but there could be scope for improvement there as well. If you like this approach, I'll happily copy & paste the tidyup & flatten part into the other dockerfiles. OTOH, there may be other ways to achieve this; happy to discuss.
2 parents 50ccc96 + df33097 commit a6cffa0

18 files changed

+182
-1
lines changed

docker/Dockerfile.aarch64-unknown-linux-musl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ RUN /qemu.sh aarch64
1818
COPY musl.sh /
1919
RUN /musl.sh TARGET=aarch64-linux-musl
2020

21+
COPY tidyup.sh /
22+
RUN /tidyup.sh
23+
24+
FROM scratch AS final
25+
COPY --from=build / /
26+
CMD ["/bin/bash"]
27+
2128
ENV CROSS_TOOLCHAIN_PREFIX=aarch64-linux-musl-
2229
ENV CROSS_SYSROOT=/usr/local/aarch64-linux-musl
2330
COPY musl-symlink.sh /

docker/Dockerfile.arm-unknown-linux-musleabi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ RUN /musl.sh \
2222
--with-float=soft \
2323
--with-mode=arm"
2424

25+
COPY tidyup.sh /
26+
RUN /tidyup.sh
27+
28+
FROM scratch AS final
29+
COPY --from=build / /
30+
CMD ["/bin/bash"]
31+
2532
ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-musleabi-
2633
ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabi
2734
COPY musl-symlink.sh /

docker/Dockerfile.arm-unknown-linux-musleabihf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ RUN /musl.sh \
2323
--with-float=hard \
2424
--with-mode=arm"
2525

26+
COPY tidyup.sh /
27+
RUN /tidyup.sh
28+
29+
FROM scratch AS final
30+
COPY --from=build / /
31+
CMD ["/bin/bash"]
32+
2633
ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-musleabihf-
2734
ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabihf
2835
COPY musl-symlink.sh /

docker/Dockerfile.armv5te-unknown-linux-musleabi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ RUN /musl.sh \
2222
--with-float=soft \
2323
--with-mode=arm"
2424

25+
COPY tidyup.sh /
26+
RUN /tidyup.sh
27+
28+
FROM scratch AS final
29+
COPY --from=build / /
30+
CMD ["/bin/bash"]
31+
2532
ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-musleabi-
2633
ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabi
2734
COPY musl-symlink.sh /

docker/Dockerfile.armv7-unknown-linux-musleabi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ RUN /musl.sh \
2323
--with-mode=thumb \
2424
--with-mode=arm"
2525

26+
COPY tidyup.sh /
27+
RUN /tidyup.sh
28+
29+
FROM scratch AS final
30+
COPY --from=build / /
31+
CMD ["/bin/bash"]
32+
2633
ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-musleabi-
2734
ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabi
2835
COPY musl-symlink.sh /

docker/Dockerfile.armv7-unknown-linux-musleabihf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ RUN /musl.sh \
2323
--with-mode=thumb \
2424
--with-fpu=vfp"
2525

26+
COPY tidyup.sh /
27+
RUN /tidyup.sh
28+
29+
FROM scratch AS final
30+
COPY --from=build / /
31+
CMD ["/bin/bash"]
32+
2633
ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-musleabihf-
2734
ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabihf
2835
COPY musl-symlink.sh /

docker/Dockerfile.i586-unknown-linux-musl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ RUN /musl.sh TARGET=i586-linux-musl
1818
COPY qemu.sh /
1919
RUN /qemu.sh i386
2020

21+
COPY tidyup.sh /
22+
RUN /tidyup.sh
23+
24+
FROM scratch AS final
25+
COPY --from=build / /
26+
CMD ["/bin/bash"]
27+
2128
ENV CROSS_TOOLCHAIN_PREFIX=i586-linux-musl-
2229
ENV CROSS_SYSROOT=/usr/local/i586-linux-musl
2330
COPY musl-symlink.sh /

docker/Dockerfile.i686-unknown-linux-musl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ RUN /musl.sh TARGET=i686-linux-musl
1818
COPY qemu.sh /
1919
RUN /qemu.sh i386
2020

21+
COPY tidyup.sh /
22+
RUN /tidyup.sh
23+
24+
FROM scratch AS final
25+
COPY --from=build / /
26+
CMD ["/bin/bash"]
27+
2128
ENV CROSS_TOOLCHAIN_PREFIX=i686-linux-musl-
2229
ENV CROSS_SYSROOT=/usr/local/i686-linux-musl
2330
COPY musl-symlink.sh /

docker/Dockerfile.loongarch64-unknown-linux-musl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ RUN /qemu.sh loongarch64
2828
COPY qemu-runner base-runner.sh /
2929
COPY toolchain.cmake /opt/toolchain.cmake
3030

31+
COPY tidyup.sh /
32+
RUN /tidyup.sh
33+
34+
FROM scratch AS final
35+
COPY --from=build / /
36+
CMD ["/bin/bash"]
37+
ENV PATH=/x-tools/loongarch64-unknown-linux-musl/bin/:$PATH
38+
3139
ENV CROSS_TOOLCHAIN_PREFIX=loongarch64-unknown-linux-musl-
3240
ENV CROSS_SYSROOT=/x-tools/loongarch64-unknown-linux-musl/loongarch64-unknown-linux-musl/sysroot/
3341
ENV CROSS_TARGET_RUNNER="/qemu-runner loongarch64"

docker/Dockerfile.mips-unknown-linux-musl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ RUN /musl.sh \
2222
TARGET=mips-linux-muslsf \
2323
"COMMON_CONFIG += -with-arch=mips32r2"
2424

25+
COPY tidyup.sh /
26+
RUN /tidyup.sh
27+
28+
FROM scratch AS final
29+
COPY --from=build / /
30+
CMD ["/bin/bash"]
31+
2532
ENV CROSS_TOOLCHAIN_PREFIX=mips-linux-muslsf-
2633
ENV CROSS_SYSROOT=/usr/local/mips-linux-muslsf
2734
COPY musl-symlink.sh /

0 commit comments

Comments
 (0)