From 857d7c1afe153f38e8102f757b3d47aaf3da6a75 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Sat, 11 May 2024 11:29:51 +0200 Subject: [PATCH 1/3] Dockerfile: merge two layers Combine the RUN statements into a single statement. The Docker cache behavior remains the same, but there is one layer less in the produced image. --- Dockerfile | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index a4c897d8f..fde29b0a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,11 @@ ENV DEBIAN_FRONTEND=noninteractive \ LANG=C.UTF-8 \ PYTHONFAULTHANDLER=1 +# Environment can be whatever is supported by setup.py +# so, either deployment, test +ARG ENVIRONMENT=deployment +# ARG ENVIRONMENT=test + # Apt installation # git: required by setuptools_scm. RUN apt-get update && \ @@ -40,16 +45,10 @@ RUN apt-get update && \ python3-pip \ && apt-get autoclean && \ apt-get autoremove && \ - rm -rf /var/lib/{apt,dpkg,cache,log} - -# Environment can be whatever is supported by setup.py -# so, either deployment, test -ARG ENVIRONMENT=deployment -# ARG ENVIRONMENT=test - -RUN echo "Environment is: $ENVIRONMENT" && \ - [ "$ENVIRONMENT" = "deployment" ] || \ - pip install --disable-pip-version-check pip-tools pytest-cov + rm -rf /var/lib/{apt,dpkg,cache,log} && \ + echo "Environment is: $ENVIRONMENT" && \ + ([ "$ENVIRONMENT" = "deployment" ] || \ + pip install --disable-pip-version-check pip-tools pytest-cov) # Set up a nice workdir and add the live code ENV APPDIR=/code From 448cd9e188b956f63df84ff55111febe6f89e644 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Sat, 11 May 2024 11:36:50 +0200 Subject: [PATCH 2/3] Dockerfile: merge two more layers Combine the RUN statements into a single statement. The Docker cache behavior remains the same, but there is one layer less in the produced image. --- Dockerfile | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index fde29b0a1..7df7ec46f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,14 +55,13 @@ ENV APPDIR=/code WORKDIR $APPDIR COPY . $APPDIR -COPY --from=builder --link /build/*.whl ./ -RUN python3.10 -m pip --disable-pip-version-check -q install *.whl && \ - rm *.whl - # These ENVIRONMENT flags make this a bit complex, but basically, if we are in dev # then we want to link the source (with the -e flag) and if we're in prod, we # want to delete the stuff in the /code folder to keep it simple. -RUN if [ "$ENVIRONMENT" = "deployment" ] ; then\ +COPY --from=builder --link /build/*.whl ./ +RUN python3.10 -m pip --disable-pip-version-check -q install *.whl && \ + rm *.whl && \ + if [ "$ENVIRONMENT" = "deployment" ] ; then\ pip --no-cache-dir --disable-pip-version-check install .[$ENVIRONMENT]; \ rm -rf /code/* /code/.git* ; \ else \ From eb9e481d1f617ccd694b39ca0703336b667f2f49 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Sat, 11 May 2024 11:39:59 +0200 Subject: [PATCH 3/3] Dockerfile: fail on errors Rewrite the if-then-else into short-circuiting tests followed by commands chained together with &&. This makes building the image fail if something is wrong, instead of giving strange errors when running the tests. --- Dockerfile | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7df7ec46f..0cf231c1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -61,12 +61,11 @@ COPY . $APPDIR COPY --from=builder --link /build/*.whl ./ RUN python3.10 -m pip --disable-pip-version-check -q install *.whl && \ rm *.whl && \ - if [ "$ENVIRONMENT" = "deployment" ] ; then\ - pip --no-cache-dir --disable-pip-version-check install .[$ENVIRONMENT]; \ - rm -rf /code/* /code/.git* ; \ - else \ - pip --disable-pip-version-check install --editable .[$ENVIRONMENT]; \ - fi && \ + ([ "$ENVIRONMENT" = "deployment" ] || \ + pip --disable-pip-version-check install --editable .[$ENVIRONMENT]) && \ + ([ "$ENVIRONMENT" != "deployment" ] || \ + (pip --no-cache-dir --disable-pip-version-check install .[$ENVIRONMENT] && \ + rm -rf /code/* /code/.git*)) && \ pip freeze && \ ([ "$ENVIRONMENT" != "deployment" ] || \ apt-get remove -y \