|
| 1 | +ARG PYTHON_VERSION=3.9-slim-buster |
| 2 | + |
| 3 | +# define an alias for the specfic python version used in this file. |
| 4 | +FROM python:${PYTHON_VERSION} as python |
| 5 | + |
| 6 | +# Python build stage |
| 7 | +FROM python as python-build-stage |
| 8 | + |
| 9 | +# Install apt packages |
| 10 | +RUN apt-get update && apt-get install --no-install-recommends -y \ |
| 11 | + # dependencies for building Python packages |
| 12 | + build-essential \ |
| 13 | + # need git for pip installs |
| 14 | + git |
| 15 | + |
| 16 | +# Requirements are installed here to ensure they will be cached. |
| 17 | +COPY ./requirements.txt . |
| 18 | + |
| 19 | +# Create Python Dependency and Sub-Dependency Wheels. |
| 20 | +RUN pip wheel --wheel-dir /usr/src/app/wheels \ |
| 21 | + -r requirements.txt |
| 22 | + |
| 23 | +# Python 'run' stage |
| 24 | +FROM python as python-run-stage |
| 25 | + |
| 26 | +ARG APP_HOME=/app |
| 27 | + |
| 28 | +ENV PYTHONUNBUFFERED 1 |
| 29 | +ENV PYTHONDONTWRITEBYTECODE 1 |
| 30 | + |
| 31 | +WORKDIR ${APP_HOME} |
| 32 | + |
| 33 | +RUN addgroup --system django \ |
| 34 | + && adduser --system --ingroup django django |
| 35 | + |
| 36 | +# Install required system dependencies |
| 37 | +RUN apt-get update && apt-get install --no-install-recommends -y \ |
| 38 | + # Translations dependencies |
| 39 | + gettext \ |
| 40 | + # other dependencies |
| 41 | + procps \ |
| 42 | + curl \ |
| 43 | + # dependencies for installing Python packages from git |
| 44 | + build-essential \ |
| 45 | + git \ |
| 46 | + # cleaning up unused files |
| 47 | + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ |
| 48 | + && rm -rf /var/lib/apt/lists/* |
| 49 | + |
| 50 | +# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction |
| 51 | +# copy python dependency wheels from python-build-stage |
| 52 | +COPY --from=python-build-stage /usr/src/app/wheels /wheels/ |
| 53 | + |
| 54 | +# sirun |
| 55 | +RUN curl -sL https://github.com/DataDog/sirun/releases/download/v0.1.8/sirun-v0.1.8-x86_64-unknown-linux-gnu.tar.gz | tar zxf - -C /tmp && mv /tmp/sirun /usr/bin |
| 56 | + |
| 57 | +# k6 |
| 58 | +RUN curl -SL https://github.com/k6io/k6/releases/download/v0.32.0/k6-v0.32.0-linux-amd64.tar.gz | tar zxf - -C /tmp && mv /tmp/k6-v0.32.0-linux-amd64/k6 /usr/bin |
| 59 | + |
| 60 | +# jq |
| 61 | +RUN curl -L -o /usr/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 && \ |
| 62 | + chmod +x /usr/bin/jq |
| 63 | + |
| 64 | +# copy application code to WORKDIR |
| 65 | +COPY --chown=django:django . ${APP_HOME} |
| 66 | + |
| 67 | +# make django owner of the WORKDIR directory as well. |
| 68 | +RUN chown django:django ${APP_HOME} |
| 69 | + |
| 70 | +# top level directory for artifacts |
| 71 | +ARG ARTIFACTS=/artifacts |
| 72 | +RUN mkdir -p ${ARTIFACTS} |
| 73 | +RUN chown django:django ${ARTIFACTS} |
| 74 | + |
| 75 | +USER django |
| 76 | + |
| 77 | +# Create venv |
| 78 | +ENV VIRTUAL_ENV=/app/venv |
| 79 | +RUN python3 -m venv $VIRTUAL_ENV |
| 80 | +ENV PATH="$VIRTUAL_ENV/bin:$PATH" |
| 81 | + |
| 82 | +# use wheels to install python dependencies |
| 83 | +RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* |
| 84 | + |
| 85 | +# Run migrations in image creation as data will not change during run |
| 86 | +RUN python /app/manage.py collectstatic --noinput \ |
| 87 | + && python /app/manage.py migrate |
| 88 | + |
| 89 | +# For performance testing |
| 90 | +ENV DDTRACE_GIT_COMMIT_ID "" |
| 91 | +ENV DDTRACE_WHEELS "" |
| 92 | +ENV WEB_CONCURRENCY 1 |
| 93 | +ENV SIRUN_NO_STDIO 0 |
| 94 | +ENV K6_STATSD_ENABLE_TAGS "true" |
| 95 | + |
| 96 | +ENTRYPOINT ["/app/entrypoint"] |
| 97 | +CMD ["/app/benchmark"] |
0 commit comments