Update Dockerfile and add comments in pyproject.toml

This commit is contained in:
2025-01-23 18:39:47 +01:00
parent 62b200d131
commit bf5c08346e
4 changed files with 51 additions and 49 deletions

View File

@ -5,6 +5,7 @@
"asctime", "asctime",
"asyncio", "asyncio",
"audioop", "audioop",
"botuser",
"cookiejar", "cookiejar",
"dateparser", "dateparser",
"delenv", "delenv",
@ -19,8 +20,12 @@
"Lovinator", "Lovinator",
"pycodestyle", "pycodestyle",
"pydocstyle", "pydocstyle",
"pyproject",
"PYTHONDONTWRITEBYTECODE",
"PYTHONUNBUFFERED",
"pyupgrade", "pyupgrade",
"sqlalchemy", "sqlalchemy",
"thelovinator",
"uvloop" "uvloop"
], ],
"python.analysis.typeCheckingMode": "standard" "python.analysis.typeCheckingMode": "standard"

View File

@ -1,48 +1,13 @@
FROM python:3.12-slim as base FROM python:3.13-slim
ENV PYTHONUNBUFFERED=1
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV POETRY_VIRTUALENVS_IN_PROJECT=1
ENV POETRY_NO_INTERACTION=1
ENV POETRY_HOME=/opt/poetry
ENV PYSETUP_PATH=/opt/pysetup
ENV VENV_PATH="/opt/pysetup/.venv"
ENV PIP_NO_CACHE_DIR=off
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
ENV PIP_DEFAULT_TIMEOUT=100
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONFAULTHANDLER=1 COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" RUN useradd -m botuser && mkdir -p /home/botuser/data
ENV PYTHONPATH="${PYTHONPATH}:/discord_reminder_bot"
FROM base as python-deps
RUN apt-get update && \
apt-get install -y --no-install-recommends curl build-essential
RUN --mount=type=cache,target=/root/.cache \
curl -sSL https://install.python-poetry.org | python3 -
WORKDIR $PYSETUP_PATH
COPY pyproject.toml ./
RUN --mount=type=cache,target=/root/.cache \
poetry install --only main
FROM base as runtime
# Copy virtual env from python-deps stage
COPY --from=python-deps $PYSETUP_PATH $PYSETUP_PATH
# Create directory for the sqlite database.
RUN mkdir -p /home/botuser/data
# Copy source code
COPY discord_reminder_bot /home/botuser/discord_reminder_bot/
WORKDIR /home/botuser WORKDIR /home/botuser
COPY interactions /home/botuser/interactions
COPY discord_reminder_bot /home/botuser/discord_reminder_bot
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --no-install-project
VOLUME ["/home/botuser/data/"] VOLUME ["/home/botuser/data/"]
CMD ["uv", "run", "discord_reminder_bot/main.py"]
# Run bot.
CMD [ "python", "/home/botuser/discord_reminder_bot/main.py" ]

View File

@ -9,9 +9,11 @@ description = "Discord bot that allows you to set date, cron and interval remind
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"
dependencies = [ dependencies = [
# The Discord bot library, and legacy-cgi is because Python 3.13 removed cgi module # The Discord bot library uses discord.py
"discord-py[speed]>=2.4.0,<3.0.0", # https://github.com/Rapptz/discord.py # legacy-cgi and audioop-lts are because Python 3.13 removed cgi module and audioop module
"legacy-cgi>=2.6.2,<3.0.0; python_version >= '3.13'", # https://github.com/jackrosenthal/legacy-cgi "discord-py[speed]>=2.4.0,<3.0.0", # https://github.com/Rapptz/discord.py
"legacy-cgi>=2.6.2,<3.0.0; python_version >= '3.13'", # https://github.com/jackrosenthal/legacy-cgi
"audioop-lts>=0.2.1,<1.0.0; python_version >= '3.13'", # https://github.com/AbstractUmbra/audioop
# For parsing dates and times in /remind commands # For parsing dates and times in /remind commands
"dateparser>=1.0.0", # https://github.com/scrapinghub/dateparser "dateparser>=1.0.0", # https://github.com/scrapinghub/dateparser
@ -55,9 +57,12 @@ dateparser = {version = ">=1.0.0"}
# https://github.com/Rapptz/discord.py # https://github.com/Rapptz/discord.py
# https://github.com/jackrosenthal/legacy-cgi # https://github.com/jackrosenthal/legacy-cgi
# The Discord bot library, and legacy-cgi is because Python 3.13 removed cgi module # https://github.com/AbstractUmbra/audioop
# The Discord bot library uses discord.py
# legacy-cgi and audioop-lts are because Python 3.13 removed cgi module and audioop module
discord-py = {version = ">=2.4.0,<3.0.0", extras = ["speed"]} discord-py = {version = ">=2.4.0,<3.0.0", extras = ["speed"]}
legacy-cgi = {version = ">=2.6.2,<3.0.0", markers = "python_version >= '3.13'"} legacy-cgi = {version = ">=2.6.2,<3.0.0", markers = "python_version >= '3.13'"}
audioop-lts = {version = ">=0.2.1,<1.0.0", markers = "python_version >= '3.13'"}
# https://github.com/lovvskillz/python-discord-webhook # https://github.com/lovvskillz/python-discord-webhook
# For sending webhook messages to Discord # For sending webhook messages to Discord

27
tests/test_dockerfile.py Normal file
View File

@ -0,0 +1,27 @@
from __future__ import annotations
from pathlib import Path
import pytest
@pytest.fixture
def dockerfile_content() -> str:
"""Read the content of the Dockerfile and return it as a string.
Returns:
str: The content of the Dockerfile.
"""
return Path("Dockerfile").read_text(encoding="utf-8")
def test_volume_exists(dockerfile_content: str) -> None:
"""Test that the volume is set in the Dockerfile."""
assert_msg = "Volume not set in Dockerfile. This has to always be set to /home/botuser/data/."
assert 'VOLUME ["/home/botuser/data/"]' in dockerfile_content, assert_msg
def test_env_variables(dockerfile_content: str) -> None:
"""Test that the environment variables for Python are set in the Dockerfile."""
assert "ENV PYTHONUNBUFFERED=1" in dockerfile_content, "PYTHONUNBUFFERED not set in Dockerfile"
assert "ENV PYTHONDONTWRITEBYTECODE=1" in dockerfile_content, "PYTHONDONTWRITEBYTECODE not set in Dockerfile"