Dockerizing Django with Postgres, Gunicorn, and Nginx

Dockerizing Django with Postgres, Gunicorn, and Nginx

Install Docker, if you don't already have it, then add a Dockerfile to the "app" directory:

So, we started with an Alpine-based Docker image for Python 3.8.0. We then set a working directory along with two environment variables:

[…]

Review Docker for Python Developers for more on structuring Dockerfiles as well as some best practices for configuring Docker for Python-based development.

Next, add a docker-compose.yml file to the project root:

[…]

First, add a new service called db to docker-compose.yml:

[…]

Update the Dockerfile to install the appropriate packages required for Psycopg2:

[…]

Then, update the Dockerfile to copy over the entrypoint.sh file and run it as the Docker entrypoint command:

[…]

If you have multiple environments, you may want to look at using a docker-compose.override.yml configuration file. With this approach, you'd add your base config to a docker-compose.yml file and then use a docker-compose.override.yml file to override those config settings based on the environment.

[…]