-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
150 lines (121 loc) · 4.84 KB
/
Dockerfile
File metadata and controls
150 lines (121 loc) · 4.84 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#################################################################################
# use node:22.*-bookworm-slim as the base image for building the frontend
#################################################################################
FROM node:22.22-bookworm-slim AS frontend-builder
WORKDIR /app
# Copy only package files first to leverage Docker caching
COPY package*.json .babelrc.js webpack.config.js postcss.config.js tailwind.config.js ./
RUN npm ci --no-optional --no-audit --progress=false
# Copy only the files needed for the frontend build
COPY ./milk2meat/frontend ./milk2meat/frontend
# Copy template files needed for tailwind to detect utility classes
COPY ./milk2meat/auth ./milk2meat/auth
COPY ./milk2meat/bible ./milk2meat/bible
COPY ./milk2meat/notes ./milk2meat/notes
COPY ./milk2meat/core ./milk2meat/core
COPY ./milk2meat/home ./milk2meat/home
COPY ./milk2meat/templates ./milk2meat/templates
RUN npm run build:prod
#################################################################################
# use python:3.13-slim-bookworm as the base image for production
#################################################################################
FROM python:3.13-slim-bookworm AS production
# Add user that will be used in the container
RUN groupadd --system django && \
useradd --system --create-home --shell /bin/bash -g django django
RUN mkdir -p /home/django/app && chown django:django /home/django/app
# Set work directory
WORKDIR /home/django/app
# Port used by this container to serve HTTP
EXPOSE 8000
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONHASHSEED=random \
PYTHONPATH=/home/django/app \
DJANGO_SETTINGS_MODULE=milk2meat.settings.production \
WEB_CONCURRENCY=3
# Install system dependencies required by Django and the project
RUN apt-get update --yes --quiet && \
apt-get install --yes --quiet --no-install-recommends \
build-essential \
ca-certificates \
curl \
gdal-bin \
libgdal-dev \
binutils \
libproj-dev \
git \
imagemagick \
libjpeg62-turbo-dev \
libmagic1 \
libpq-dev \
libwebp-dev \
zlib1g-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Use user "django" to run the build commands below and the server itself
USER django
# Set up virtual environment & install python dependencies
ARG DEVELOPMENT=false
ARG POETRY_VERSION=1.8.5
ENV VIRTUAL_ENV=/home/django/venv \
DEVELOPMENT=${DEVELOPMENT}
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN pip install --no-cache-dir --upgrade pip && \
python -m pip install --no-cache-dir poetry==$POETRY_VERSION
# Install dependencies first to leverage Docker caching
COPY --chown=django:django ./pyproject.toml ./poetry.lock ./
RUN poetry config virtualenvs.create false && \
poetry install --only main --no-interaction --no-ansi
# Copy build artifacts from frontend-builder stage
RUN mkdir -p /home/django/app/milk2meat/static
COPY --from=frontend-builder --chown=django:django /app/milk2meat/static /home/django/app/milk2meat/static
# Copy the source code of the project into the container
COPY --chown=django:django . .
# Make entrypoint script executable
RUN chmod +x entrypoint.sh
# Collect static files
RUN DJANGO_SECRET_KEY=fake \
DATABASE_URL=postgres://user:password@host:5432/db \
REDIS_URL=redis://:password@redis:6379/0 \
REDIS_KEY_PREFIX=fake \
AWS_ACCESS_KEY_ID=fake \
AWS_SECRET_ACCESS_KEY=fake \
AWS_STORAGE_BUCKET_NAME=fake \
AWS_S3_ENDPOINT_URL=fake \
BREVO_API_KEY=fake \
EMAIL_RECIPIENTS='' \
DEFAULT_FROM_EMAIL='' \
SENTRY_DSN=https://fakekey@foo.ingest.de.sentry.io/12345 \
python manage.py collectstatic --noinput --clear
# Set the entrypoint script
ENTRYPOINT ["./entrypoint.sh"]
# Runtime command that executes when "docker run" is called
# gunicorn will use the settings defined in gunicorn.conf.py
CMD ["gunicorn"]
#################################################################################
# The next steps won't be run in production
#################################################################################
FROM production AS dev
# Swap user, so the following tasks can be run as root
USER root
# Install Node.js for development
ENV NODE_MAJOR=22
RUN apt-get update && \
apt-get install -y --no-install-recommends gnupg && \
curl -fsSL https://deb.nodesource.com/setup_${NODE_MAJOR}.x | bash - && \
apt-get update && \
apt-get install -y --no-install-recommends \
nodejs \
postgresql-client && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install development dependencies
USER django
RUN poetry install --with dev,test,docs --no-interaction --no-ansi
# Pull in the node modules for the frontend
COPY --chown=django:django --from=frontend-builder /app/node_modules ./node_modules
# do nothing - exec commands elsewhere
CMD tail -f /dev/null