From 00c93599ee1f7b5aaaad9804a9caea9f56aa9bab Mon Sep 17 00:00:00 2001 From: DDSRem <1448139087@qq.com> Date: Wed, 26 Apr 2023 20:12:46 +0800 Subject: [PATCH] feat: Rewrite docker --- Dockerfile | 80 ++++++++++++------- docker/etc/cont-init.d/010-old-compatible | 14 ++++ docker/etc/cont-init.d/020-fixuser | 13 +++ docker/etc/services.d/auto_bangumi/finish | 4 + .../services.d/auto_bangumi/notification-fd | 1 + docker/etc/services.d/auto_bangumi/run | 16 ++++ src/run.sh | 10 --- 7 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 docker/etc/cont-init.d/010-old-compatible create mode 100644 docker/etc/cont-init.d/020-fixuser create mode 100644 docker/etc/services.d/auto_bangumi/finish create mode 100644 docker/etc/services.d/auto_bangumi/notification-fd create mode 100644 docker/etc/services.d/auto_bangumi/run delete mode 100755 src/run.sh diff --git a/Dockerfile b/Dockerfile index 007948ed..cc750ece 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,41 +1,65 @@ # syntax=docker/dockerfile:1 -FROM python:3.11-alpine -ENV TZ=Asia/Shanghai \ +FROM python:3.11-alpine AS Builder +WORKDIR /app +COPY requirements.txt . +RUN apk add --no-cache --virtual=build-dependencies \ + libxml2-dev \ + libxslt-dev \ + gcc \ + g++ \ + linux-headers \ + build-base && \ + python3 -m pip install --upgrade pip && \ + pip install cython && \ + pip install --no-cache-dir -r requirements.txt && \ + apk del --purge \ + build-dependencies && \ + rm -rf \ + /root/.cache \ + /tmp/* + +FROM scratch AS APP + +ENV S6_SERVICES_GRACETIME=30000 \ + S6_KILL_GRACETIME=60000 \ + S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 \ + S6_SYNC_DISKS=1 \ + TERM="xterm" \ + HOME="/ab" \ + LANG="C.UTF-8" \ + TZ=Asia/Shanghai \ PUID=1000 \ PGID=1000 \ UMASK=022 +COPY --from=Builder / / + WORKDIR /app -COPY requirements.txt . - -RUN python3 -m pip install --upgrade pip \ - && pip install -r requirements.txt --no-cache-dir +RUN apk add --no-cache \ + curl \ + wget \ + jq \ + shadow \ + s6-overlay \ + bash && \ + # Download WebUI + wget "https://github.com/Rewrite0/Auto_Bangumi_WebUI/releases/latest/download/dist.zip" -O /tmp/dist.zip && \ + unzip -q -d /tmp /tmp/dist.zip && \ + mv /tmp/dist /app/templates && \ + # Add user + mkdir /ab && \ + addgroup -S ab -g 911 && \ + adduser -S ab -G ab -h /ab -s /bin/bash -u 911 && \ + rm -rf \ + /root/.cache \ + /tmp/* COPY --chmod=755 src/. . +COPY --chmod=755 ./docker / -RUN apk add --no-cache \ - curl \ - shadow \ - su-exec \ - bash - -# Download WebUI -RUN wget "https://github.com/Rewrite0/Auto_Bangumi_WebUI/releases/download/$(curl 'https://api.github.com/repos/Rewrite0/Auto_Bangumi_WebUI/releases/latest' | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')/dist.zip" && \ - unzip dist.zip && \ - mv dist templates - -RUN addgroup -S auto_bangumi -g 1000 && \ - adduser -S auto_bangumi -G auto_bangumi -h /home/auto_bangumi -u 1000 && \ - usermod -s /bin/bash auto_bangumi && \ - mkdir -p "config" && \ - mkdir -p "data" && \ - chmod a+x \ - run.sh +ENTRYPOINT [ "/init" ] EXPOSE 7892 - -VOLUME [ "/app/config" , "/app/data"] - -CMD ["sh", "run.sh"] +VOLUME [ "/app/config" , "/app/data" ] diff --git a/docker/etc/cont-init.d/010-old-compatible b/docker/etc/cont-init.d/010-old-compatible new file mode 100644 index 00000000..f6136488 --- /dev/null +++ b/docker/etc/cont-init.d/010-old-compatible @@ -0,0 +1,14 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +function __old_compatible { + + umask ${UMASK} + + if [ -f /config/bangumi.json ]; then + mv /config/bangumi.json /app/data/bangumi.json + fi + +} + +__old_compatible 2>&1 | sed "s#^#cont-init: info: $(realpath $0): &#g" \ No newline at end of file diff --git a/docker/etc/cont-init.d/020-fixuser b/docker/etc/cont-init.d/020-fixuser new file mode 100644 index 00000000..51856377 --- /dev/null +++ b/docker/etc/cont-init.d/020-fixuser @@ -0,0 +1,13 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +function __fixuser { + + groupmod -o -g "${PGID}" ab + usermod -o -u "${PUID}" ab + + chown ab:ab -R /app + +} + +__fixuser 2>&1 | sed "s#^#cont-init: info: $(realpath $0): &#g" \ No newline at end of file diff --git a/docker/etc/services.d/auto_bangumi/finish b/docker/etc/services.d/auto_bangumi/finish new file mode 100644 index 00000000..b1ccd97a --- /dev/null +++ b/docker/etc/services.d/auto_bangumi/finish @@ -0,0 +1,4 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +pkill -f 'python3 main.py' \ No newline at end of file diff --git a/docker/etc/services.d/auto_bangumi/notification-fd b/docker/etc/services.d/auto_bangumi/notification-fd new file mode 100644 index 00000000..e440e5c8 --- /dev/null +++ b/docker/etc/services.d/auto_bangumi/notification-fd @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/docker/etc/services.d/auto_bangumi/run b/docker/etc/services.d/auto_bangumi/run new file mode 100644 index 00000000..ea0d1347 --- /dev/null +++ b/docker/etc/services.d/auto_bangumi/run @@ -0,0 +1,16 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +umask ${UMASK} + +if [ -f /app/config/config.json ]; then + AB_PORT=$(jq '.program.webui_port' /app/config/config.json) +elif [ -f /app/config/config_dev.json ]; then + AB_PORT=$(jq '.program.webui_port' /app/config/config_dev.json) +else + AB_PORT=7892 +fi + +exec \ + s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z localhost ${AB_PORT}" \ + cd /app s6-setuidgid ab python3 main.py \ No newline at end of file diff --git a/src/run.sh b/src/run.sh deleted file mode 100755 index bf18b90f..00000000 --- a/src/run.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -# Check old version -if [ -f /config/bangumi.json ]; then - mv /config/bangumi.json /app/data/bangumi.json -fi - - -umask ${UMASK} -python3 main.py