Update Dockerfile to use Python 3.13-slim and streamline dependency installation
This commit is contained in:
72
Dockerfile
72
Dockerfile
@ -1,64 +1,14 @@
|
|||||||
# Stage 1: Build the requirements.txt using Poetry.
|
FROM python:3.13-slim
|
||||||
FROM python:3.12 AS builder
|
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
|
||||||
|
RUN useradd --create-home botuser && \
|
||||||
# Set environment variables for Python.
|
mkdir -p /home/botuser/discord-rss-bot/ /home/botuser/.local/share/discord_rss_bot/ && \
|
||||||
ENV PYTHONDONTWRITEBYTECODE 1
|
chown -R botuser:botuser /home/botuser/
|
||||||
ENV PYTHONUNBUFFERED 1
|
|
||||||
ENV PATH="${PATH}:/root/.local/bin"
|
|
||||||
|
|
||||||
# Install system dependencies.
|
|
||||||
RUN apt-get update && \
|
|
||||||
apt-get install -y --no-install-recommends \
|
|
||||||
curl \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Install Poetry.
|
|
||||||
RUN curl -sSL https://install.python-poetry.org | python3 -
|
|
||||||
|
|
||||||
# Copy only the poetry.lock/pyproject.toml to leverage Docker cache.
|
|
||||||
WORKDIR /app
|
|
||||||
COPY pyproject.toml poetry.lock /app/
|
|
||||||
|
|
||||||
# Install dependencies and create requirements.txt.
|
|
||||||
RUN poetry self add poetry-plugin-export && poetry export --format=requirements.txt --output=requirements.txt --only=main --without-hashes
|
|
||||||
|
|
||||||
# Stage 2: Install dependencies and run the application
|
|
||||||
FROM python:3.12 AS runner
|
|
||||||
|
|
||||||
# Set environment variables for Python.
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE 1
|
|
||||||
ENV PYTHONUNBUFFERED 1
|
|
||||||
|
|
||||||
# Create a non-root user.
|
|
||||||
RUN useradd -ms /bin/bash botuser && \
|
|
||||||
install --verbose --directory --mode=0775 --owner=botuser --group=botuser /home/botuser/discord-rss-bot/ && \
|
|
||||||
install --verbose --directory --mode=0775 --owner=botuser --group=botuser /home/botuser/.local/share/discord_rss_bot/
|
|
||||||
|
|
||||||
# Copy the generated requirements.txt from the builder stage.
|
|
||||||
WORKDIR /home/botuser/discord-rss-bot
|
|
||||||
COPY --from=builder /app/requirements.txt /home/botuser/discord-rss-bot/
|
|
||||||
|
|
||||||
# Create a virtual environment and install dependencies.
|
|
||||||
RUN python -m venv /home/botuser/.venv && \
|
|
||||||
. /home/botuser/.venv/bin/activate && \
|
|
||||||
pip install --no-cache-dir --upgrade pip && \
|
|
||||||
pip install --no-cache-dir --upgrade setuptools wheel && \
|
|
||||||
pip install --no-cache-dir --requirement requirements.txt
|
|
||||||
|
|
||||||
# Copy the rest of the application code.
|
|
||||||
COPY . /home/botuser/discord-rss-bot/
|
|
||||||
|
|
||||||
# Change to the bot user so we don't run as root.
|
|
||||||
USER botuser
|
USER botuser
|
||||||
|
WORKDIR /home/botuser/discord-rss-bot
|
||||||
# The uvicorn server will listen on this port.
|
COPY --chown=botuser:botuser requirements.txt /home/botuser/discord-rss-bot/
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
||||||
|
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
|
||||||
|
uv sync --no-install-project
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
|
||||||
# Where our database file will be stored.
|
|
||||||
VOLUME /home/botuser/.local/share/discord_rss_bot/
|
VOLUME /home/botuser/.local/share/discord_rss_bot/
|
||||||
|
CMD ["uv", "run", "uvicorn", "discord_rss_bot.main:app", "--host=0.0.0.0", "--port=5000", "--proxy-headers", "--forwarded-allow-ips='*'", "--log-level", "debug"]
|
||||||
# Print the folder structure and wait so we can inspect the container.
|
|
||||||
# CMD ["tail", "-f", "/dev/null"]
|
|
||||||
|
|
||||||
# Run the application.
|
|
||||||
CMD ["/home/botuser/.venv/bin/python", "-m", "uvicorn", "discord_rss_bot.main:app", "--host=0.0.0.0", "--port=5000", "--proxy-headers", "--forwarded-allow-ips='*'", "--log-level", "debug"]
|
|
||||||
|
Reference in New Issue
Block a user