Skip to content

Commit cf12088

Browse files
build (dockerfile.dev): refactor to use uv
1 parent 85e7d09 commit cf12088

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

Dockerfile.dev

+59-27
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,92 @@
11
# syntax=docker/dockerfile:1.7.0
22

3-
ARG PYTHON_VERSION=3.11.9
3+
ARG PYTHON_VERSION=3.11
44

5-
FROM python:${PYTHON_VERSION}-alpine AS builder
5+
FROM ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-bookworm-slim as builder
66

7-
RUN apk add --no-cache \
8-
curl \
9-
gcc \
10-
musl-dev \
11-
python3-dev
7+
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
128

13-
ENV PIP_NO_CACHE_DIR=off
14-
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
15-
ENV PIP_DEFAULT_TIMEOUT=100
9+
# Configure the Python directory so it is consistent
10+
ENV UV_PYTHON_INSTALL_DIR /python
1611

17-
ENV VENV="/opt/venv"
18-
ENV PATH="$VENV/bin:$PATH"
12+
# Only use the managed Python version
13+
ENV UV_PYTHON_PREFERENCE=only-managed
14+
15+
# Install Python before the project for caching
16+
RUN uv python install "$PYTHON_VERSION"
1917

2018
WORKDIR /app
2119

22-
COPY requirements-dev.txt .
20+
COPY pyproject.toml .
2321

24-
RUN python -m venv $VENV \
25-
&& . $VENV/bin/activate \
26-
&& python -m pip install --upgrade pip \
27-
&& python -m pip install -r requirements-dev.txt
22+
# venv
23+
ARG UV_PROJECT_ENVIRONMENT="/opt/venv"
24+
ENV VENV="${UV_PROJECT_ENVIRONMENT}"
25+
ENV PATH="$VENV/bin:$PATH"
2826

29-
FROM python:${PYTHON_VERSION}-alpine AS runner
27+
RUN uv venv $UV_PROJECT_ENVIRONMENT \
28+
&& uv pip install -r pyproject.toml --all-extras
3029

30+
FROM python:${PYTHON_VERSION}-slim-bookworm as deps
31+
32+
# avoid stuck build due to user prompt
33+
ARG DEBIAN_FRONTEND=noninteractive
34+
35+
# install dependencies
36+
RUN apt-get -qq update \
37+
&& apt-get -qq install \
38+
--no-install-recommends -y \
39+
curl \
40+
sudo \
41+
tzdata \
42+
&& rm -rf /var/lib/apt/lists/*
43+
44+
FROM deps as runner
45+
46+
# set timezone
3147
ENV TZ=${TZ:-"America/Chicago"}
32-
RUN apk add --no-cache tzdata \
33-
&& ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime \
34-
&& echo "$TZ" > /etc/timezone
48+
RUN ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime && echo "$TZ" > /etc/timezone
3549

50+
# setup standard non-root user for use downstream
3651
ENV USER_NAME=appuser
37-
ENV VENV="/opt/venv"
52+
ARG VENV="/opt/venv"
3853

39-
ENV PATH="${VENV}/bin:${VENV}/lib/python${PYTHON_VERSION}/site-packages:/usr/local/bin:${HOME}/.local/bin:/bin:/usr/bin:/usr/share/doc:$PATH"
54+
ENV PATH="${VENV}/bin:${HOME}/.local/bin:$PATH"
4055

56+
# standardise on locale, don't generate .pyc, enable tracebacks on seg faults
4157
ENV LANG C.UTF-8
4258
ENV LC_ALL C.UTF-8
4359
ENV PYTHONDONTWRITEBYTECODE 1
4460
ENV PYTHONFAULTHANDLER 1
4561

62+
# add non-root user with sudo privileges
4663
ARG UID=10001
47-
RUN adduser -D -u ${UID} ${USER_NAME}
64+
RUN adduser \
65+
--disabled-password \
66+
--gecos "" \
67+
--home "/home/${USER_NAME}" \
68+
--shell "/bin/bash" \
69+
--uid "${UID}" \
70+
${USER_NAME} \
71+
&& echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USER_NAME} \
72+
&& chmod 0440 /etc/sudoers.d/${USER_NAME}
4873

4974
USER ${USER_NAME}
75+
WORKDIR /home/${USER_NAME}
76+
77+
# Copy the Python version
78+
COPY --from=builder --chown=${USER_NAME}:${USER_NAME} /python /python
5079

5180
WORKDIR /app
5281

53-
COPY --chown=${USER_NAME} ./app .
82+
# Copy venv first (has better caching properties)
5483
COPY --from=builder --chown=${USER_NAME} "$VENV" "$VENV"
5584

85+
# Copy application code last
86+
COPY --chown=${USER_NAME} ./app .
87+
5688
CMD [ "sleep", "infinity" ]
5789

5890
LABEL org.opencontainers.image.title="meetup_bot"
59-
LABEL org.opencontainers.image.version="test"
60-
LABEL org.opencontainers.image.description="meetup_bot image for running tests in CI"
91+
LABEL org.opencontainers.image.version="dev"
92+
LABEL org.opencontainers.image.description="meetup_bot image for development"

0 commit comments

Comments
 (0)