From f55d51139b9d4acbc0f5651e4f695906b94b74a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Mon, 26 Jul 2021 21:08:43 +0200 Subject: [PATCH] Update Dockerfile --- Dockerfile | 69 +++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/Dockerfile b/Dockerfile index 99b2c09..7ec9175 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,44 +6,8 @@ FROM python:3.9-slim AS compile-image # We don't want apt-get to interact with us, # and we want the default answers to be used for all questions. -# Is it also completely silent and unobtrusive. ARG DEBIAN_FRONTEND=noninteractive -# Update packages and install needed packages to build our requirements. -RUN apt-get update && \ - apt-get install -y --no-install-recommends build-essential gcc - -# Create new virtual environment in /opt/venv and change to it. -ENV VIRTUAL_ENV=/opt/venv -RUN python3 -m venv $VIRTUAL_ENV -ENV PATH="$VIRTUAL_ENV/bin:$PATH" - -# Copy and install requirements. -COPY requirements.txt . -RUN pip install --disable-pip-version-check --no-cache-dir --requirement requirements.txt - -# Change to our second stage. This is the one that will run the bot. -FROM python:3.9-slim AS runtime-image - -# Copy Python dependencies from our build image. -COPY --from=compile-image /opt/venv /opt/venv - -# 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 - -# Change directory to where we will run the bot. -WORKDIR /home/botuser - -# 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 - -# Copy our Python bot to our home directory. -COPY main.py . - # 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. @@ -52,11 +16,36 @@ ENV PYTHONDONTWRITEBYTECODE 1 # 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. -# Has a minor performance loss. We don't have many log messages so probably makes zero difference. ENV PYTHONUNBUFFERED 1 -# Use our virtual environment that we created in the other stage. -ENV PATH="/opt/venv/bin:$PATH" +# Update packages and install needed packages to build our requirements. +RUN apt-get update && \ + apt-get install -y --no-install-recommends build-essential gcc git curl + +# 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 + +# 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://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - + +# Add poetry to our path +ENV PATH="/home/botuser/.poetry/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 --no-dev + +COPY discord_reminder_bot/main.py discord_reminder_bot/settings.py /home/botuser/discord_reminder_bot/ # Run bot. -CMD [ "python", "./main.py" ] \ No newline at end of file +CMD [ "poetry", "run", "bot" ] \ No newline at end of file