Deploying Python and PostgreSQL in Docker

Shrinand Kadekodi
4 min readJan 2, 2022

One of the most common issues that we run into during software development is mismatch in development and testing environment. For example, a developer may use the latest version of .Net Framework for creating any application but at the testing or deployment side things may not be the same. Maybe the version is old or any dependent libraries may support only the latest version and so on 😅. The problems are many. To solve this we can use Docker to test on different environments and get the best solution which will work on any environment. The topic on Docker is big and in this post we will cover only points to get our application running 😅.
We will run the application created in last post on Docker. You can see the previous post which uses psycopg2 here — https://evrythngunder3d.wordpress.com/2021/12/25/database-in-python-using-psycopg2/
or in medium — https://shrinandkadekodi.medium.com/database-in-python-using-psycopg2-7c495cca1ab3

Docker Installation:

The first and foremost thing is to install Docker Desktop in your system. You can find the link here — https://www.docker.com/products/docker-desktop. There are couple of more steps that has to be taken care. Docker requires WSL2 installation. You can refer this you tube video for more details — https://www.youtube.com/watch?v=xVVEZB4sQqo.

Environment creation:

The next step is to create the folder structure for our Python code. For this create a new folder and inside it create two subfolders as shown in below image:

The app folder will have our Python code and pickle file. In general it should contain all the codes and required data associated with the code inside this folder created. In addition to this, we will have two more extra file, namely — Dockerfile and requirements.txt. For our case the app folder should have below contents:

The requirements.txt is fairly simple. It consists of all the additional packages or software that may be needed for our code to run smoothly.

Here I had added pickle as well. But it was showing error while building in Docker. Removing it from requirements.txt worked.
The next file is the Dockerfile. This file will be referenced by Docker during build process. Dockerfile consists of the instructions which are executed by Docker. Note that this file has no extension.

FROM python:latest
WORKDIR /code
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY databaseExp.py databaseExp.py
COPY data.pickle data.pickle
CMD ["python", "-u", "databaseExp.py"]

Similarly we will write a Dockerfile for our database for PostgreSQL as well.

FROM postgres:latest
ENV POSTGRES_PASSWORD=1234
ENV POSTGRES_USER=postgres
ENV POSTGRES_DB=postgres

Another last thing that we will be creating is yaml file for Docker Compose. This is needed to create defining and running multi-container Docker applications. The code is as follows:

version: "3.8"
services:
db:
build: ./database/
ports:
- 5433:5432
volumes:
- ./postgres:/docker-entrypoint-initdb.d
networks:
- app-tier
app :
build: ./app/
depends_on:
- db
networks:
- app-tier
networks:
app-tier:
driver: bridge

Honestly speaking, this was created by trial-and-error by checking different combinations 😅. After using the above code, I was able to get the Docker build successfully and run the python code. So at the end we can see the following folder structure:

Another thing to note would be to change the code where database connection is done. Since we are using db in the compose yaml, replace all the instances of localhost by db in the Python code.

Build Docker:

To build and run the Docker container you need to open windows Powershell at the folder path. Also open the Docker Desktop. After opening Powershell type the command — docker-compose up -build.
That’s it! Docker will take care of the rest. It will take some time before it can show the below result:

I hope this post was helpful! Let me know in the comments.

References:
- A lot of googling amongst which the major sources were stackoverflow.com, dev.to, dabbleofdevops.com and youtube.com

Originally published at http://evrythngunder3d.wordpress.com on January 2, 2022.

--

--