Skip to content

Commit f97a7e2

Browse files
authored
Replace bind mount method with base chroot in trainer development material (#20)
This is our recommended method for installing apk packages into distroless images.
1 parent e2ac4e7 commit f97a7e2

2 files changed

Lines changed: 17 additions & 44 deletions

File tree

train-the-trainer/developer-track/python-example-2/Dockerfile

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ USER root
88
# Comment out or remove if specified system-level packages do not need to be present during the build stage
99
RUN apk add --no-cache ${APK_LIST}
1010

11-
# Build a script used to install packages during the distroless stage
12-
# The find commands allow us to avoid hardcoding the version number of libapk.so
13-
RUN echo "cp /lib/libapk.so $(find /lib -name 'libapk.so*')" > /add_apks.sh
14-
RUN echo "apk add --no-cache ${APK_LIST}" >> /add_apks.sh
15-
RUN echo "rm $(find /lib -name 'libapk.so*')" >> /add_apks.sh
16-
RUN cp $(find /lib -name 'libapk.so*') /lib/libapk.so
11+
# Install the packages into a root derived from the distroless image.
12+
COPY --from=cgr.dev/chainguard/python:latest / /base-chroot
13+
RUN apk add --no-commit-hooks --no-cache --root /base-chroot ${APK_LIST}
14+
1715
USER 65532
1816

1917
# Install desired Python packages
@@ -27,24 +25,13 @@ RUN pip install --upgrade pip setuptools wheel && \
2725

2826
FROM cgr.dev/chainguard/python:latest
2927

28+
# Replace the filesystem with the one containing the additional packages.
29+
COPY --from=dev /base-chroot /
30+
3031
WORKDIR /app
3132
COPY --from=dev /app/venv /app/venv
3233
COPY run.py run.py
33-
USER root
3434

35-
# Mount resources needed to install apks and run the install script
36-
RUN --mount=type=bind,from=dev,target=/sbin/apk,source=/sbin/apk \
37-
--mount=type=bind,from=dev,target=/bin/busybox,source=/bin/busybox \
38-
--mount=type=bind,from=dev,target=/bin/sh,source=/bin/sh \
39-
--mount=type=bind,from=dev,target=/bin/cp,source=/bin/cp \
40-
--mount=type=bind,from=dev,target=/bin/rm,source=/bin/rm \
41-
--mount=type=bind,from=dev,target=/etc/apk/keys,source=/etc/apk/keys,rw \
42-
--mount=type=bind,from=dev,target=/etc/apk/repositories,source=/etc/apk/repositories,rw \
43-
--mount=type=bind,from=dev,target=/etc/apk/protected_paths.d,source=/etc/apk/protected_paths.d,rw \
44-
--mount=type=bind,from=dev,target=/lib/libapk.so,source=/lib/libapk.so,rw \
45-
--mount=type=bind,from=dev,target=/add_apks.sh,source=/add_apks.sh,rw \
46-
/bin/sh /add_apks.sh
47-
48-
USER 65532
4935
ENV PATH="/app/venv/bin:$PATH"
36+
5037
ENTRYPOINT ["python", "run.py"]

train-the-trainer/developer-track/python-example-3/Dockerfile

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ ENV PATH="/app/venv/bin":$PATH
1313
# Comment out or remove if specified system-level packages do not need to be present during the build stage
1414
RUN apk add --no-cache ${APK_LIST}
1515

16-
# Build a script used to install packages during the distroless stage
17-
# The find commands allow us to avoid hardcoding the version number of libapk.so
18-
RUN echo "cp /lib/libapk.so $(find /lib -name 'libapk.so*')" > /add_apks.sh
19-
RUN echo "apk add --no-cache ${APK_LIST}" >> /add_apks.sh
20-
RUN echo "rm $(find /lib -name 'libapk.so*')" >> /add_apks.sh
21-
RUN cp $(find /lib -name 'libapk.so*') /lib/libapk.so
16+
# Install the packages into a root derived from the distroless image.
17+
COPY --from=cgr.dev/chainguard/python:latest / /base-chroot
18+
RUN apk add --no-commit-hooks --no-cache --root /base-chroot ${APK_LIST}
19+
2220
USER 0
2321

2422
# Install desired Python packages
@@ -32,27 +30,15 @@ RUN pip install --upgrade pip setuptools wheel && \
3230

3331
FROM cgr.dev/chainguard/python:latest
3432

33+
# Replace the filesystem with the one containing the additional packages.
34+
COPY --from=dev /base-chroot /
35+
3536
WORKDIR /app
3637

3738
COPY --from=dev /app/__pycache__ /app/__pycache__
38-
COPY --from=dev add_apks.sh add_apks.sh
3939
COPY --from=dev /app/venv /app/venv
4040
COPY main.py main.py
41-
USER root
4241

43-
# Mount resources needed to install apks and run the install script
44-
RUN --mount=type=bind,from=dev,target=/sbin/apk,source=/sbin/apk \
45-
--mount=type=bind,from=dev,target=/bin/busybox,source=/bin/busybox \
46-
--mount=type=bind,from=dev,target=/bin/sh,source=/bin/sh \
47-
--mount=type=bind,from=dev,target=/bin/cp,source=/bin/cp \
48-
--mount=type=bind,from=dev,target=/bin/rm,source=/bin/rm \
49-
--mount=type=bind,from=dev,target=/etc/apk/keys,source=/etc/apk/keys,rw \
50-
--mount=type=bind,from=dev,target=/etc/apk/repositories,source=/etc/apk/repositories,rw \
51-
--mount=type=bind,from=dev,target=/etc/apk/protected_paths.d,source=/etc/apk/protected_paths.d,rw \
52-
--mount=type=bind,from=dev,target=/lib/libapk.so,source=/lib/libapk.so,rw \
53-
--mount=type=bind,from=dev,target=/add_apks.sh,source=/add_apks.sh,rw \
54-
/bin/sh /add_apks.sh
55-
56-
USER 0
5742
ENV PATH="/app/venv/bin:$PATH"
58-
ENTRYPOINT ["python", "__pycache__/main.cpython-312.pyc"]
43+
44+
ENTRYPOINT ["python", "__pycache__/main.cpython-313.pyc"]

0 commit comments

Comments
 (0)