Skip to content

Commit b0620bf

Browse files
Merge pull request #437 from puneetmatharu/update-docker-builds
Switch to slim Python Docker base image for TensorFlow docker builds and tidy up Python Dockerfile
2 parents e9ecf08 + 3c62b54 commit b0620bf

4 files changed

Lines changed: 89 additions & 113 deletions

File tree

ML-Frameworks/pytorch-aarch64/Dockerfile

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@ COPY --chown=$DOCKER_USER:$DOCKER_USER welcome.txt /home/$DOCKER_USER/welcome.tx
3333

3434
# Switch to userland
3535
USER $DOCKER_USER
36-
RUN echo "DOCKER_USER=${DOCKER_USER}"
3736
WORKDIR /home/$DOCKER_USER
3837

39-
# Create virtual environment
40-
RUN python -m venv /home/$DOCKER_USER/.venv
41-
ENV PATH="/home/$DOCKER_USER/.venv/bin:${PATH}"
38+
# Add ~/.local/bin to PATH to use uv later
39+
ENV PATH="/home/$DOCKER_USER/.local/bin:${PATH}"
40+
41+
# Install uv for quicker package installations (installed to ~/.local/bin with --user)
42+
RUN python -m pip install --user uv==0.9.29
4243

43-
# Install uv for quicker package installations
44-
RUN pip install uv==0.9.29
44+
# Create virtual environment with uv
45+
RUN uv venv /home/$DOCKER_USER/.venv
46+
ENV PATH="/home/$DOCKER_USER/.venv/bin:${PATH}"
4547

46-
# Update to newer pip/setuptools/wheel (setuptools >= 70.0.0 due to CVE-2024-6345
47-
# and CVE-2025-47273, wheel >= 0.38.0 due to CVE-2022-40898) and delete old system
48-
# version (we essentially use apt:python3-pip to bootstrap pip)
49-
RUN uv pip install --upgrade pip~=25.2 setuptools~=78.1.1 wheel~=0.45.1
48+
# Remove pip and update to newer setuptools/wheel (setuptools >= 70.0.0 due to CVE-2024-6345
49+
# and CVE-2025-47273, wheel >= 0.38.0 due to CVE-2022-40898)
50+
RUN uv pip uninstall pip && uv pip install --upgrade setuptools~=78.1.1 wheel~=0.45.1
5051

5152
# Install non-torch requirements
5253
COPY --chown=$DOCKER_USER:$DOCKER_USER requirements.txt .
@@ -76,18 +77,17 @@ ENV DOCKER_USER=debian
7677

7778
# Runtime OS bits + UI
7879
RUN set -eux && \
79-
apt-get update && \
80-
rm -rf /var/lib/apt/lists/* && \
8180
if ! id "$DOCKER_USER" >/dev/null 2>&1; then useradd --create-home -s /bin/bash "$DOCKER_USER"; fi && \
8281
echo '[ -n "$TERM" -a -r "$HOME/welcome.txt" ] && cat "$HOME/welcome.txt"' >> /etc/bash.bashrc && \
83-
echo 'export PATH="$HOME/.local/bin:$HOME/.venv/bin:$PATH"' >> /etc/bash.bashrc
82+
echo 'export PATH="$HOME/.local/bin:$HOME/.venv/bin:$PATH"' >> /etc/bash.bashrc && \
83+
python3 -m pip uninstall -y pip
8484

8585
# Bring in prepped env + code
8686
COPY --from=workshop --chown=$DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER /home/$DOCKER_USER
8787

8888
USER $DOCKER_USER
8989
WORKDIR /home/$DOCKER_USER
9090

91-
ENV PATH="/home/$DOCKER_USER/.venv/bin:${PATH}"
91+
ENV PATH="/home/$DOCKER_USER/.local/bin:/home/$DOCKER_USER/.venv/bin:$PATH"
9292

9393
CMD ["bash", "-l"]
Lines changed: 63 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,89 @@
1-
# SPDX-FileCopyrightText: Copyright 2020-2025 Arm Limited and affiliates.
1+
# SPDX-FileCopyrightText: Copyright 2020-2026 Arm Limited and affiliates.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

55
# Specify DOCKER_IMAGE_MIRROR if you want to use a mirror of hub.docker.com
6+
ARG BASE_IMAGE=python:3.12-slim
67
ARG DOCKER_IMAGE_MIRROR=""
7-
FROM ${DOCKER_IMAGE_MIRROR}ubuntu:24.04 AS workshop
88

9-
ARG USERNAME
9+
# ============================
10+
# Workshop
11+
# ============================
12+
FROM ${DOCKER_IMAGE_MIRROR}${BASE_IMAGE} AS workshop
1013

1114
ARG TENSORFLOW_WHEEL
12-
ENV TENSORFLOW_WHEEL=$TENSORFLOW_WHEEL
13-
14-
RUN if ! [ "$(arch)" = "aarch64" ] ; then exit 1; fi
15-
16-
RUN apt-get update && apt-get install -y \
17-
# We need pip to install things, this will also bring in a minimal python3
18-
python3-pip \
19-
# So that we can create a virtual environment
20-
python3-venv \
21-
# So that we can call python instead of python3
22-
python-is-python3 \
23-
# To allow users to install new things if they want
24-
sudo \
25-
# Git
26-
git \
27-
&& rm -rf /var/lib/apt/lists/*
28-
RUN apt-get update
29-
RUN apt-get install -y protobuf-compiler
30-
RUN apt-get install -y wget
31-
32-
# DOCKER_USER for the Docker user
33-
ENV DOCKER_USER=${USERNAME}
34-
35-
# Create user only if it doesn't already exist
36-
RUN id "$DOCKER_USER" >/dev/null 2>&1 || useradd --create-home -s /bin/bash -m "$DOCKER_USER"
37-
38-
# Set password and add to sudo group
39-
RUN echo "$DOCKER_USER:ToolSolutionsTensorFlow" | chpasswd && adduser "$DOCKER_USER" sudo || true
40-
41-
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
42-
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
43-
44-
# Import profile for bash
45-
COPY bash_profile /home/$DOCKER_USER/.bash_profile
46-
RUN chown $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER/.bash_profile
47-
48-
# Add welcome message to warn about dev quality
49-
COPY welcome.txt /home/$DOCKER_USER/
50-
RUN echo '[ ! -z "$TERM" -a -r /home/$DOCKER_USER/welcome.txt ] && cat /home/$DOCKER_USER/welcome.txt' >> /etc/bash.bashrc
51-
RUN echo 'export PATH="$HOME/.local/bin:$PATH"' >> /etc/bash.bashrc
52-
53-
# Grab the SECURITY.md from the root directory
54-
COPY --from=rootdir SECURITY.md /home/$DOCKER_USER/
55-
56-
# Remove system Python stuff. Should be safe to wipe after the line above, because
57-
# python3 -m pip now uses the /usr/local install. Also removes unused protobuf
58-
# packages to resolve CVE-2025-4565.
59-
RUN apt-get update && apt-get purge -y \
60-
python3-pip \
61-
python3-setuptools \
62-
python3-pkg-resources \
63-
python3-wheel \
64-
python3-distutils \
65-
python3-lib2to3 \
66-
python3-dev \
67-
python3.12-dev \
68-
libprotobuf32t64 \
69-
libprotobuf-lite32t64 \
70-
libprotoc32t64 \
71-
protobuf-compiler \
72-
&& apt-get autoremove -y \
73-
&& rm -rf /var/lib/apt/lists/*
74-
75-
# Move to userland
76-
WORKDIR /home/$DOCKER_USER
15+
16+
ENV DEBIAN_FRONTEND=noninteractive
17+
ENV DOCKER_USER=debian
18+
19+
RUN test "$(arch)" = "aarch64"
20+
21+
# Install OS dependencies
22+
RUN apt-get update && \
23+
apt-get install -y --no-install-recommends protobuf-compiler wget && \
24+
rm -rf /var/lib/apt/lists/*
25+
26+
# Create user
27+
RUN set -eux && id "$DOCKER_USER" >/dev/null 2>&1 || useradd --create-home -s /bin/bash "$DOCKER_USER"
28+
29+
# Copy bash profile and welcome text into user home
30+
COPY --chown=$DOCKER_USER:$DOCKER_USER bash_profile /home/$DOCKER_USER/.bash_profile
31+
COPY --chown=$DOCKER_USER:$DOCKER_USER welcome.txt /home/$DOCKER_USER/welcome.txt
32+
33+
# Switch to userland
7734
USER $DOCKER_USER
35+
WORKDIR /home/$DOCKER_USER
36+
37+
# Add ~/.local/bin to PATH to use uv later
38+
ENV PATH="/home/$DOCKER_USER/.local/bin:${PATH}"
7839

79-
# Create a per-user virtualenv and use that for everything Python
80-
RUN python -m venv /home/$DOCKER_USER/.venv
40+
# Install uv for quicker package installations (installed to ~/.local/bin with --user)
41+
RUN python -m pip install --user uv==0.9.29
8142

82-
# Make the venv python/pip first on PATH for all subsequent layers and at runtime
83-
ENV PATH="/home/$DOCKER_USER/.venv/bin:$PATH"
43+
# Create virtual environment with uv
44+
RUN uv venv /home/$DOCKER_USER/.venv
45+
ENV PATH="/home/$DOCKER_USER/.venv/bin:${PATH}"
8446

85-
# Update to newer pip/setuptools/wheel (setuptools >= 70.0.0 due to CVE-2024-6345
86-
# and CVE-2025-47273, wheel >= 0.38.0 due to CVE-2022-40898) and delete old system
87-
# version (we essentially use apt:python3-pip to bootstrap pip)
88-
RUN pip install --upgrade pip~=25.2 setuptools~=78.1.1 wheel~=0.45.1
47+
# Remove pip and update to newer setuptools/wheel (setuptools >= 70.0.0 due to CVE-2024-6345
48+
# and CVE-2025-47273, wheel >= 0.38.0 due to CVE-2022-40898)
49+
RUN uv pip uninstall pip && uv pip install --upgrade setuptools~=78.1.1 wheel~=0.45.1
8950

90-
# Check TENSORFLOW_WHEEL was set and copy
91-
RUN test -n "$TENSORFLOW_WHEEL"
92-
COPY $TENSORFLOW_WHEEL /home/$DOCKER_USER/
51+
# Install non-TensorFlow requirements
52+
COPY --chown=$DOCKER_USER:$DOCKER_USER requirements.txt .
53+
RUN uv pip install -r requirements.txt --no-deps
9354

94-
# Install our dev build of TensorFlow.
95-
RUN pip install "$(basename "$TENSORFLOW_WHEEL")" \
96-
&& rm "$(basename "$TENSORFLOW_WHEEL")"
55+
# Bring wheels into image
56+
RUN test -n "${TENSORFLOW_WHEEL}"
57+
COPY --chown=$DOCKER_USER:$DOCKER_USER ${TENSORFLOW_WHEEL} /home/$DOCKER_USER/
9758

98-
# Base requirements for examples, excluding TensorFlow
99-
COPY requirements.txt ./
100-
RUN pip install -r requirements.txt --no-deps
59+
# Install wheel
60+
RUN set -eux && uv pip install "$(basename "$TENSORFLOW_WHEEL")" && rm "$(basename "$TENSORFLOW_WHEEL")"
10161

102-
# Setup Examples and tests
103-
COPY --chown=$DOCKER_USER examples/ /home/$DOCKER_USER/
104-
COPY --chown=$DOCKER_USER tensorflow/ /home/$DOCKER_USER/tensorflow
62+
# Copy examples/tests into image
63+
COPY --chown=$DOCKER_USER:$DOCKER_USER examples/ /home/$DOCKER_USER/
64+
COPY --chown=$DOCKER_USER:$DOCKER_USER tensorflow/ /home/$DOCKER_USER/tensorflow
10565

106-
# Move build into final image as a single layer.
107-
FROM ${DOCKER_IMAGE_MIRROR}ubuntu:24.04
66+
# ============================
67+
# Final flat image
68+
# ============================
69+
FROM ${DOCKER_IMAGE_MIRROR}${BASE_IMAGE}
10870

109-
ARG USERNAME
71+
ENV DEBIAN_FRONTEND=noninteractive
72+
ENV DOCKER_USER=debian
11073

111-
ENV DOCKER_USER=${USERNAME}
74+
# Runtime OS bits + UI
75+
RUN set -eux && \
76+
if ! id "$DOCKER_USER" >/dev/null 2>&1; then useradd --create-home -s /bin/bash "$DOCKER_USER"; fi && \
77+
echo '[ -n "$TERM" -a -r "$HOME/welcome.txt" ] && cat "$HOME/welcome.txt"' >> /etc/bash.bashrc && \
78+
echo 'export PATH="$HOME/.local/bin:$HOME/.venv/bin:$PATH"' >> /etc/bash.bashrc && \
79+
python3 -m pip uninstall -y pip
11280

113-
COPY --from=workshop / /
114-
RUN chown $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER
81+
# Bring in prepped env + code
82+
COPY --from=workshop --chown=$DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER /home/$DOCKER_USER
11583

11684
USER $DOCKER_USER
11785
WORKDIR /home/$DOCKER_USER
11886

119-
# Ensure the venv is on PATH in the final image as well
120-
ENV PATH="/home/$DOCKER_USER/.venv/bin:$PATH"
87+
ENV PATH="/home/$DOCKER_USER/.local/bin:/home/$DOCKER_USER/.venv/bin:$PATH"
12188

12289
CMD ["bash", "-l"]

ML-Frameworks/tensorflow-aarch64/bash_profile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright 2019, 2020, 2025 Arm Limited and affiliates.
1+
# SPDX-FileCopyrightText: Copyright 2019, 2020, 2025, 2026 Arm Limited and affiliates.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -21,3 +21,13 @@ export PS1
2121

2222
# Add local bin to PATH
2323
export PATH="$HOME/.local/bin:$PATH"
24+
25+
# Deprecate pip in favor of uv
26+
function pip () {
27+
echo ""
28+
echo " *******************************************************************************"
29+
echo " * We no longer ship pip with Tool-Solutions, please use uv instead. Note that *"
30+
echo " * you can use uv pip to get functionality similar to pip. *"
31+
echo " *******************************************************************************"
32+
echo ""
33+
}

ML-Frameworks/tensorflow-aarch64/dockerize.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# SPDX-FileCopyrightText: Copyright 2024, 2025 Arm Limited and affiliates.
3+
# SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and affiliates.
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

@@ -25,7 +25,6 @@ docker buildx \
2525
--build-context rootdir=../.. \
2626
--build-arg TENSORFLOW_WHEEL=$1 \
2727
--build-arg DOCKER_IMAGE_MIRROR \
28-
--build-arg USERNAME=ubuntu \
2928
.
3029

3130
[[ $* == *--build-only* ]] && exit 0

0 commit comments

Comments
 (0)