Skip to content

Commit b4d63f4

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

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

Dockerfile

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@ 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 \
3228
&& mkdir -pm 700 /root/.ssh \
3329
&& echo "alias vi=nvim" > /etc/profile.d/vim_nvim.sh \
34-
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}
30+
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME} \
31+
&& echo "${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
3532
ENV EDITOR=nvim
3633

34+
35+
3736
# Fix Locale issues
38-
RUN set -x; \
37+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
38+
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
39+
set -x; \
3940
apt-get install locales -y --no-install-recommends \
4041
&& echo "LC_ALL=en_US.UTF-8" >> /etc/environment \
4142
&& echo "LANG=en_US.utf-8" >> /etc/environment \
@@ -47,11 +48,16 @@ RUN set -x; \
4748
COPY known_hosts /root/.ssh/known_hosts
4849
COPY --chown=${USERNAME}:${USERNAME} known_hosts /home/${USERNAME}/.ssh/known_hosts
4950

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
51+
# Install uv
52+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
53+
ENV UV_CACHE_DIR=/var/cache/uv \
54+
UV_PYTHON_CACHE_DIR=/var/cache/uv/python \
55+
UV_LINK_MODE=copy \
56+
UV_COMPILE_BYTECODE=1
57+
RUN set -x; mkdir -p $UV_CACHE_DIR && mkdir -p $UV_PYTHON_CACHE_DIR
58+
RUN --mount=type=cache,target=/var/cache,sharing=locked uv python install ${PYTHONVERSION} --default
5459

60+
RUN apt autoremove --purge -y && apt clean -y
5561

5662
# Non Root User
5763
USER ${USERNAME}
@@ -63,6 +69,4 @@ RUN set -x ; \
6369
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
6470
COPY --chown=${USERNAME}:${USERNAME} .zshrc .zshrc
6571

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

scripts/install_base_deps.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ apt-get install -y --no-install-recommends \
2424
neovim \
2525
pv \
2626
jq \
27-
lsb-release
28-
29-
# Cleanup
30-
apt-get clean
27+
lsb-release \
28+
sudo \
29+
net-tools
3130

3231
echo "Base dependencies installation complete"

0 commit comments

Comments
 (0)