Skip to content

Shows a debugging of a dockerized Python script interacting with a dockerized Postgres databas

License

Notifications You must be signed in to change notification settings

GhentCDH/Python-db-dev

Repository files navigation

Containerized database development with Python

This repository shows a dockerized Python script interacting with a Postgres database - using the SQLAlchemy library to ease db interaction. A docker compose script is used to spin up a database server and a development environment.

There are a few advantages of using containers like Docker, even during development: it makes setting a development reproducable, transferable and makes dependencies and assumptions on which system the script is running. It also makes you think about configuration and responsibilities of systems.

Another advantage is that it keeps a development machine clean: only docker needs to be installed. To interact with a postgres database, for example, there is no need to install - and configure - a postgres server on the development machine.

A disadvantage is that a bit of configuration is needed. This repository can help in this regard.

Getting started

To get started, clone and 'compose' the images. The docker compse command needs an .env file so this is linked to the configuration example. If this runs succesfuly, the IDE can be connected to the container for development.

git clone git@github.com:GhentCDH/Python-db-dev.git
cd Python-db-dev
ln -s env.example .env
docker compose -f compose.dev.yaml up

In the video below Visual Studio Code is used to (re)start two containers. A container with a PostgreSQL database server and a container with a Python development environment. A IDE session is attached to the running Python container, executing code and debugging is done in the container itself. To allow VS Code to access debugging the Visual Studio Code Python plugin needs to be available in the container. So first start the Attach to running container and then in VS Code install the Python plugin in the container.

dev_in_container.mp4

Background info on the Python environment

The Python environment is managed by PDM, a python dependency manager. The script connects via SQLAlchemy - an ORM - to the database which is accessed via a connection string which is configured via the .env and looks like postgresql://exampleuser:examplepass@database/test.

To format Python code via the ruff formatter, execute pdm run ruff format src. Ruff is also added as a pdm dependency.

The python image is based of the standard Python docker image but adds a couple of extra packages to interact with the database and installs the pdm manager.

Credits

The GhentCDH development team: Pieterjan Depotter, Frederic Lamsens, Joren Six

About

Shows a debugging of a dockerized Python script interacting with a dockerized Postgres databas

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published