Gunicorn WSGI Dockerfile (Python webserver)

Write the Dockerfile #

First, I really love using alpine linux for all my docker images, when testing my containers,
they most of the time only take a few megabytes of space. For the version, I have nothing special running linux related so always
keeping latest is allright (by not specifying any version for the alpine part). For Python, we're currently at 3.7.4, by putting 3.7
you ensure your docker image to always have those minor updates (latest of 3.7).

In my app, I needed some libraries that required compiling and cryptography libraries, so I added gcc,
libc-dev, libressl-dev and libffi-dev.

For the rest it's just the normal python commands to install packages using pip.

FROM python:3.7-alpine

RUN apk add gcc libc-dev libffi-dev libressl-dev


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

COPY . /app


CMD gunicorn -b app.wsgi

Build the docker image #

To build the docker image you can simply run the build command in the right directory:

docker build . -t your-django-app

Run the app #

Since we exposed our gunicorn app on port 8000, we need to publish (-p) our container port to one of the host ports,
here I'll be chosing 8000 for simplicity, so in daemon mode (-d):

docker run -d -p 8000:8000 your-django-app


Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.