|
1 | 1 | # syntax=docker/dockerfile:1 |
2 | 2 |
|
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 |
9 | 6 |
|
10 | | -RUN apk update \ |
11 | | - && apk add --no-cache \ |
| 7 | +RUN apk update && \ |
| 8 | + apk add --no-cache \ |
12 | 9 | python3 \ |
13 | 10 | python3-dev \ |
14 | 11 | gcc \ |
15 | 12 | libffi-dev \ |
16 | 13 | libc-dev \ |
17 | 14 | 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 |
28 | 24 |
|
29 | 25 | ARG INSTALL_DEV_DEPS=false |
30 | 26 | RUN if [ "$INSTALL_DEV_DEPS" = "true" ]; then \ |
31 | | - poetry install --no-interaction; \ |
| 27 | + $POETRY_HOME/bin/poetry install --no-interaction; \ |
32 | 28 | else \ |
33 | | - poetry install --no-interaction --only main; \ |
| 29 | + $POETRY_HOME/bin/poetry install --no-interaction --only main; \ |
34 | 30 | fi |
35 | 31 |
|
36 | | -ENV DJANGO_APP="kernelCI" |
| 32 | +# Copy application code |
| 33 | +COPY . /backend/ |
37 | 34 |
|
38 | 35 | # Precompile Python modules |
39 | 36 | 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"; \ |
43 | 40 | for N in $O; do \ |
44 | 41 | 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; \ |
46 | 43 | done |
47 | 44 |
|
| 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 | + |
48 | 65 | ARG BACKEND_VOLUME_DIR |
49 | 66 | ENV BACKEND_VOLUME_DIR=${BACKEND_VOLUME_DIR} |
50 | 67 | RUN echo "Building with volume on: $BACKEND_VOLUME_DIR" |
|
0 commit comments