Skip to content

Commit ae71b66

Browse files
committed
Mount wheels from build stage instead of copying
- avoids copying wheels, eliminating the need to track which need compilers on arm - mount pip cache as a cache dir, so cache can be used without polluting the image Requires buildkit for --mount results: smaller images, simpler Dockerfiles, faster rebuilds
1 parent 52ad148 commit ae71b66

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

images/hub/Dockerfile

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# syntax = docker/dockerfile:1.3
12
# The build stage
23
# ---------------
34
FROM python:3.9-bullseye as build-stage
@@ -6,21 +7,16 @@ FROM python:3.9-bullseye as build-stage
67

78
WORKDIR /build-stage
89

9-
# Build wheels for packages that require gcc or other build dependencies and
10-
# lack wheels either for amd64 or aarch64.
11-
#
12-
# - https://pypi.org/project/pycurl/#files, no wheels available as of 7.45.1.
13-
# See https://github.com/pycurl/pycurl/issues/738.
14-
#
15-
# If you find a dependency here no longer listed in requirements.txt, remove it
16-
# from here as well. The more wheels we build here and copy and then install,
17-
# where the copy and install are separate steps, the larger the final image
18-
# becomes.
19-
#
10+
# set env for pip cache,
11+
# but use ARG to avoid persisting in image
12+
ARG PIP_CACHE_DIR=/tmp/pip-cache
13+
14+
# Build wheels
15+
# These are mounted into the final image for installation
2016
COPY requirements.txt requirements.txt
21-
RUN pip install build \
22-
&& pip wheel \
23-
$(cat requirements.txt | grep "pycurl==")
17+
RUN --mount=type=cache,target=${PIP_CACHE_DIR} \
18+
pip install build \
19+
&& pip wheel -r requirements.txt
2420

2521

2622
# The final stage
@@ -30,6 +26,7 @@ FROM python:3.9-slim-bullseye
3026
ARG NB_USER=jovyan
3127
ARG NB_UID=1000
3228
ARG HOME=/home/jovyan
29+
3330
ENV DEBIAN_FRONTEND=noninteractive
3431

3532
RUN adduser --disabled-password \
@@ -56,10 +53,16 @@ RUN apt-get update && \
5653
tini \
5754
&& rm -rf /var/lib/apt/lists/*
5855

59-
COPY --from=build-stage /build-stage/*.whl /tmp/pre-built-wheels/
56+
# set env for pip cache,
57+
# but use ARG to avoid persisting in image
58+
ARG PIP_CACHE_DIR=/tmp/pip-cache
59+
60+
# install wheels built in the build-stage
6061
COPY requirements.txt /tmp/requirements.txt
61-
RUN pip install --no-cache-dir \
62-
/tmp/pre-built-wheels/*.whl \
62+
RUN --mount=type=cache,target=${PIP_CACHE_DIR} \
63+
--mount=type=cache,from=build-stage,source=/build-stage,target=/tmp/wheels \
64+
pip install \
65+
--find-links /tmp/wheels/ \
6366
-r /tmp/requirements.txt
6467

6568
WORKDIR /srv/jupyterhub

images/singleuser-sample/Dockerfile

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# syntax = docker/dockerfile:1.3
12
# The build stage
23
# ---------------
34
FROM python:3.9-bullseye as build-stage
@@ -6,21 +7,15 @@ FROM python:3.9-bullseye as build-stage
67

78
WORKDIR /build-stage
89

9-
# Build wheels for packages that require gcc or other build dependencies and
10-
# lack wheels either for amd64 or aarch64.
11-
#
12-
# - https://pypi.org/project/psutil/#files, no wheels available as of 5.9.1
13-
# for aarch64. See https://github.com/giampaolo/psutil/pull/2070.
14-
#
15-
# If you find a dependency here no longer listed in requirements.txt, remove it
16-
# from here as well. The more wheels we build here and copy and then install,
17-
# where the copy and install are separate steps, the larger the final image
18-
# becomes.
19-
#
10+
# set env for pip cache,
11+
# but use ARG to avoid persisting in image
12+
ARG PIP_CACHE_DIR=/tmp/pip-cache
13+
14+
# These are mounted into the final image for installation
2015
COPY requirements.txt requirements.txt
21-
RUN pip install build \
22-
&& pip wheel \
23-
$(cat requirements.txt | grep "psutil==")
16+
RUN --mount=type=cache,target=${PIP_CACHE_DIR} \
17+
pip install build \
18+
&& pip wheel -r requirements.txt
2419

2520

2621
# The final stage
@@ -53,10 +48,16 @@ RUN apt-get update \
5348
git \
5449
&& rm -rf /var/lib/apt/lists/*
5550

56-
COPY --from=build-stage /build-stage/*.whl /tmp/pre-built-wheels/
51+
# set env for pip cache,
52+
# but use ARG to avoid persisting in image
53+
ARG PIP_CACHE_DIR=/tmp/pip-cache
54+
55+
# install wheels built in the build-stage
5756
COPY requirements.txt /tmp/requirements.txt
58-
RUN pip install --no-cache-dir \
59-
/tmp/pre-built-wheels/*.whl \
57+
RUN --mount=type=cache,target=${PIP_CACHE_DIR} \
58+
--mount=type=cache,from=build-stage,source=/build-stage,target=/tmp/wheels \
59+
pip install \
60+
--find-links /tmp/wheels/ \
6061
-r /tmp/requirements.txt
6162

6263
WORKDIR ${HOME}

0 commit comments

Comments
 (0)