forked from kobotoolbox/kpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
188 lines (148 loc) · 6.03 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
FROM python:3.10 as build-python
ENV VIRTUAL_ENV=/opt/venv \
TMP_DIR=/srv/tmp
RUN python -m venv "$VIRTUAL_ENV"
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN pip install --quiet pip-tools==7.\*
COPY ./dependencies/pip/requirements.txt "${TMP_DIR}/pip_dependencies.txt"
RUN pip-sync "${TMP_DIR}/pip_dependencies.txt" 1>/dev/null
from python:3.10-slim
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
ENV VIRTUAL_ENV=/opt/venv
ENV KPI_LOGS_DIR=/srv/logs \
DJANGO_SETTINGS_MODULE=kobo.settings.prod \
# The mountpoint of a volume shared with the `nginx` container. Static files will
# be copied there.
NGINX_STATIC_DIR=/srv/static \
KPI_SRC_DIR=/srv/src/kpi \
KPI_MEDIA_DIR=/srv/src/kpi/media \
KPI_NODE_PATH=/srv/src/kpi/node_modules \
TMP_DIR=/srv/tmp \
UWSGI_USER=kobo \
UWSGI_GROUP=kobo \
SERVICES_DIR=/etc/service \
CELERY_PID_DIR=/var/run/celery \
INIT_PATH=/srv/init
##########################################
# Create build directories #
##########################################
RUN mkdir -p "${NGINX_STATIC_DIR}" && \
mkdir -p "${KPI_SRC_DIR}" && \
mkdir -p "${KPI_NODE_PATH}" && \
mkdir -p "${TMP_DIR}" && \
mkdir -p ${CELERY_PID_DIR} && \
mkdir -p ${SERVICES_DIR}/uwsgi && \
mkdir -p ${SERVICES_DIR}/celery && \
mkdir -p ${SERVICES_DIR}/celery_low_priority && \
mkdir -p ${SERVICES_DIR}/celery_beat && \
mkdir -p "${INIT_PATH}"
##########################################
# Install `apt` packages. #
##########################################
# DO NOT remove packages like `less` and `procps` without approval from
# jnm (or the current on-call sysadmin). Thanks.
RUN apt-get -qq update && \
apt-get -qq -y install ca-certificates curl gnupg && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
| gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x nodistro main" \
| tee /etc/apt/sources.list.d/nodesource.list && \
apt-get -qq update && \
apt-get -qq -y install --no-install-recommends \
ffmpeg \
gdal-bin \
gettext \
git \
gosu \
less \
libproj-dev \
locales \
nodejs=$(apt-cache show nodejs | grep -F 'Version: 16.15.0' | cut -f 2 -d ' ') \
postgresql-client \
procps \
rsync \
runit-init \
vim-tiny \
wait-for-it && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
###########################
# Install locales #
###########################
RUN echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && \
locale-gen && dpkg-reconfigure locales -f noninteractive
#################################
# Create local user UWSGI_USER` #
#################################
RUN adduser --disabled-password --gecos '' "$UWSGI_USER"
###########################
# Copy KPI directory #
###########################
COPY . "${KPI_SRC_DIR}"
###########################
# Copy virtualenv #
###########################
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
COPY ./dependencies/pip/requirements.txt "${TMP_DIR}/pip_dependencies.txt"
COPY --from=build-python "$VIRTUAL_ENV" "$VIRTUAL_ENV"
###########################
# Install `npm` packages. #
###########################
WORKDIR ${KPI_SRC_DIR}/
RUN rm -rf ${KPI_NODE_PATH} && \
mkdir -p "${TMP_DIR}/.npm" && \
npm config set cache "${TMP_DIR}/.npm" --global && \
npm install -g [email protected] && \
npm install -g check-dependencies@1 && \
rm -rf "${KPI_SRC_DIR}/jsapp/fonts" && \
rm -rf "${KPI_SRC_DIR}/jsapp/compiled" && \
npm install --quiet && \
npm cache clean --force
ENV PATH $PATH:${KPI_NODE_PATH}/.bin
######################
# Build client code. #
######################
RUN npm run build
###########################
# Organize static assets. #
###########################
RUN python manage.py collectstatic --noinput
#####################################
# Retrieve and compile translations #
#####################################
RUN git submodule init && \
git submodule update --remote && \
python manage.py compilemessages
##########################################
# Persist the log and email directories. #
##########################################
RUN mkdir -p "${KPI_LOGS_DIR}/" "${KPI_SRC_DIR}/emails"
#################################################
# Handle runtime tasks and create main process. #
#################################################
# Using `/etc/profile.d/` as a repository for non-hard-coded environment variable overrides.
RUN echo "export PATH=${PATH}" >> /etc/profile && \
echo 'source /etc/profile' >> /root/.bashrc && \
echo 'source /etc/profile' >> /home/${UWSGI_USER}/.bashrc
# Remove getty* services to avoid errors of absent tty at sv start-up
RUN rm -rf /etc/runit/runsvdir/default/getty-tty*
# Create symlinks for runsv services
RUN ln -s "${KPI_SRC_DIR}/docker/run_uwsgi.bash" "${SERVICES_DIR}/uwsgi/run" && \
ln -s "${KPI_SRC_DIR}/docker/run_celery.bash" "${SERVICES_DIR}/celery/run" && \
ln -s "${KPI_SRC_DIR}/docker/run_celery_low_priority.bash" "${SERVICES_DIR}/celery_low_priority/run" && \
ln -s "${KPI_SRC_DIR}/docker/run_celery_beat.bash" "${SERVICES_DIR}/celery_beat/run"
# Add/Restore `UWSGI_USER`'s permissions
# chown of `${TMP_DIR}/.npm` is a hack needed for kobo-install-based staging deployments;
# see internal discussion at https://chat.kobotoolbox.org/#narrow/stream/4-Kobo-Dev/topic/Unu.2C.20du.2C.20tri.2C.20kvar.20deployments/near/322075
RUN chown -R ":${UWSGI_GROUP}" ${CELERY_PID_DIR} && \
chmod g+w ${CELERY_PID_DIR} && \
chown -R "${UWSGI_USER}:${UWSGI_GROUP}" ${KPI_SRC_DIR}/emails/ && \
chown -R "${UWSGI_USER}:${UWSGI_GROUP}" ${KPI_LOGS_DIR} && \
chown -R "${UWSGI_USER}:${UWSGI_GROUP}" ${TMP_DIR} && \
chown -R root:root "${TMP_DIR}/.npm"
EXPOSE 8000
CMD ["/bin/bash", "-c", "exec ${KPI_SRC_DIR}/docker/init.bash"]