Update Dockerfile: Use Poetry instead of Pip

Also:
Remove multi-stage (Need to be added in the future to minimize the image)
This commit is contained in:
2022-07-15 20:35:44 +02:00
parent 6de97dad73
commit 9b31f8e021
2 changed files with 36 additions and 60 deletions

View File

@ -109,6 +109,7 @@ venv/
ENV/ ENV/
env.bak/ env.bak/
venv.bak/ venv.bak/
.env.example
# Spyder project settings # Spyder project settings
.spyderproject .spyderproject
@ -144,12 +145,10 @@ tests/
# https://github.com/marketplace/renovate # https://github.com/marketplace/renovate
renovate.json renovate.json
# Other files not needed in the Docker image # Git files and directories
.git/ .git/
README.md .gitignore
pyproject.toml
poetry.lock # Docker files
LICENSE
docker-compose.yml docker-compose.yml
Dockerfile Dockerfile
.gitignore

View File

@ -1,71 +1,48 @@
# We have two stages, one for making the virtual environment and FROM python:3.10-slim
# installing the dependencies and another for running the uvicorn server. # TODO: Do the Poetry stuff in its own stage
# We use an virtual environment so we seperate the dependencies from the # TODO: Add support for logging
# system-level dependencies. # TODO: Add health check
FROM python:3.10-slim AS build-image # TODO: Add support for changing uid/gid
# TODO: Add support for changing host and port
# Create virtual environment in /opt/venv, we will use this in the other # We don't want apt-get to interact with us and we want the default answers to be used for all questions.
# stage. ARG DEBIAN_FRONTEND=noninteractive
RUN python -m venv /opt/venv
# Make sure we use the virtualenv. # Force the stdout and stderr streams to be unbuffered.
ENV PATH="/opt/venv/bin:$PATH" # 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
# Copy requirements.txt to the container, it was generated with # Update the system and install curl, it is needed for downloading Poetry.
# 'poetry export -f requirements.txt --without-hashes' RUN apt-get update && apt-get install curl ffmpeg -y --no-install-recommends
# Note that if we pipe the output of the commmand to requirements.txt in
# Windows, it will be UTF-16 LE encoded.
COPY requirements.txt .
# Install the requirements. # 1. Create user so we don't run as root
RUN pip install -r requirements.txt # 2. Create directories that the bot needs that are owned by the user.
# /Uploads is used to store the uploaded files.
# This is the stage where we will run the uvicorn server. # /home/botuser/discord-embed is where the Python code is stored.
FROM python:3.10-slim AS run-image
# Copy the virtual environment to the run-image.
COPY --from=build-image /opt/venv /opt/venv
# Install ffmpeg, it is needed for finding the video resolution and
# making the video thumbnail.
RUN apt-get update
RUN apt-get install -y --no-install-recommends ffmpeg
# Create user so we don't run as root and make the needed directories
# Logs are stored in /var/log/discord-embed and uploaded files,
# thumbnails, and HTML are stored in /Uploads.
RUN useradd --create-home botuser && \ RUN useradd --create-home botuser && \
install --verbose --directory --mode=0755 --owner=botuser --group=botuser /var/log/discord-embed/ /Uploads install --verbose --directory --mode=0775 --owner=botuser --group=botuser /Uploads /home/botuser/discord-embed
# Persist the uploaded files and files we have created. # Change to the user we created.
VOLUME ["/Uploads"]
# Change to the user we created so we don't run as root.
USER botuser USER botuser
# Change directory to where we will run the bot. # Change directory to where we will run the bot.
WORKDIR /home/botuser/discord-embed WORKDIR /home/botuser/discord-embed
# Add main.py and settings.py to the container. # Add needed files to the container, files and directories not needed are ignored in .dockerignore.
ADD --chown=botuser:botuser . /home/botuser/discord-embed/ ADD --chown=botuser:botuser . /home/botuser/discord-embed/
# Uvicorn runs on port 5000, we can't use any ports below 1024 due to # 1. Install Poetry.
# not being root. # 2. Add Poetry to the PATH.
EXPOSE 5000 # 3. Install dependencies.
ENV PATH="/home/botuser/.local/bin/:$PATH"
RUN curl -sSL https://install.python-poetry.org | python -
RUN poetry install --no-interaction --no-ansi --no-dev
# Make sure we use the virtualenv. # Persist the uploaded files and files we have created.
ENV PATH="/opt/venv/bin:$PATH" VOLUME ["/Uploads"]
# 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
# Force the stdout and stderr streams to be unbuffered. This means that
# the output will be printed immediately instead of being buffered. If
# the Python application crashes, the output could be lost.
ENV PYTHONUNBUFFERED 1
# Run the server on all interfaces and on port 5000. # Run the server on all interfaces and on port 5000.
# You should run a reverse proxy like nginx infront of this. # You should run a reverse proxy like nginx infront of this.
CMD ["uvicorn", "discord_embed.main:app", "--host", "0.0.0.0", "--port", "5000"] EXPOSE 5000
CMD ["poetry", "run", "uvicorn", "discord_embed.main:app", "--host", "0.0.0.0", "--port", "5000"]