-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Dockerfile
106 lines (90 loc) · 3.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# syntax = docker/dockerfile:1
# This Dockerfile uses multi-stage build to customize DEV and PROD images:
# https://docs.docker.com/develop/develop-images/multistage-build/
FROM python:3.11.9-slim-bookworm AS development_build
LABEL maintainer="{{ cookiecutter.project_domain }}"
LABEL vendor="{{ cookiecutter.project_domain }}"
# `DJANGO_ENV` arg is used to make prod / dev builds:
ARG DJANGO_ENV \
# Needed for fixing permissions of files created by Docker:
UID=1000 \
GID=1000
ENV DJANGO_ENV=${DJANGO_ENV} \
# python:
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PYTHONDONTWRITEBYTECODE=1 \
# pip:
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_DEFAULT_TIMEOUT=100 \
PIP_ROOT_USER_ACTION=ignore \
# tini:
TINI_VERSION=v0.19.0 \
# poetry:
POETRY_VERSION=1.8.3 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local'
SHELL ["/bin/bash", "-eo", "pipefail", "-c"]
# System deps (we don't use exact versions because it is hard to update them,
# pin when needed):
# hadolint ignore=DL3008
RUN apt-get update && apt-get upgrade -y \
&& apt-get install --no-install-recommends -y \
bash \
brotli \
build-essential \
curl \
gettext \
git \
libpq-dev \
wait-for-it \
# Installing `tini` utility:
# https://github.com/krallin/tini
# Get architecture to download appropriate tini release:
# See https://github.com/wemake-services/wemake-django-template/issues/1725
&& dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
&& curl -o /usr/local/bin/tini -sSLO "https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${dpkgArch}" \
&& chmod +x /usr/local/bin/tini && tini --version \
# Installing `poetry` package manager:
# https://github.com/python-poetry/poetry
&& curl -sSL 'https://install.python-poetry.org' | python - \
&& poetry --version \
# Cleaning cache:
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
WORKDIR /code
RUN groupadd -g "${GID}" -r web \
&& useradd -d '/code' -g web -l -r -u "${UID}" web \
&& chown web:web -R '/code' \
# Static and media files:
&& mkdir -p '/var/www/django/static' '/var/www/django/media' \
&& chown web:web '/var/www/django/static' '/var/www/django/media'
# Copy only requirements, to cache them in docker layer
COPY --chown=web:web ./poetry.lock ./pyproject.toml /code/
# Project initialization:
# hadolint ignore=SC2046
RUN --mount=type=cache,target="$POETRY_CACHE_DIR" \
echo "$DJANGO_ENV" \
&& poetry version \
# Install deps:
&& poetry run pip install -U pip \
&& poetry install \
$(if [ "$DJANGO_ENV" = 'production' ]; then echo '--only main'; fi) \
--no-interaction --no-ansi --sync
# This is a special case. We need to run this script as an entry point:
COPY ./docker/django/entrypoint.sh /docker-entrypoint.sh
# Setting up proper permissions:
RUN chmod +x '/docker-entrypoint.sh' \
# Replacing line separator CRLF with LF for Windows users:
&& sed -i 's/\r$//g' '/docker-entrypoint.sh'
# Running as non-root user:
USER web
# We customize how our app is loaded with the custom entrypoint:
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]
# The following stage is only for production:
# https://wemake-django-template.readthedocs.io/en/latest/pages/template/production.html
FROM development_build AS production_build
COPY --chown=web:web . /code