Rewrite Dockerfile

This commit is contained in:
2023-03-20 03:58:17 +01:00
parent 39ecbd31a9
commit 8213b365bb

View File

@ -1,48 +1,48 @@
FROM python:3.11-slim FROM python:3.11-slim as base
# We don't want apt-get to interact with us, ENV LANG C.UTF-8
# and we want the default answers to be used for all questions. ENV LC_ALL C.UTF-8
ARG DEBIAN_FRONTEND=noninteractive 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 PYTHONFAULTHANDLER=1
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
ENV PYTHONPATH="${PYTHONPATH}:/discord_reminder_bot"
# Don't generate byte code (.pyc-files). FROM base as python-deps
# These are only needed if we run the python-files several times.
# Docker doesn't keep the data between runs so this adds nothing.
ENV PYTHONDONTWRITEBYTECODE 1
# Force the stdout and stderr streams to be unbuffered. RUN apt-get update && \
# Will allow log messages to be immediately dumped instead of being buffered. apt-get install -y --no-install-recommends curl build-essential
# This is useful when the bot crashes before writing messages stuck in the buffer.
ENV PYTHONUNBUFFERED 1
# Update packages and install needed packages to build our requirements. RUN --mount=type=cache,target=/root/.cache \
RUN apt-get update && apt-get install -y --no-install-recommends build-essential gcc curl curl -sSL https://install.python-poetry.org | python3 -
# Create user so we don't run as root. WORKDIR $PYSETUP_PATH
RUN useradd --create-home botuser COPY pyproject.toml ./
RUN chown -R botuser:botuser /home/botuser && chmod -R 755 /home/botuser
USER botuser 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. # Create directory for the sqlite database.
# You need to share this directory with the computer running
# the container to save the data.
RUN mkdir -p /home/botuser/data RUN mkdir -p /home/botuser/data
# Install poetry # Copy source code
RUN curl -sSL https://install.python-poetry.org | python3 -
# Add poetry to our path
ENV PATH="/home/botuser/.local/bin/:${PATH}"
COPY pyproject.toml poetry.lock README.md /home/botuser/
# Change directory to where we will run the bot.
WORKDIR /home/botuser
RUN poetry install --no-interaction --no-ansi --only main
COPY discord_reminder_bot /home/botuser/discord_reminder_bot/ COPY discord_reminder_bot /home/botuser/discord_reminder_bot/
WORKDIR /home/botuser
VOLUME ["/home/botuser/data/"] VOLUME ["/home/botuser/data/"]
# Run bot. # Run bot.
CMD [ "poetry", "run", "bot" ] CMD [ "python", "/home/botuser/discord_reminder_bot/main.py" ]