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: