From 56638f58786cf027edc7167fcb3294dede1173cd Mon Sep 17 00:00:00 2001 From: Egor Deev Date: Fri, 2 Oct 2026 19:25:42 +0000 Subject: [PATCH] =?UTF-8?q?Docker:=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B7=20?= =?UTF-8?q?=D0=BD=D0=B0=20python:3.12-slim,=20compose=20=D1=81=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BC=D0=BE=D0=BC=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D0=B0?= =?UTF-8?q?=D0=B7=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Лог в файл стал необязательным (ALERTBOT_LOG_FILE): в контейнере пишем только в stdout. --- .dockerignore | 8 ++++++++ Dockerfile | 17 +++++++++++++++++ bot.py | 8 ++++++-- compose.yaml | 14 ++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c368c0f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git +.github +**/__pycache__ +.env +tests +*.md +*.db +*.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..86f9ac3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + ALERTBOT_LOG_FILE= \ + PYTHONUNBUFFERED=1 \ + ALERTBOT_DB=/data/notifications.db + +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY *.py ./ +RUN useradd --create-home --uid 1000 app && mkdir -p /data && chown -R app:app /app /data +USER app +VOLUME ["/data"] + +CMD ["python", "bot.py"] diff --git a/bot.py b/bot.py index a0e6997..2c38dcb 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,6 @@ import asyncio import logging +import os import sys from aiogram.types import BotCommand @@ -7,11 +8,14 @@ from aiogram.types import BotCommand from init import bot, dp from handlers import router, alert_poller +_log_handlers = [logging.StreamHandler(sys.stdout)] +# File log is optional: in Docker set ALERTBOT_LOG_FILE= (empty) and read `docker logs` +if os.environ.get("ALERTBOT_LOG_FILE", "bot.log"): + _log_handlers.append(logging.FileHandler(os.environ.get("ALERTBOT_LOG_FILE", "bot.log"), encoding="utf-8")) logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - handlers=[logging.StreamHandler(sys.stdout), - logging.FileHandler('bot.log', encoding='utf-8')], + handlers=_log_handlers, ) log = logging.getLogger(__name__) diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..126a2d7 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,14 @@ +services: + bot: + build: . + image: ghcr.io/edeev/alertbot:latest + env_file: .env + environment: + ALERTBOT_DB: /data/notifications.db + ALERTBOT_LOG_FILE: "" + volumes: + - data:/data + restart: unless-stopped + +volumes: + data: