Skip to content

Commit b3a2e47

Browse files
committed
Consolidate start commands
1 parent db39317 commit b3a2e47

File tree

9 files changed

+176
-180
lines changed

9 files changed

+176
-180
lines changed

compose/django/Dockerfile

Lines changed: 72 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ ARG STAGE=production
1111
COPY ./requirements /requirements
1212
# install build dependencies
1313
RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \
14-
apk update \
15-
&& apk add --no-cache build-base \
16-
# psycopg2 dependencies
17-
&& apk add --no-cache --virtual build-deps gcc python3-dev musl-dev \
18-
&& apk add --no-cache postgresql-dev \
19-
# Pillow dependencies
20-
&& apk add --no-cache jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
21-
# CFFI dependencies
22-
&& apk add --no-cache libffi-dev py-cffi \
23-
# XLSX dependencies
24-
&& apk add --no-cache libxml2-dev libxslt-dev \
25-
# Rust and Cargo required by the ``cryptography`` Python package - only required during build
26-
&& apk add --no-cache rust \
27-
&& apk add --no-cache cargo \
28-
# && pip install -r /requirements/${STAGE}.txt \
29-
# build wheels
30-
&& pip install wheel && pip wheel --wheel-dir=/tmp/wheels -r /requirements/${STAGE}.txt \
31-
# remove the virtual package group 'build-deps'
32-
&& apk del build-deps
14+
apk update \
15+
&& apk add --no-cache build-base \
16+
# psycopg2 dependencies
17+
&& apk add --no-cache --virtual build-deps gcc python3-dev musl-dev \
18+
&& apk add --no-cache postgresql-dev \
19+
# Pillow dependencies
20+
&& apk add --no-cache jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
21+
# CFFI dependencies
22+
&& apk add --no-cache libffi-dev py-cffi \
23+
# XLSX dependencies
24+
&& apk add --no-cache libxml2-dev libxslt-dev \
25+
# Rust and Cargo required by the ``cryptography`` Python package - only required during build
26+
&& apk add --no-cache rust \
27+
&& apk add --no-cache cargo \
28+
# && pip install -r /requirements/${STAGE}.txt \
29+
# build wheels
30+
&& pip install wheel && pip wheel --wheel-dir=/tmp/wheels -r /requirements/${STAGE}.txt \
31+
# remove the virtual package group 'build-deps'
32+
&& apk del build-deps
3333
# ---------------------------------------------
3434
# END build image stage
3535
# ---------------------------------------------
@@ -48,38 +48,38 @@ ENV PYTHONPATH="$PYTHONPATH:/app/config"
4848
ARG USER_UID=1000
4949
ARG USER_GID=$USER_UID
5050
RUN if [ -n "$(getent group ${USER_GID})" ]; \
51-
then \
52-
apk --no-cache add shadow; \
53-
groupmod -n "django" "${USER_GID}"; \
54-
else \
55-
addgroup --gid "${USER_GID}" "django"; \
56-
fi && \
57-
if [ -n "$(getent passwd ${USER_UID})" ]; \
58-
then \
59-
apk --no-cache add shadow; \
60-
usermod -l "django" -g "${USER_GID}" -d "/app"; \
61-
else \
62-
adduser \
63-
--home "/app" \
64-
--gecos "MAIL_DIR=/dev/null" \
65-
--shell /bin/ash \
66-
--ingroup "django" \
67-
--system \
68-
--disabled-password \
69-
--no-create-home \
70-
--uid "${USER_UID}" \
71-
"django"; \
72-
fi
51+
then \
52+
apk --no-cache add shadow; \
53+
groupmod -n "django" "${USER_GID}"; \
54+
else \
55+
addgroup --gid "${USER_GID}" "django"; \
56+
fi && \
57+
if [ -n "$(getent passwd ${USER_UID})" ]; \
58+
then \
59+
apk --no-cache add shadow; \
60+
usermod -l "django" -g "${USER_GID}" -d "/app"; \
61+
else \
62+
adduser \
63+
--home "/app" \
64+
--gecos "MAIL_DIR=/dev/null" \
65+
--shell /bin/ash \
66+
--ingroup "django" \
67+
--system \
68+
--disabled-password \
69+
--no-create-home \
70+
--uid "${USER_UID}" \
71+
"django"; \
72+
fi
7373

7474
# install runtime dependencies. `add --no-cache` performs an apk update, adds packages and excludes caching
7575
# in order to not require deletion of apk cache.
7676
RUN apk add --no-cache postgresql-dev \
77-
# Pillow dependencies
78-
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
79-
# CFFI dependencies
80-
libffi-dev py-cffi \
81-
# XLSX dependencies
82-
libxml2-dev libxslt-dev
77+
# Pillow dependencies
78+
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
79+
# CFFI dependencies
80+
libffi-dev py-cffi \
81+
# XLSX dependencies
82+
libxml2-dev libxslt-dev
8383

8484
# ensure that we are using the same requirements from the build stage
8585
COPY --from=build /requirements /tmp/requirements
@@ -88,11 +88,11 @@ COPY --from=build /requirements /tmp/requirements
8888

8989
# combine build and ${STAGE}.txt - remove --no-binary to installing our own wheels
9090
RUN --mount=type=bind,target=/tmp/wheels,source=/tmp/wheels,from=build \
91-
--mount=type=cache,mode=0755,target=/root/.cache/pip \
92-
du -shc /tmp/wheels \
93-
&& ( cat /tmp/requirements/base.txt; sed -e 's/--no-binary.*//' -e 's/^-r .*//' /tmp/requirements/${STAGE}.txt ) | tee /tmp/requirements.txt >/dev/null \
94-
&& pip install --find-links=/tmp/wheels -r /tmp/requirements.txt \
95-
&& rm -rf /tmp/requirements /tmp/requirements.txt
91+
--mount=type=cache,mode=0755,target=/root/.cache/pip \
92+
du -shc /tmp/wheels \
93+
&& ( cat /tmp/requirements/base.txt; sed -e 's/--no-binary.*//' -e 's/^-r .*//' /tmp/requirements/${STAGE}.txt ) | tee /tmp/requirements.txt >/dev/null \
94+
&& pip install --find-links=/tmp/wheels -r /tmp/requirements.txt \
95+
&& rm -rf /tmp/requirements /tmp/requirements.txt
9696
# ---------------------------------------------
9797
# END django image stage
9898
# ---------------------------------------------
@@ -106,18 +106,17 @@ FROM django as django-production
106106
COPY --chown=django . /app
107107

108108
# copy the entrypoint and run scripts
109-
RUN cp -ap /app/compose/django/entrypoint /entrypoint \
110-
&& cp -vap /app/compose/django/start /start \
111-
&& cp -vap /app/compose/django/queue/start /start-queue \
112-
&& cp -vap /app/compose/django/seed_data /seed_data \
113-
&& chmod -v 0755 /entrypoint /start /start-queue /seed_data \
114-
# remove all carriage returns in the case that a user checks out the files on a windows system
115-
# and has their git core.eol set to native or crlf
116-
&& sed -i 's/\r$//g' /entrypoint /start /start-queue /seed_data \
117-
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
118-
# so that it is correctly propagated to the named volume
119-
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
120-
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
109+
RUN for target in /app/compose/django/*; \
110+
do ln "$target" /"$(basename "$target")" \
111+
&& chmod +X /"$(basename "$target")" \
112+
# remove all carriage returns in the case that a user checks out the files on a windows system
113+
# and has their git core.eol set to native or crlf
114+
&& sed -i 's/\r$//g' /entrypoint /start /seed_data; \
115+
done \
116+
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
117+
# so that it is correctly propagated to the named volume
118+
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
119+
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
121120
# ---------------------------------------------
122121
# END production stage
123122
# ---------------------------------------------
@@ -128,21 +127,17 @@ RUN cp -ap /app/compose/django/entrypoint /entrypoint \
128127
FROM django as django-local
129128

130129
# add our application
131-
COPY --chown=django ./compose/django/entrypoint /entrypoint
132-
COPY --chown=django ./compose/django/start-dev /start
133-
COPY --chown=django ./compose/django/queue/start /start-queue
134-
COPY --chown=django ./compose/django/seed_data /seed_data
135-
130+
COPY --chown=django ./compose/django/ /
136131

137132
# copy the entrypoint and run scripts
138-
RUN chmod -v 0755 /entrypoint /start /start-queue /seed_data \
139-
# remove all carriage returns in the case that a user checks out the files on a windows system
140-
# and has their git core.eol set to native or crlf
141-
&& sed -i 's/\r$//g' /entrypoint /start /start-queue /seed_data \
142-
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
143-
# so that it is correctly propagated to the named volume
144-
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
145-
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
133+
RUN chmod -v 0755 /entrypoint /start /seed_data \
134+
# remove all carriage returns in the case that a user checks out the files on a windows system
135+
# and has their git core.eol set to native or crlf
136+
&& sed -i 's/\r$//g' /entrypoint /start /seed_data \
137+
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
138+
# so that it is correctly propagated to the named volume
139+
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
140+
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
146141
# ---------------------------------------------
147142
# END local stage
148143
# ---------------------------------------------

compose/django/queue/start

Lines changed: 0 additions & 7 deletions
This file was deleted.

compose/django/start

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,75 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
AVATAR_DIR=/app/ghostwriter/media/user_avatars
8-
EVIDENCE_DIR=/app/ghostwriter/media/evidence
9-
TEMPLATE_DIR=/app/ghostwriter/media/templates
7+
readonly AVATAR_DIR=/app/ghostwriter/media/user_avatars
8+
readonly EVIDENCE_DIR=/app/ghostwriter/media/evidence
9+
readonly TEMPLATE_DIR=/app/ghostwriter/media/templates
1010

11-
TEMPLATE_PATH_DOCX=/app/ghostwriter/reporting/templates/reports/template.docx
12-
TEMPLATE_PATH_PPTX=/app/ghostwriter/reporting/templates/reports/template.pptx
11+
readonly TEMPLATE_PATH_DOCX=/app/ghostwriter/reporting/templates/reports/template.docx
12+
readonly TEMPLATE_PATH_PPTX=/app/ghostwriter/reporting/templates/reports/template.pptx
1313

14-
[[ ! -d "$EVIDENCE_DIR" ]] && mkdir -p "$EVIDENCE_DIR" && chown -R django "$EVIDENCE_DIR"
15-
[[ ! -d "$AVATAR_DIR" ]] && mkdir -p "$AVATAR_DIR" && chown -R django "$AVATAR_DIR"
16-
[[ ! -d "$TEMPLATE_DIR" ]] && mkdir -p "$TEMPLATE_DIR" && chown -R django "$TEMPLATE_DIR"
14+
function create_dir {
15+
local -r target_directory="$1";
16+
local -r target_user="${2:-django}";
1717

18-
cp -u -p "$TEMPLATE_PATH_DOCX" "$TEMPLATE_DIR"
19-
cp -u -p "$TEMPLATE_PATH_PPTX" "$TEMPLATE_DIR"
18+
if test -d "$target_directory";
19+
then return 0;
20+
fi
2021

21-
python /app/manage.py collectstatic --noinput
22-
python /app/manage.py migrate
23-
# /usr/local/bin/daphne -b 0.0.0.0 -p 5000 config.asgi:application
24-
uvicorn config.asgi:application --host 0.0.0.0 --port 5000
22+
mkdir -p "$target_directory";
23+
chown -R "$target_user": "$target_directory";
24+
}
25+
26+
function prepare {
27+
create_dir "$EVIDENCE_DIR" "${USER:-django}";
28+
create_dir "$AVATAR_DIR" "${USER:-django}";
29+
create_dir "$TEMPLATE_DIR" "${USER:-django}";
30+
31+
cp -u -p "$TEMPLATE_PATH_DOCX" "${TEMPLATE_DIR}/";
32+
cp -u -p "$TEMPLATE_PATH_PPTX" "${TEMPLATE_DIR}/";
33+
}
34+
35+
function queue {
36+
python manage.py qcluster;
37+
}
38+
39+
function live {
40+
python /app/manage.py collectstatic --noinput;
41+
python /app/manage.py migrate;
42+
# /usr/local/bin/daphne -b 0.0.0.0 -p 5000 config.asgi:application
43+
uvicorn config.asgi:application --host 0.0.0.0 --port 5000;
44+
}
45+
46+
function dev {
47+
python manage.py makemigrations;
48+
python manage.py migrate;
49+
# python manage.py runserver 0.0.0.0:8000
50+
uvicorn config.asgi:application --host 0.0.0.0 --reload;
51+
}
52+
53+
function debug {
54+
python -m pip install --upgrade pip;
55+
python manage.py makemigrations;
56+
python manage.py migrate;
57+
pip install debugpy -t /tmp;
58+
python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading;
59+
}
60+
61+
function main {
62+
local -r target="${1:-live}";
63+
64+
prepare;
65+
case "$target" in
66+
live | debug | dev)
67+
prepare;
68+
$target;
69+
;;
70+
queue )
71+
$target;
72+
;;
73+
*)
74+
echo >&2 "Unknown target: $target";
75+
exit 1;
76+
;;
77+
esac
78+
}

compose/django/start-dev

Lines changed: 0 additions & 24 deletions
This file was deleted.

compose/local/django/start_debug

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)