Skip to content

Commit 5a5f50f

Browse files
fix(backend): improve Dockerfile multistep build (#1727)
* fix(backend): improve Dockerfile multistep build * chore: update Dockerfile and entrypoint script to setup poetry correctly * refactor: update Dockerfile to change working directory --------- Co-authored-by: Gustavo Flores <gustavo.flores@profusion.mobi>
1 parent ec11ea9 commit 5a5f50f

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

backend/Dockerfile

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
11
# syntax=docker/dockerfile:1
22

3-
# Create server docker image (all non-code dependencies)
4-
FROM alpine:3.20 as backend-base
5-
6-
WORKDIR /
7-
8-
ENV POETRY_HOME=/opt/poetry
3+
# Build stage: Install dependencies and build the application
4+
FROM alpine:3.20 as builder
5+
WORKDIR /backend
96

10-
RUN apk update \
11-
&& apk add --no-cache \
7+
RUN apk update && \
8+
apk add --no-cache \
129
python3 \
1310
python3-dev \
1411
gcc \
1512
libffi-dev \
1613
libc-dev \
1714
libpq-dev \
18-
postgresql \
19-
curl \
20-
&& python3 -m venv $POETRY_HOME \
21-
&& $POETRY_HOME/bin/pip install poetry==1.8.3 \
22-
&& ln -s $POETRY_HOME/bin/poetry /bin/poetry
23-
24-
FROM backend-base
25-
COPY . /backend
26-
COPY ./utils/docker/entrypoint.sh /entrypoint.sh
27-
WORKDIR /backend
15+
curl
16+
17+
COPY pyproject.toml poetry.lock /backend/
18+
19+
ENV POETRY_HOME=/opt/poetry \
20+
POETRY_VIRTUALENVS_IN_PROJECT=1
21+
22+
RUN python3 -m venv $POETRY_HOME
23+
RUN $POETRY_HOME/bin/pip install poetry==1.8.3
2824

2925
ARG INSTALL_DEV_DEPS=false
3026
RUN if [ "$INSTALL_DEV_DEPS" = "true" ]; then \
31-
poetry install --no-interaction; \
27+
$POETRY_HOME/bin/poetry install --no-interaction; \
3228
else \
33-
poetry install --no-interaction --only main; \
29+
$POETRY_HOME/bin/poetry install --no-interaction --only main; \
3430
fi
3531

36-
ENV DJANGO_APP="kernelCI"
32+
# Copy application code
33+
COPY . /backend/
3734

3835
# Precompile Python modules
3936
RUN O='0 1 2' \
40-
PY_MAJMIN=`poetry run python -c "import sys; print('%s.%s'%sys.version_info[0:2])"` \
41-
PY_D=`poetry env info --path` \
42-
D="$PY_D $DJANGO_APP"; \
37+
PY_MAJMIN=`$POETRY_HOME/bin/poetry run python -c "import sys; print('%s.%s'%sys.version_info[0:2])"` \
38+
PY_D=`$POETRY_HOME/bin/poetry env info --path` \
39+
D="$PY_D kernelCI"; \
4340
for N in $O; do \
4441
echo "compile python $PY_MAJMIN byte code at -O$N: $D"; \
45-
PYTHONOPTIMIZE=$N poetry run python -m compileall -q $D || exit 1; \
42+
PYTHONOPTIMIZE=$N $POETRY_HOME/bin/poetry run python -m compileall -q $D || exit 1; \
4643
done
4744

45+
# Runtime stage: Create a clean image with only runtime dependencies
46+
FROM alpine:3.20 as app
47+
WORKDIR /backend
48+
49+
# Install only runtime dependencies (no build tools)
50+
RUN apk update && \
51+
apk add --no-cache \
52+
python3 \
53+
libpq \
54+
postgresql-client \
55+
curl
56+
57+
COPY --from=builder /opt/poetry /opt/poetry
58+
COPY --from=builder /backend/.venv /backend/.venv
59+
60+
ENV PATH="/backend/.venv/bin:/opt/poetry/bin:$PATH"
61+
62+
COPY --from=builder /backend /backend
63+
COPY --from=builder /backend/utils/docker/entrypoint.sh /entrypoint.sh
64+
4865
ARG BACKEND_VOLUME_DIR
4966
ENV BACKEND_VOLUME_DIR=${BACKEND_VOLUME_DIR}
5067
RUN echo "Building with volume on: $BACKEND_VOLUME_DIR"

backend/utils/docker/entrypoint.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
export PYTHONOPTIMIZE=${PYTHONOPTIMIZE:-2}
33
export PYTHONUNBUFFERED=${PYTHONUNBUFFERED:-1}
44

5-
source $(poetry env info -C ./backend --path)/bin/activate
65
set -e
76

87
# usage: file_env VAR [DEFAULT]

0 commit comments

Comments
 (0)