Skip to content

Instantly share code, notes, and snippets.

@jensneuhaus
Last active August 29, 2019 10:44
Show Gist options
  • Select an option

  • Save jensneuhaus/e6976d42cb1fdd26a887b16ca8c40ed7 to your computer and use it in GitHub Desktop.

Select an option

Save jensneuhaus/e6976d42cb1fdd26a887b16ca8c40ed7 to your computer and use it in GitHub Desktop.
Dockerfile Django project for usage with Pip, Heroku, Healthcheck
# 1 Use the Python Slim
FROM python:3.6.5-slim
# 2 Force stdin, stdout and stderr to be totally unbuffered and don´t write .pyc or .pyo files
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
# 3 Create the Working dir and the staticfiles folder
RUN mkdir -p /app \
&& mkdir -p /venv \
&& mkdir -p /app/staticfiles
# 4 Copy some required files
COPY Pipfile Pipfile.lock devops/docker/entrypoint.sh devops/docker/wait-for-postgres.sh /
# 5 Install some requirements
RUN set -xe \
&& apt-get update && apt-get install -y --no-install-recommends \
curl \
gettext \
python-dev \
python-pip \
python-setuptools \
python-wheel \
python-cffi \
libcairo2>=1.14.2 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libcairo2-dev \
libgdk-pixbuf2.0-0 \
libffi-dev \
shared-mime-info \
git \
&& pip install pipenv \
&& LIBRARY_PATH=/lib:/usr/lib /bin/sh -c "pipenv install --system" \
&& groupadd -r django \
&& useradd -r -g django django \
&& chmod +x /entrypoint.sh \
&& chmod +x /wait-for-postgres.sh \
&& chown django /entrypoint.sh \
&& chown django /wait-for-postgres.sh \
&& rm -rf /var/lib/apt/lists/*
# && runDeps="$( \
# scanelf --needed --nobanner --recursive /venv \
# | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
# | sort -u \
# | xargs -r apk info --installed \
# | sort -u \
# )" \
# && apk add --virtual .python-rundeps $runDeps \
# && apk del .build-deps
# 6 Copy the App
COPY . /app/
# 7 user django is the owner
RUN chown -R django /app
# 8
WORKDIR /app
# 9 Run as django user
USER django
#10+11 Add git commit hash ARG , make it visible in frontend administrator interface
ARG GIT_COMMIT=not-set-during-build
ENV GIT_COMMIT_HASH=$GIT_COMMIT
#12+13 Add datetime ARG , make it visible in frontend admistrator interface
ARG DEPLOYMENT_DATE='YYYY-MM-DD HH:mm'
ENV DEPLOYMENT_DATE=$DEPLOYMENT_DATE
# 14
HEALTHCHECK CMD curl --fail http://localhost:8009/ || exit 1
# 15
CMD ["/entrypoint.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment