From 8213b365bb858c0d75daa8cf2316fe904922c7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Mon, 20 Mar 2023 03:58:17 +0100 Subject: [PATCH] Rewrite Dockerfile --- Dockerfile | 68 +++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Dockerfile b/Dockerfile index a1b76b3..653a526 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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, -# and we want the default answers to be used for all questions. -ARG DEBIAN_FRONTEND=noninteractive +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 PYTHONFAULTHANDLER=1 +ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" +ENV PYTHONPATH="${PYTHONPATH}:/discord_reminder_bot" -# Don't generate byte code (.pyc-files). -# 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 +FROM base as python-deps -# Force the stdout and stderr streams to be unbuffered. -# Will allow log messages to be immediately dumped instead of being buffered. -# This is useful when the bot crashes before writing messages stuck in the buffer. -ENV PYTHONUNBUFFERED 1 +RUN apt-get update && \ + apt-get install -y --no-install-recommends curl build-essential -# Update packages and install needed packages to build our requirements. -RUN apt-get update && apt-get install -y --no-install-recommends build-essential gcc curl +RUN --mount=type=cache,target=/root/.cache \ + curl -sSL https://install.python-poetry.org | python3 - -# Create user so we don't run as root. -RUN useradd --create-home botuser -RUN chown -R botuser:botuser /home/botuser && chmod -R 755 /home/botuser -USER botuser +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. -# You need to share this directory with the computer running -# the container to save the data. RUN mkdir -p /home/botuser/data -# Install poetry -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 source code COPY discord_reminder_bot /home/botuser/discord_reminder_bot/ +WORKDIR /home/botuser + VOLUME ["/home/botuser/data/"] # Run bot. -CMD [ "poetry", "run", "bot" ] +CMD [ "python", "/home/botuser/discord_reminder_bot/main.py" ]