Default Python app's Dockerfile

Hi! this might be just a suggestion. I believe you can just default to python:$PYTHON_VERSION-slim flavor for Python apps. Also, you don’t need to reinstall python stuff (the apt update && install python3-* layer), as everything should be provisioned in the image.

That way you reduce the image from 1G to ~230MB! Then, anyone is free to update it, but the starting point can save you some space for free.

This could be your default Dockefile for Python apps,

ARG PYTHON_VERSION=3.7

FROM python:${PYTHON_VERSION}-slim

RUN mkdir -p /app
WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

RUN python manage.py collectstatic --noinput

EXPOSE 8080

# replace APP_NAME with module name
CMD ["gunicorn", "--bind", ":8080", "--workers", "2", "demo.wsgi"]

Not sure if APP_NAME was meant to be replaced with the app folder containing the wsgi module automatically, but I had to change it to the real location.

2 Likes