demo for compose

This commit is contained in:
2025-05-05 13:44:59 +03:00
parent c3e28c48d0
commit e1e2593fa5
5 changed files with 86 additions and 0 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
.idea
postgres_data
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

28
docker-compose.yaml Normal file
View File

@@ -0,0 +1,28 @@
services:
db:
image: postgres:15-alpine
env_file:
- ./.env
ports:
- "5432:5432"
volumes:
- ./postgres_data:/var/lib/postgresql/data
container_name: postgres_db
restart: always
healthcheck:
test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s
web:
ports:
- "80:8086"
build:
context: ./src
env_file:
- ./.env
depends_on:
db:
condition: service_healthy
restart: always

8
src/Dockerfile Normal file
View File

@@ -0,0 +1,8 @@
FROM python:3.12
WORKDIR /app
COPY main.py .
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python3", "main.py"]

48
src/main.py Normal file
View File

@@ -0,0 +1,48 @@
import os
import psycopg2
from flask import Flask
# Подключение к базе данных postgres
conn = psycopg2.connect(
dbname=os.getenv("POSTGRES_DB"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"),
host="db",
port="5432"
)
app = Flask(__name__)
@app.route("/")
def hello_world():
return """<p>Hello, World!</p><br><a href="/db">ТЫК</a>"""
@app.route("/db")
def db():
cur = conn.cursor()
cur.execute('''UPDATE mytable SET visitors_count = visitors_count + 1''')
conn.commit()
cur.execute("SELECT visitors_count from mytable")
result = cur.fetchone()
value = -1
if result:
value = result[0]
return f"""DB:{value}<br><a href="/">Обратно</a>"""
if __name__ == "__main__":
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS mytable (
id SERIAL PRIMARY KEY,
visitors_count INTEGER NOT NULL DEFAULT 0)''')
cur.execute('''INSERT INTO mytable (id, visitors_count)
VALUES (1, 0)
ON CONFLICT (id) DO NOTHING''')
conn.commit()
app.run(host="0.0.0.0", port=8086)

BIN
src/requirements.txt Normal file

Binary file not shown.