From 5cd649b741611b49647c9b5842051520d84ba058 Mon Sep 17 00:00:00 2001 From: sanek Date: Wed, 11 Dec 2024 18:05:48 +0000 Subject: [PATCH] first commit --- Dockerfile | 7 +++++++ app.py | 25 ++++++++++++++++++++++ db/init.sql | 18 ++++++++++++++++ docker-compose.yaml | 51 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 5 files changed, 102 insertions(+) create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 db/init.sql create mode 100644 docker-compose.yaml create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..86ae833 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.10 +WORKDIR /app +COPY requirements.txt /app/requirements.txt +RUN pip install --no-cache-dir -r /app/requirements.txt +COPY . /app + +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..d52f743 --- /dev/null +++ b/app.py @@ -0,0 +1,25 @@ +import psycopg2 + +print('Can`t establish connection to database') + +''' +conn = psycopg2.connect( + dbname="kolchin", + user="kolchin", + password="postgres", + host="pg", + port="5432" +) + +cursor = conn.cursor() + +with conn.cursor() as curs: + curs.execute(CREATE TABLE IF NOT EXISTS tablee( + name varchar(10) NOT NULL, + surname varchar, + city varchar, + age smallint + );) +conn.commit() +conn.close() +''' diff --git a/db/init.sql b/db/init.sql new file mode 100644 index 0000000..57a4cdc --- /dev/null +++ b/db/init.sql @@ -0,0 +1,18 @@ +--create database test_db; +--create user test_user with encrypted password '123'; +--grant all privileges on database test_db to test_user; +CREATE TABLE IF NOT EXISTS test_table( + --id INT PRIMARY KEY, + name varchar(10) NOT NULL, + surname varchar, + city varchar, + age smallint + ); + +insert into test_table (name, surname, city, age) +values ('Aleksandr', 'Kolchin', 'Murmansk', 27), ('Dima', 'Ivanov', 'Moscow', 20), ('Petr', 'Sevanov', 'Omsk', 30), ('Oleg', 'Solop', 'Moscow', 25), +('Aleksandr', 'Bistrov', 'Kaliningrad', 22), ('Ivan', 'Kochev', 'Apatity', 33), ('Sergey', 'Sergeev', 'Piter', 25), ('Nikita', 'Korol', 'Voronezh', 25), +('Semen', 'Semenov', 'Yaroslavl', 26), ('Yaroslav', 'Vaneev', 'Kirov', 39), ('Oleg', 'Simonov', 'Moscow', 20), ('Nikita', 'Levin', 'Murmansk', 29), +('Sergey', 'Moiseev', 'Apatity', 19), ('Roman', 'Padorin', 'Orel', 32), ('Petr', 'Krychkov', 'Kandalaksha', 26), ('Stas', 'Kotin', 'Norilsk', 21), +('Boris', 'Kolyshkin', 'Polyarniy', 20), ('Konstantin', 'Lenin', 'Moscow', 25), ('Boris', 'Safonov', 'Severomorsk', 17), ('Ivan', 'Kolyshkin', 'Murmansk', 42); + diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..efa6f24 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,51 @@ +name: pg_and_pgadmin +services: + pg: + image: postgres:17.0-bookworm + container_name: postgres_new + environment: + POSTGRES_USER: kolchin + POSTGRES_PASSWORD: postgres + PGDATA: /var/lib/postgresql/data/pgdata + volumes: + - pg-data:/var/lib/postgresql/data + - ./db/:/docker-entrypoint-initdb.d + restart: unless-stopped + networks: + - pg-net + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 10s + retries: 3 + pgadmin: + image: dpage/pgadmin4:8.12.0 + container_name: pgadmin_new + environment: + PGADMIN_DEFAULT_EMAIL: user@example.com + PGADMIN_DEFAULT_PASSWORD: postgres + ports: + - 10501:80 + restart: unless-stopped + networks: + - pg-net + depends_on: + pg: + condition: service_healthy +# app: +# build: +# context: ./ +# args: +# PYTHON_VERSION: "3.10" +# UID: 10002 +# container_name: python-app +# networks: +# - pg-net +# depends_on: +# pg: +# condition: service_healthy + +volumes: + pg-data: +networks: + pg-net: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ad99b21 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +psycopg2 == 2.8