From e091f08e2cd8e59fd29f8df38242faf82f0c0a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Wed, 23 Mar 2022 01:53:52 +0100 Subject: [PATCH] Fix Dockerfile --- Dockerfile | 53 +++++++++++++++ .../docker-compose.yml => docker-compose.yml | 7 +- extras/.env.example | 8 --- extras/Dockerfile | 65 ------------------- 4 files changed, 57 insertions(+), 76 deletions(-) create mode 100644 Dockerfile rename extras/docker-compose.yml => docker-compose.yml (64%) delete mode 100644 extras/.env.example delete mode 100644 extras/Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b1c692c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +FROM python:3.10-slim + +# 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 + +# 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. +# 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 + +# 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 git ffmpeg python-is-python3 + +# Create user so we don't run as root. +RUN useradd --create-home botuser + +# Make log directory +RUN \ +mkdir -p /var/log/discord-embed/ && chown -R botuser:botuser /var/log/discord-embed/ && \ +mkdir /Uploads && chown -R botuser:botuser /Uploads + +VOLUME ["/var/log/discord-embed/", "/Uploads"] + +# Change to the user we created. +USER botuser + +# Install poetry. +RUN curl -sSL https://install.python-poetry.org | python - + +# Add poetry to our path. +ENV PATH="/home/botuser/.local/bin/:${PATH}" + +# Copy files from our repository to the container. +ADD --chown=botuser:botuser pyproject.toml poetry.lock README.md LICENSE /home/botuser/discord-embed/ + +# Change directory to where we will run the bot. +WORKDIR /home/botuser/discord-embed + +# Install the requirements. +RUN poetry install --no-interaction --no-ansi --no-dev && \ +poetry add uvicorn[standard] + +# Add main.py and settings.py to the container. +ADD --chown=botuser:botuser discord_embed /home/botuser/discord-embed/discord_embed/ + +EXPOSE 5000 + +CMD ["poetry", "run", "uvicorn", "discord_embed.main:app", "--host", "0.0.0.0", "--port", "5000"] diff --git a/extras/docker-compose.yml b/docker-compose.yml similarity index 64% rename from extras/docker-compose.yml rename to docker-compose.yml index 77767b0..19e495c 100644 --- a/extras/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,9 @@ version: "3" services: - discord-nice-embed-maker-for-my-yoy: + discord-embed: image: thelovinator/discord-nice-embed-maker-for-my-yoy - container_name: discord-nice-embed-maker-for-my-yoy + container_name: discord-embed + build: . env_file: - .env environment: @@ -10,7 +11,7 @@ services: ports: - "5000:5000" volumes: - - uploads:/home/botuser/Uploads + - uploads:/Uploads restart: unless-stopped volumes: uploads: diff --git a/extras/.env.example b/extras/.env.example deleted file mode 100644 index d0b380b..0000000 --- a/extras/.env.example +++ /dev/null @@ -1,8 +0,0 @@ -# Domain where we server files from, not where we upload files to -DOMAIN=https://i.example.com/ - -# Path to the directory where we store files -UPLOAD_FOLDER=/Uploads - -# Discord Webhook URL -WEBHOOK_URL=https://discordapp.com/api/webhooks/123456789/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \ No newline at end of file diff --git a/extras/Dockerfile b/extras/Dockerfile deleted file mode 100644 index 4bb41ee..0000000 --- a/extras/Dockerfile +++ /dev/null @@ -1,65 +0,0 @@ -# We need gcc, build-essential and git to install our requirements but we -# don't need them when run the application so we can selectively copy artifacts -# from this stage (compile-image) to second one (runtime-image), leaving -# behind everything we don't need in the final build. -FROM python:3.9-slim as requirements-stage - -WORKDIR /tmp - -RUN pip install poetry - -COPY ./pyproject.toml ./poetry.lock /tmp/ - -RUN poetry export -f requirements.txt --output requirements.txt --without-hashes - -FROM python:3.9-slim - -# TODO: Remove this -# 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 - -# Update packages and install needed packages to build our requirements. -RUN apt-get update && \ - apt-get install -y --no-install-recommends build-essential ffmpeg - -# Create user so we don't run as root. -RUN useradd --create-home botuser - -# Change ownership of directories -RUN chown -R botuser:botuser /home/botuser && chmod -R 755 /home/botuser - -# Change user -USER botuser - -# Change directory to where we will run the application. -WORKDIR /home/botuser - -ENV PATH "$PATH:/home/botuser/.local/bin" - -# Copy our Python application to our home directory. -COPY --from=requirements-stage /tmp/requirements.txt /home/botuser/requirements.txt - -RUN pip install --no-cache-dir --upgrade -r /home/botuser/requirements.txt - -COPY discord_embed/main.py discord_embed/settings.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. -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 application 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" - -# Expose the web port -EXPOSE 5000 - -# Run bot. -CMD ["uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "5000"]