Skip to content

Commit 1e09db7

Browse files
committed
⚡️ Optimize Build Order for better caching
1 parent 63ebfec commit 1e09db7

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

Dockerfile

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ RUN mkdir -p $PIP_CACHE_DIR
1414
ARG USERNAME
1515
LABEL org.opencontainers.image.source=https://github.com/OpenJKSoftware/python-devcontainer
1616

17-
# Install uv
18-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
19-
ENV UV_CACHE_DIR=/var/cache/uv \
20-
UV_PYTHON_CACHE_DIR=/var/cache/uv/python \
21-
UV_LINK_MODE=copy \
22-
UV_COMPILE_BYTECODE=1
23-
RUN set -x; mkdir -p $UV_CACHE_DIR && mkdir -p $UV_PYTHON_CACHE_DIR
24-
RUN --mount=type=cache,target=/var/cache uv python install ${PYTHONVERSION} --default
17+
# Ensure Apt cache is kept between builds
18+
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
2519

2620
# Install Base Reqs
2721
COPY scripts/install_base_deps.sh /tmp/
28-
RUN /tmp/install_base_deps.sh
22+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
23+
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
24+
/tmp/install_base_deps.sh
2925

3026
# Create User and setup environment
3127
RUN useradd --shell /usr/bin/zsh --create-home ${USERNAME} -u 1000 \
@@ -34,8 +30,17 @@ RUN useradd --shell /usr/bin/zsh --create-home ${USERNAME} -u 1000 \
3430
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}
3531
ENV EDITOR=nvim
3632

33+
# Sudo Support
34+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
35+
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
36+
set -x; \
37+
apt-get -y install --no-install-recommends sudo \
38+
&& echo "${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
39+
3740
# Fix Locale issues
38-
RUN set -x; \
41+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
42+
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
43+
set -x; \
3944
apt-get install locales -y --no-install-recommends \
4045
&& echo "LC_ALL=en_US.UTF-8" >> /etc/environment \
4146
&& echo "LANG=en_US.utf-8" >> /etc/environment \
@@ -47,11 +52,16 @@ RUN set -x; \
4752
COPY known_hosts /root/.ssh/known_hosts
4853
COPY --chown=${USERNAME}:${USERNAME} known_hosts /home/${USERNAME}/.ssh/known_hosts
4954

50-
# Sudo Support
51-
RUN set -x; \
52-
apt-get -y install --no-install-recommends sudo \
53-
&& echo "${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
55+
# Install uv
56+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
57+
ENV UV_CACHE_DIR=/var/cache/uv \
58+
UV_PYTHON_CACHE_DIR=/var/cache/uv/python \
59+
UV_LINK_MODE=copy \
60+
UV_COMPILE_BYTECODE=1
61+
RUN set -x; mkdir -p $UV_CACHE_DIR && mkdir -p $UV_PYTHON_CACHE_DIR
62+
RUN --mount=type=cache,target=/var/cache,sharing=locked uv python install ${PYTHONVERSION} --default
5463

64+
RUN apt autoremove --purge -y && apt clean -y
5565

5666
# Non Root User
5767
USER ${USERNAME}
@@ -63,6 +73,4 @@ RUN set -x ; \
6373
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
6474
COPY --chown=${USERNAME}:${USERNAME} .zshrc .zshrc
6575

66-
67-
RUN sudo bash -c "rm -rf {./*,/tmp/*,/var/cache/apt/*,/var/lib/apt/lists/*,$PIP_CACHE_DIR/*}"
6876
ENTRYPOINT [ "/usr/bin/zsh" ]

scripts/install_base_deps.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ apt-get install -y --no-install-recommends \
2626
jq \
2727
lsb-release
2828

29-
# Cleanup
30-
apt-get clean
31-
3229
echo "Base dependencies installation complete"

0 commit comments

Comments
 (0)