Docker 初始化 Hexo 镜像
1. 构建 Docker 镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #!/bin/sh
if [ -f "/root/.ssh_mount/id_rsa" ]; then mkdir -p /root/.ssh cp /root/.ssh_mount/id_rsa /root/.ssh/id_rsa chmod 600 /root/.ssh/id_rsa echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config else echo "Error: id_rsa not found. Please mount it to /root/.ssh_mount/id_rsa" exit 1 fi
git config --global user.name "zengy" git config --global user.email i@zengy.win
if [ ! -d ".git" ]; then echo "Initializing git repository..." git init git remote add origin git@github.com:zengycn/vercel.git echo "Pulling from master..." git pull origin master else echo "Repository exists. Pulling latest changes..." git remote set-url origin git@github.com:zengycn/vercel.git git pull origin master fi
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| FROM alpine:latest
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \ apk add --no-cache git openssh
WORKDIR /usr/src/hexo
COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
|
2. 运行容器
首先需创建两个挂载点 👀
1 2 3 4
| docker run --rm \ -v /path/to/your/local/hexo:/usr/src/hexo \ -v ~/.ssh/id_rsa:/root/.ssh_mount/id_rsa \ registry.cn-hangzhou.aliyuncs.com/z-z/hexo-init:github
|
注意将 /path/to/your/local/hexo 替换为你宿主机上想要保存代码的实际路径
Docker 运行 Hexo 镜像
1. 构建 Docker 镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #!/bin/sh
if [ -f "/root/.ssh_mount/id_rsa" ]; then echo "Found mounted SSH key, configuring..." mkdir -p /root/.ssh cp /root/.ssh_mount/id_rsa /root/.ssh/id_rsa chmod 600 /root/.ssh/id_rsa echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config else echo "Warning: No SSH key mounted at /root/.ssh_mount/id_rsa" echo "Git operations might fail if the repo is private." fi
git config --global user.name "zengy" git config --global user.email i@zengy.win
if [ ! -d ".git" ]; then echo "Directory is empty or not a git repo. Initializing..." git init git remote add origin git@github.com:zengycn/vercel.git echo "Pulling latest code from master..." git pull origin master else echo "Git repo detected. Updating..." git remote set-url origin git@github.com:zengycn/vercel.git git pull origin master fi
echo "Checking dependencies..." if [ -f "package.json" ]; then echo "Installing/Updating npm packages..." npm install else echo "Warning: package.json not found!" fi
echo "Starting application..." exec "$@"
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| FROM node:lts-alpine
ENV TZ=Asia/Shanghai
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \ apk add --no-cache git openssh tzdata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/src/hexo
RUN npm install hexo-cli -g
COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh
EXPOSE 4000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["hexo", "server", "-i", "0.0.0.0"]
|
2. 运行容器
同样挂载 代码目录 和 SSH密钥
1 2 3 4 5 6
| docker run -d \ --name hexo \ -p 4000:4000 \ -v /path/to/your/local/hexo:/usr/src/hexo \ -v ~/.ssh/id_rsa:/root/.ssh_mount/id_rsa \ registry.cn-hangzhou.aliyuncs.com/z-z/hexo-theme-redefine:github
|