-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
86 lines (67 loc) · 2.24 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# syntax=docker/dockerfile:1.3
FROM python:3.12.6-slim as base
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ARG UID=1000
ARG GID=1000
ENV USER="fakester"
ENV HOME="/home/$USER"
ENV APP_DIR="$HOME/app"
ENV VIRTUAL_ENV="$HOME/venv"
# Install runtime dependencies
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
RUN --mount=type=cache,sharing=locked,target=/var/lib/apt/lists \
--mount=type=cache,sharing=locked,target=/var/cache/apt \
apt-get update \
&& apt-get --no-install-recommends install -y \
# psycopg2
libpq-dev
# Create a non root user
RUN groupadd --gid ${GID} $USER \
&& useradd --system --create-home --no-log-init \
--uid ${UID} --gid ${GID} \
--shell /bin/bash $USER
USER $USER
WORKDIR $APP_DIR
###########
# Builder #
###########
FROM base as builder
# Install build dependencies
USER root
RUN --mount=type=cache,sharing=locked,target=/var/lib/apt/lists \
--mount=type=cache,sharing=locked,target=/var/cache/apt \
apt-get update \
&& apt-get --no-install-recommends install -y \
# psycopg2
python3-dev gcc build-essential
USER $USER
# Set up the virtualenv
RUN python -m venv --copies "$VIRTUAL_ENV"
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
# Install Python dependencies
COPY --chown=$USER:$USER requirements/main.txt $APP_DIR/requirements/main.txt
RUN --mount=type=cache,uid=${UID},gid=${GID},target=$HOME/.cache \
python -m pip install --no-deps -r requirements/main.txt
###########
# Dev #
###########
FROM builder as dev
# Install dev dependencies
COPY --chown=$USER:$USER requirements/dev.txt $APP_DIR/requirements/dev.txt
RUN --mount=type=cache,uid=${UID},gid=${GID},target=$HOME/.cache \
python -m pip install --no-deps -r requirements/dev.txt
###########
# App #
###########
FROM base as app
# Copy virtualenv from builder
COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
# Copy the app
COPY --chown=$USER:$USER src/ $APP_DIR/src
# Collect static assets
RUN python src/manage.py collectstatic --noinput
# Run the app with `gunicorn`
EXPOSE 8000
ENTRYPOINT ["gunicorn", "--pythonpath=src", "--bind=0.0.0.0:8000", "--worker-tmp-dir=/dev/shm", "fakester.wsgi"]