diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe769cf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +# .dockerignore + +.git +.gitattributes +.gitignore +.github + +.editorconfig + +README.md + +Dockerfile + +[b|B]in +[O|o]bj \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1b299c4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +# Dockerfile +# build +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env +WORKDIR ./ + +COPY . . + +RUN dotnet restore +RUN dotnet build --configuration Release --no-restore -o out + + +## runtime +FROM mcr.microsoft.com/dotnet/aspnet:5.0 +WORKDIR /bilibili + +# install cron and nano +RUN apt update +RUN apt install -qq -y cron nano + +# copy files from build +COPY --from=build-env ./out . + +# make mount point +RUN mkdir /bilibili/config + +# copy scripts +COPY ./docker/entry.sh /bilibili +COPY ./docker/job.sh /bilibili +COPY ./docker/template.json /bilibili + +# copy template config +COPY ./docker/template.json /bilibili/config + +# setup cron +COPY ./docker/crontab /etc/cron.d/bilicron +RUN chmod 0644 /etc/cron.d/bilicron +RUN crontab /etc/cron.d/bilicron +RUN touch /var/log/cron.log + + +VOLUME ["/bilibili/config"] +ENTRYPOINT ["/bin/bash", "-c", "/bilibili/entry.sh"] \ No newline at end of file diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..965f825 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,36 @@ +# Docker 使用说明 + +### 1 + +在有Dockerfile的目录运行 + +`docker build -t TARGET_NAME .` + + `TARGET_NAME`为镜像名称和版本 自己起个名字 + +### 2 + +完成后 + +`docker run -d IMAGE_NAME -v PATH_TO_CONFIGS:/bilibili/config` + +`IMAGE_NAME`为刚才镜像的名字 + +`PATH_TO_CONFIGS`为任意目录 + +Window系统推荐使用Docker gui + +* 如果目录中含有.json后缀的文件则会以该文件运行BilibiliTool +* 可以放置多个.json文件来实现多账号 +* 如果目录为空则会生成一个模板文件方便配置 +* 也可以使用`docker volume` 如果没有映射目录则自动生成volume +* 默认每天15点自动运行一次,每次容器启动也会运行一次 +* 更改运行时间/频率用`docker exec -it CONTAINER_NAME crontab -e` 默认编辑器是nano + +## + +构建环境: mcr.microsoft.com/dotnet/sdk:5.0 + +运行环境: mcr.microsoft.com/dotnet/aspnet:5.0 + +大概不支持arm \ No newline at end of file diff --git a/docker/crontab b/docker/crontab new file mode 100644 index 0000000..bea6f47 --- /dev/null +++ b/docker/crontab @@ -0,0 +1 @@ +0 15 * * *root /bin/bash /bilibili/job.sh >> /var/log/cron.log diff --git a/docker/entry.sh b/docker/entry.sh new file mode 100644 index 0000000..584a2a3 --- /dev/null +++ b/docker/entry.sh @@ -0,0 +1,16 @@ +#!/bin/bash +#https://stackoverflow.com/questions/3856747/check-whether-a-certain-file-type-extension-exists-in-directory + +# if no configs are found, copy the template to configs directory +configs=(`find /bilibili/config -maxdepth 1 -name "*.json"`) +if [ ${#configs[@]} -eq 0 ]; then + cp /bilibili/template.json /bilibili/config +fi + +echo Starting first run +/bin/bash /bilibili/job.sh + +echo By default, Bilibilitool will run at 15:00 every day for each config file +echo To configure scheduling, run \'docker exec -it CONTAINER_NAME crontab -e\' and edit + +cron && tail -f /var/log/cron.log diff --git a/docker/job.sh b/docker/job.sh new file mode 100644 index 0000000..4007477 --- /dev/null +++ b/docker/job.sh @@ -0,0 +1,15 @@ +#!/bin/bash +configs=(`find /bilibili/config -maxdepth 1 -name "*.json"`) +if [ ${#configs[@]} -gt 0 ]; then + for ((idx=0; idx<${#configs[@]}; ++idx)) + do + : + echo Copying config file $idx + cp ${configs[idx]} /bilibili/appsettings.json + /bilibili/Ray.BiliBiliTool.Console -closeConsoleWhenEnd=1 + echo Execution finished + done +else + echo No config files found in /bilibili/config + exit 0 +fi \ No newline at end of file