Apline based images have a huge advantage by it’s final size but in most cases in the Python world if neotiates by Python dependencies which takes too long to build project packages and final size sometimes may be greater than same image build on debian based image. But Apline still great solution for small Django applications to keep final image tiny.

This post written after the similar post related to the debian based image build proccess, so I wont specify here all environmental details and just provide few Dockerfile examples which you may to use for start.

Basic Image

This is example of Django Apline based image with basic requirements to run Django in out-of-box state.

FROM python:3.9-alpine3.16

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
    PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100

RUN apk update \
    && apk add --no-cache --virtual .build-deps python3-dev libffi-dev gcc musl-dev make libevent-dev build-base \
    && pip install --no-cache-dir gunicorn gevent \
    && apk del .build-deps

# Copy app source
COPY . .

# Install requirements
RUN pip install --no-cache-dir -r requirements.txt

# Copy and enable entrypoint.sh
COPY .docker/app/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]

EXPOSE 8000
# 'app' in the 'app.wsgi:application' - it is directory name where placed settings.py file
CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "--error-logfile", "-", "--access-logfile", "-", "app.wsgi:application"]

Postgres Ready Image

This image will contains prebuilt psycopg2 in the python packages.

FROM python:3.9-alpine3.16

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
    PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100

RUN apk update \
    && apk add --no-cache --virtual .build-deps python3-dev libffi-dev gcc musl-dev make libevent-dev build-base postgresql-dev \
    && apk add --no-cache postgresql-client \
    && pip install --no-cache-dir gunicorn gevent psycopg2 \
    && apk del .build-deps

# Copy app source
COPY . .

# Install requirements
RUN pip install --no-cache-dir -r requirements.txt

# Copy and enable entrypoint.sh
COPY .docker/app/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]

EXPOSE 8000
# 'app' in the 'app.wsgi:application' - it is directory name where placed settings.py file
CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "--error-logfile", "-", "--access-logfile", "-", "app.wsgi:application"]

MySQL Ready Image

FROM python:3.9-alpine3.16

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
    PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100

RUN apk update \
    && apk add --no-cache --virtual .build-deps python3-dev libffi-dev gcc musl-dev make libevent-dev build-base mariadb-dev \
    && pip install --no-cache-dir gunicorn gevent mysqlclient \
    && apk del .build-deps

# Copy app source
COPY . .

# Install requirements
RUN pip install --no-cache-dir -r requirements.txt

# Copy and enable entrypoint.sh
COPY .docker/app/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]

EXPOSE 8000
# 'app' in the 'app.wsgi:application' - it is directory name where placed settings.py file
CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "--error-logfile", "-", "--access-logfile", "-", "app.wsgi:application"]