我是基於ChatGPT-turbo-3.5實現的AI助手,在此網站上負責整理和概括文章
這篇文章主要介紹了博主使用的 Hexo 主題ShokaX以及為其度身訂做的Dockerfile。ShokaX是基於Shoka二次開發的主題,具有獨立的指令和安裝方式。雖然安裝過程對初學者不太友善,但因為獨特的操作介面和ts功能而受到歡迎。而Dockerfile則是將整個Hexo收進一個container中運行,並使用npm安裝插件。文章最後提供了Dockerfile的具體配置,讓讀者可以參考。
不經不覺已經過了一個月,也是博主遷站至 ShokaX 主題後的第一個月
今天想和各位分享一下自己建站時所用的 ShokaX 主題以及為其度身訂做的 Dockerfile,供大家一個參考
# ShokaX 的來源和特點
ShokaX 是一個基於 Shoka 二次開發的主題,有其獨立的一套指令和安裝方式。與其他 Hexo 主題和 Shoka 的 git clone
截然不同
npm add hexo-theme-shokax | |
pnpm add hexo-theme-shokax | |
… |
安裝後還需要手動執行 ./toolbox/hoistdep.mjs
來安裝插件,可謂對初學者不太友善
然而,正因如此,它有一個獨特的操作介面,以及各種各樣透過 ts 實行的功能
這也是為什麼,它得到了眾人的歡心
# Dockerfile
在研究 Hexo 時無意之間發現了 spurin/docker-hexo ,可以把整個 hexo 收進一個 container 中運行
恰好他的 Dockerfile 也是能夠直接透過 npm 來安裝插件的,所以便借來參考 抄考 了一下,把 ShokaX 和其依賴一同放進去了
這裏便多不賣關子了,讓我們一起看看成品如何吧
FROM node:latest | |
ENV PNPM_HOME="/pnpm" | |
ENV PATH="$PNPM_HOME:$PATH" | |
RUN corepack enable | |
# Set the server port as an environmental variable | |
ENV HEXO_SERVER_PORT=4000 | |
# Set the git username and email | |
ENV GIT_USER="Yuzuk1Shimotsuki" | |
ENV GIT_EMAIL="yuzukishimotsuki-dev@lolicon.wtf" | |
# Set the user and group that the files inside Docker belong to | |
ENV SCRIPT_USER="Yuzuk1Shimotsuki" | |
ENV SCRIPT_GROUP="users" | |
# Set the working directory | |
WORKDIR /app | |
# Install requirements | |
RUN apt-get update && \ | |
apt-get install git -y && \ | |
pnpm install hexo-cli -g | |
# Expose server port | |
EXPOSE ${HEXO_SERVER_PORT} | |
# Consolidated command to perform all checks and start the server | |
CMD echo "\n***** Welcome to Hexo server with Theme ShokaX in Docker Compose. Before we start, we need to perform some checks. *****\n"; \ | |
echo "\n***** 1. Checking if the App directory exists and has content. *****"; \ | |
if [ "$(ls -A /app)" ]; then \ | |
echo "***** 1.1. App directory exists and has content, continuing... *****"; \ | |
else \ | |
echo "***** 1.1. App directory is empty. Initializing hexo environment with ShokaX and its dependencies... *****" && \ | |
hexo init && \ | |
pnpm add hexo-admin-modern --allow-build=bcrypt && \ | |
pnpm add hexo-server && \ | |
pnpm install --ignore-scripts || { echo "Error: Failed to install $package"; exit 1; } && \ | |
#echo "\n# Admin Panel\n## Replace the username, YOUR_HASH and SOMETHING_SECRET with your own value below" >> /app/_config.yml && \ | |
#echo "admin:\n username: admin\n password_hash: YOUR_HASH\n secret: SOMETHING_SECRET" >> /app/_config.yml && \ | |
echo "\n***** Success: Hexo environment initialization with ShokaX theme completed. ****"; \ | |
fi && \ | |
echo "\n***** 2. Check for requirements.txt and install npm requirements if it exists. ****"; \ | |
if [ -f /app/requirements.txt ]; then \ | |
echo "***** 2.1. App directory contains a requirements.txt file, installing npm requirements... *****" && \ | |
cat /app/requirements.txt | xargs npm --prefer-offline install || { echo "Failed to install npm requirements"; exit 1; }; \ | |
else \ | |
echo "***** 2.1. App directory contains no requirements.txt file, continuing... *****"; \ | |
fi && \ | |
echo "\n***** 3. Checking if app .ssh directory exists and has content. ****"; \ | |
if [ "$(ls -A /app/.ssh 2>/dev/null)" ]; then \ | |
echo "***** 3.1. App .ssh directory exists and has content, continuing... *****"; \ | |
else \ | |
echo "***** 3.1. App .ssh directory is empty, initializing ssh key and configuring known_hosts for common git repositories (GitHub/GitLab)... *****" && \ | |
rm -rf ~/.ssh/* && \ | |
ssh-keygen -t rsa -f ~/.ssh/id_rsa -q -P "" && \ | |
ssh-keyscan github.com > ~/.ssh/known_hosts 2>/dev/null && \ | |
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts 2>/dev/null && \ | |
cp -r ~/.ssh /app; \ | |
fi && \ | |
echo "\n***** 4. Running git config, user = ${GIT_USER}, email = ${GIT_EMAIL} *****" && \ | |
git config --global user.email ${GIT_EMAIL} && \ | |
git config --global user.name ${GIT_USER} && \ | |
git config --global --add safe.directory /app && \ | |
echo "\n***** 5. Copying .ssh from App directory and setting permissions *****" && \ | |
cp -r /app/.ssh ~/ && \ | |
chmod 600 ~/.ssh/id_rsa && \ | |
chmod 600 ~/.ssh/id_rsa.pub && \ | |
chmod 700 ~/.ssh && \ | |
echo "\n***** Contents of public ssh key (for deploy) - *****" && \ | |
cat ~/.ssh/id_rsa.pub && \ | |
echo "\n\n\n\n\n***** Success: all checks has been passed, waiting the server to start... *****\n\n\n\n\n" && \ | |
echo "\n***** Creating user and group, setting permissions, and starting Hexo server *****" && \ | |
useradd ${SCRIPT_USER} || echo "***** User already exists, skipping... *****" && \ | |
groupadd ${SCRIPT_GROUP} || echo "***** Group already exists, skipping... *****" && \ | |
chmod -R 777 /app && \ | |
chown -R ${SCRIPT_USER}:${SCRIPT_GROUP} /app && \ | |
echo "\n***** Starting server on port ${HEXO_SERVER_PORT} *****" && \ | |
hexo server -d -p ${HEXO_SERVER_PORT} |
有關 Dockerfile 的各種配置請參見這裏