-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
136 lines (99 loc) · 4.14 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
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
# syntax = docker/dockerfile:1.5
# (Keep the version in sync with the node install below)
FROM node:20 as frontend
# Install front-end dependencies.
COPY package.json package-lock.json tsconfig.json webpack.config.js ./
RUN npm ci --no-optional --no-audit --progress=false
# Compile static files
COPY ./climtech/static/ ./climtech/static/
RUN npm run build:prod
# Build Python app - this stage is a common base for the prod and dev stages
FROM ghcr.io/osgeo/gdal:ubuntu-small-3.10.0 AS backend
ARG POETRY_VERSION=1.8.3
ARG UID
ENV UID=${UID:-1000}
ARG GID
ENV GID=${GID:-1000}
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# We might be running as a user which already exists in this image. In that situation
# Everything is OK and we should just continue on.
RUN groupadd -g $GID climtech_docker_group || exit 0
RUN useradd --shell /bin/bash -u $UID -g $GID -o -c "" -m climtech_docker_user -l || exit 0
ENV DOCKER_USER=climtech_docker_user
ENV POSTGRES_VERSION=15
ENV GUNICORN_CMD_ARGS="--max-requests 1200 --max-requests-jitter 50 --access-logfile -" \
PORT=8000 \
PYTHONUNBUFFERED=1 \
VIRTUAL_ENV=/venv \
WEB_CONCURRENCY=3
# Install operating system dependencies.
RUN apt-get update --yes --quiet \
&& apt-get install -y --no-install-recommends \
build-essential \
lsb-release \
ca-certificates \
gnupg2 \
curl \
cron \
tini \
libpq-dev \
libgeos-dev \
python3-pip \
python3-dev \
python3-venv \
git \
gosu \
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
&& curl --silent https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& apt-get update \
&& apt-get install -y apt-transport-https rsync libmagickwand-dev unzip postgresql-client-$POSTGRES_VERSION \
jpegoptim pngquant gifsicle libjpeg-progs webp && \
rm -rf /var/lib/apt/lists/*
ARG DOCKER_COMPOSE_WAIT_VERSION
ENV DOCKER_COMPOSE_WAIT_VERSION=${DOCKER_COMPOSE_WAIT_VERSION:-2.12.1}
ARG DOCKER_COMPOSE_WAIT_PLATFORM_SUFFIX
ENV DOCKER_COMPOSE_WAIT_PLATFORM_SUFFIX=${DOCKER_COMPOSE_WAIT_PLATFORM_SUFFIX:-}
# Install docker-compose wait
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$DOCKER_COMPOSE_WAIT_VERSION/wait${DOCKER_COMPOSE_WAIT_PLATFORM_SUFFIX} /wait
RUN chown $UID:$GID /wait && chmod +x /wait
RUN mkdir -p /app /venv /.cache && chown $UID:$GID /app /venv /.cache
USER $UID:$GID
# Create a virtual environment and install Poetry
RUN python3 -m venv /venv && /venv/bin/pip install --upgrade pip wheel && /venv/bin/pip install poetry==$POETRY_VERSION
ENV PATH="/venv/bin:$PATH"
WORKDIR /app
# This stage builds the image that will run in production
FROM backend as prod
# Install production dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install --only main --no-root
# Copy in application code and install the root package
COPY --chown=$UID:$GID . .
RUN poetry install --only-root
COPY --chown=$UID:$GID --from=frontend ./climtech/static_compiled ./climtech/static_compiled
# RUN SECRET_KEY=none django-admin collectstatic --noinput --clear
COPY --chown=$UID:$GID docker-entrypoint.sh ./
RUN chmod a+x ./docker-entrypoint.sh
ENTRYPOINT ["/usr/bin/tini", "--", "./docker-entrypoint.sh"]
ENV PATH="/venv/bin:$PATH"
ENV DJANGO_SETTINGS_MODULE="climtech.config.settings.production"
# Run application
CMD ["gunicorn"]
# This stage builds the image that we use for development
FROM backend AS dev
# Install Node.js because newer versions of Heroku CLI have a node binary dependency
# RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
# RUN apt-get install -y nodejs
# Switch to the application user
USER $UID:$GID
# Install development dependencies
COPY --chown=$UID:$GID pyproject.toml poetry.lock ./
RUN poetry install --no-root
# Copy in application code and install the root package
COPY --chown=$UID:$GID . .
# Pull in the node modules for the frontend
COPY --chown=$UID:$GID --from=frontend ./node_modules ./node_modules
# Make sure the working directory is on PYTHONPATH (so django-admin etc. can
# import climtech)
ENV PYTHONPATH=/app
ENV DJANGO_SETTINGS_MODULE="climtech.config.settings.dev"