Skip to content

Commit

Permalink
Dockerfile: trim GCC installation by 52 MB (#66)
Browse files Browse the repository at this point in the history
This commit reduces our image size by about 50 MB by deleting some
large, unnecessary files.

Image size before this commit: 139.68 MB
Image size with this commit:    87.48 MB

The majority of the image size comes from the GCC installation, but we
don't need things like LTO support or the D standard library in our
image.

Therefore this commit removes these large files:
21.5M    /usr/libexec/gcc/x86_64-alpine-linux-musl/10.2.1/lto1
21.5M    /usr/bin/lto-dump
5.8M     /usr/lib/libgphobos.so.1.0.0
976.0K   /usr/libexec/gcc/x86_64-alpine-linux-musl/10.2.1/lto-wrapper

The largest remaining files in the image:
$ find / -type f -exec du -k {} + | sort -nr | head -n20 | cut -f2 | xargs du -sh
22.4M    /usr/libexec/gcc/x86_64-alpine-linux-musl/10.2.1/cc1
9.4M     /usr/lib/libc.a
4.6M     /nim/bin/nim
2.9M     /usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/libgcc.a
2.6M     /usr/lib/libmpfr.so.6.1.0
2.5M     /lib/libcrypto.so.1.1
1.6M     /usr/lib/libstdc++.so.6.0.28
1.6M     /usr/lib/libisl.so.22.0.0
1.6M     /usr/bin/ld
1.5M     /usr/bin/dwp
1.3M     /usr/lib/libopcodes-2.35.2.so
1.2M     /usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/plugin/gtype.state
1.1M     /usr/lib/libgdruntime.so.1.0.0
1.1M     /usr/lib/libbfd-2.35.2.so
1.1M     /usr/bin/gcc
1.1M     /usr/bin/cpp
952.0K   /usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/plugin/include/insn-flags.h
808.0K   /bin/busybox
604.0K   /usr/bin/as
592.0K   /lib/ld-musl-x86_64.so.1

The final disk usage in the root directory:
$ du -k -d1 / | sort -nr | cut -f2 | xargs du -sh
87.9M    /
67.6M    /usr
9.9M     /nim
3.9M     /lib
1.1M     /bin
428.0K   /etc
324.0K   /sbin
12.0K    /var
0        /tmp
0        /srv
0        /run
0        /root
0        /opt
0        /mnt
0        /media
0        /home

Note that:
- The `busybox` `sort` available in the image from doesn't have the `-h`
  option, hence the above convoluted piping.
- We can't delete the files by force-removing dependencies of the `gcc`
  package. That is, something like `apk del --force libgphobos` doesn't
  work.
  • Loading branch information
ee7 authored Apr 27, 2021
1 parent 7bd66d8 commit 282e388
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ COPY src/runner.nim /build/
COPY src/unittest_json.nim /build/
RUN /nim/bin/nim c -d:release -d:lto -d:strip /build/runner.nim

FROM base
FROM ${REPO}:${IMAGE}
COPY --from=nim_builder /nim/ /nim/
# hadolint ignore=DL3018
RUN apk add --no-cache \
gcc \
musl-dev \
pcre \
&& ln -s /nim/bin/nim /usr/local/bin/nim
&& ln -s /nim/bin/nim /usr/local/bin/nim \
&& printf '\nRemoving some unneeded large files:\n' \
&& rm -v /usr/bin/lto-dump \
&& find / -path '/usr/libexec/gcc/x86_64-alpine-linux-musl/*/lto*' -exec rm -v {} + \
&& find / -path '/usr/lib/libgphobos.so*' -exec rm -v {} +
WORKDIR /opt/test-runner/
COPY --from=runner_builder /build/runner bin/
COPY bin/run.sh bin/
Expand Down

0 comments on commit 282e388

Please sign in to comment.