From 539276d27ee15b5cb7b6885d6ceafeb92d12cd93 Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Sat, 24 Sep 2022 01:31:32 +0800 Subject: [PATCH] update docker file --- docker/Dockerfile_with_frontend | 37 ++++++++++++++++++---- docker/Dockerfile_with_frontend_sentry | 44 ++++++++++++++++++++------ 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/docker/Dockerfile_with_frontend b/docker/Dockerfile_with_frontend index 1002229..00793f3 100644 --- a/docker/Dockerfile_with_frontend +++ b/docker/Dockerfile_with_frontend @@ -1,8 +1,33 @@ # syntax=docker/dockerfile:1.2 -FROM python:3.9 as base +FROM python:3.9-slim as base + +FROM base as builder + +ENV PYTHONFAULTHANDLER=1 \ + PYTHONUNBUFFERED=1 \ + PYTHONHASHSEED=random \ + PIP_NO_CACHE_DIR=off \ + PIP_DISABLE_PIP_VERSION_CHECK=on \ + PIP_DEFAULT_TIMEOUT=100 \ + POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_CREATE=false \ + PATH="$PATH:/runtime/bin" \ + PYTHONPATH="$PYTHONPATH:/runtime/lib/python3.9/site-packages" \ + # Versions: + POETRY_VERSION=1.1.14 +RUN apt-get update && apt-get install -y build-essential unzip wget python-dev git +RUN pip install "poetry==$POETRY_VERSION" + +WORKDIR /src + +COPY pyproject.toml poetry.lock /src/ + +RUN poetry export --without-hashes --no-interaction --no-ansi -f requirements.txt -o requirements.txt +RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt + +FROM base as runtime + WORKDIR /app -RUN --mount=type=cache,target=/root/.cache/pip \ - python3 -m pip install poetry && poetry config virtualenvs.create false RUN --mount=type=cache,target=/var/cache/apt \ --mount=type=cache,target=/var/lib/apt \ apt-get update && apt-get install -y xvfb fonts-noto-color-emoji ttf-unifont \ @@ -12,12 +37,12 @@ RUN --mount=type=cache,target=/var/cache/apt \ libcairo2 libcups2 libdbus-1-3 libdrm2 libegl1 libgbm1 libglib2.0-0 libgtk-3-0 \ libnspr4 libnss3 libpango-1.0-0 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \ libxdamage1 libxext6 libxfixes3 libxrandr2 libxshmfence1 -COPY ./pyproject.toml ./poetry.lock* /app/ -RUN --mount=type=cache,target=/root/.cache/pypoetry \ - poetry install --no-dev --no-root + +COPY --from=builder /runtime /usr/local RUN playwright install chromium ADD src /app/src ADD bot.py /app/ +RUN echo 'DATASTORE_DATA_DIR=/data' > .env ENV HOST=0.0.0.0 CMD ["python", "bot.py"] diff --git a/docker/Dockerfile_with_frontend_sentry b/docker/Dockerfile_with_frontend_sentry index 927d81b..551629d 100644 --- a/docker/Dockerfile_with_frontend_sentry +++ b/docker/Dockerfile_with_frontend_sentry @@ -1,8 +1,35 @@ # syntax=docker/dockerfile:1.2 -FROM python:3.9 as base +FROM python:3.9-slim as base + +FROM python:3.9 as builder + +ENV PYTHONFAULTHANDLER=1 \ + PYTHONUNBUFFERED=1 \ + PYTHONHASHSEED=random \ + PIP_NO_CACHE_DIR=off \ + PIP_DISABLE_PIP_VERSION_CHECK=on \ + PIP_DEFAULT_TIMEOUT=100 \ + POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_CREATE=false \ + PATH="$PATH:/runtime/bin" \ + PYTHONPATH="$PYTHONPATH:/runtime/lib/python3.9/site-packages" \ + # Versions: + POETRY_VERSION=1.1.14 +RUN apt-get update && apt-get install -y build-essential unzip wget python-dev git +RUN pip install "poetry==$POETRY_VERSION" + +WORKDIR /src + +COPY pyproject.toml poetry.lock bot.py /src/ + +RUN poetry add nonebot-plugin-sentry +RUN sed '/nonebot.load_builtin_plugins("echo")/a nonebot.load_plugin("nonebot_plugin_sentry")' -i bot.py +RUN poetry export --without-hashes --no-interaction --no-ansi -f requirements.txt -o requirements.txt +RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt + +FROM base as runtime + WORKDIR /app -RUN --mount=type=cache,target=/root/.cache/pip \ - python3 -m pip install poetry && poetry config virtualenvs.create false RUN --mount=type=cache,target=/var/cache/apt \ --mount=type=cache,target=/var/lib/apt \ apt-get update && apt-get install -y xvfb fonts-noto-color-emoji ttf-unifont \ @@ -12,15 +39,12 @@ RUN --mount=type=cache,target=/var/cache/apt \ libcairo2 libcups2 libdbus-1-3 libdrm2 libegl1 libgbm1 libglib2.0-0 libgtk-3-0 \ libnspr4 libnss3 libpango-1.0-0 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \ libxdamage1 libxext6 libxfixes3 libxrandr2 libxshmfence1 -COPY ./pyproject.toml ./poetry.lock* ./bot.py /app/ -RUN --mount=type=cache,target=/root/.cache/pypoetry \ - poetry add nonebot-plugin-sentry && \ - sed '/nonebot.load_builtin_plugins("echo")/a nonebot.load_plugin("nonebot_plugin_sentry")' -i bot.py -RUN --mount=type=cache,target=/root/.cache/pypoetry \ - poetry install --no-dev --no-root + +COPY --from=builder /runtime /usr/local RUN playwright install chromium ADD src /app/src +COPY --from=builder /src/bot.py /app/ +RUN echo 'DATASTORE_DATA_DIR=/data' > .env ENV HOST=0.0.0.0 CMD ["python", "bot.py"] - # vim: set ft=dockerfile: