From 0e242801e7ca6e6f387e6ecc99df493113102e93 Mon Sep 17 00:00:00 2001 From: luptmoor Date: Wed, 25 Feb 2026 18:19:22 +0100 Subject: [PATCH] simple Python app that conencts to db --- Dockerfile | 12 ++++++++++++ app/main.py | 35 +++++++++++++++++++++++++++++++++++ app/requirements.txt | 2 ++ docker-compose.yml | 19 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 Dockerfile create mode 100644 app/main.py create mode 100644 app/requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5351a70 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.11-slim + +RUN apt-get update && apt-get install -y libpq-dev gcc \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +COPY app/requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY app/ . + +CMD ["python", "main.py"] \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..b62f1c0 --- /dev/null +++ b/app/main.py @@ -0,0 +1,35 @@ +import os +import psycopg2 +import time + + +DB_USER = os.getenv("POSTGRES_USER"); +DB_PASSWWORD = os.getenv("POSTGRES_PASSWORD"); +DB_NAME = os.getenv("POSTGRES_DB"); +DB_HOST = os.getenv("POSTGRES_HOST"); + + + +def connect_db(): + while True: + try: + conn = psycopg2.connect( + host=DB_HOST, + database=DB_NAME, + user=DB_USER, + password=DB_PASSWWORD + ) + print("Connected to the database successfully!") + return conn + except psycopg2.OperationalError as e: + print(f"Database connection failed: {e}") + time.sleep(3) + + +if __name__ == "__main__": + print("starting app.") + conn = connect_db(); + cursor = conn.cursor() + cursor.execute("SELECT postgis_full_version();") + + print("obst", cursor.fetchone()); \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..80ac694 --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1,2 @@ +psycopg2-binary==2.9.3 +SQLAlchemy==1.4.36 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a14b35b..ad81c79 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,25 @@ services: networks: - backend + + + + app: + build: . + container_name: geo-app + environment: + PYTHONUNBUFFERED: 1 + POSTGRES_USER: ${GEODB_USER} + POSTGRES_PASSWORD: ${GEODB_PW} + POSTGRES_DB: ${GEODB_NAME} + POSTGRES_HOST: geo-db-postgis + depends_on: + - geo-db-postgis + networks: + - backend + + + volumes: pgdata: driver: local