Skip to content

Commit e8d2025

Browse files
authored
Split docker image into multiple stages and improve cache behavior (#24447)
This PR does a major and a minor thing: * __Major__: splits the dockerfile into multiple stages: a 'common' stage which represents the build-and-run environment of the final image, a 'build' environment which contains all the sources etc. necessary to build Chapel, and a 'release' environment which contains the final build products etc. The multi-stage nature of the build allows files that we don't care about (the `.git` directory, the LLVM sources, most of the `test` directory) to be skipped when building the final image. This way, we are able to strip off at least 0.6GB from the image, and that's not including the size of `.git` (not included in my experiments). * __Minor__: Adds a `.dockerignore` file that ignores the `Dockerfile`. This way, the `COPY . .` has less of a chance to invalidate the cache as you are iterating on the docker image by tweaking the Dockerfile. Reviewed by @riftEmber -- thanks! ## Testing - [x] Image build succeeded - [x] Successfully used image in a devcontainer
2 parents 6593856 + b8c646b commit e8d2025

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dockerfile

Dockerfile

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
FROM debian:11
1+
# Common stage: install dependencies, set up the Debian image
2+
# ===========================================================
3+
4+
FROM debian:11 AS chapel-base
25

36
RUN apt-get update && apt-get install -y --no-install-recommends \
47
bash \
@@ -48,8 +51,15 @@ ENV CHPL_HOME=/opt/chapel \
4851
CHPL_GMP=system \
4952
CHPL_LLVM=system
5053

51-
# acquire sources
5254
WORKDIR $CHPL_HOME
55+
56+
57+
# Build stage: build Chapel from sources
58+
# ======================================
59+
60+
FROM chapel-base as chapel-build
61+
62+
# acquire sources
5363
COPY . .
5464

5565
# build Chapel for both C and LLVM backends
@@ -61,4 +71,21 @@ RUN make cleanall
6171

6272
# Hack to get access to Chapel binaries
6373
RUN cd $CHPL_HOME/bin && ln -s */* .
74+
75+
# The .git folder is huge and we really don't need it.
76+
RUN rm -rf .git
77+
RUN for subdir in `ls test || true`; do \
78+
if [ "$subdir" != "release" ]; then \
79+
rm -rf "test/$subdir"; \
80+
fi \
81+
done
82+
RUN rm -rf third-party/llvm/llvm-src
83+
84+
85+
# Final stage: copy build results, but omit large files.
86+
# ======================================
87+
88+
FROM chapel-base as chapel
89+
COPY --from=chapel-build $CHPL_HOME $CHPL_HOME
90+
6491
ENV PATH="${PATH}:${CHPL_HOME}/bin:${CHPL_HOME}/util"

0 commit comments

Comments
 (0)