0%

在docker中部署hexo

开发机软件环境比较复杂,为避免无谓的干扰,所以将hexo环境放入docker中
于是写了个Dockfile
只用来本地预览,发布还是由appveyor完成
顺手够用就好

参考
VSCode 配合sftp同步编辑远程代码

源码

Dockerfile

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FROM node:lts-alpine

WORKDIR /hexo
ENV LANG C.UTF-8

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk add --no-cache git tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
npm config set registry https://registry.npm.taobao.org && \
npm install hexo-cli -g --no-optional && \
npm install hexo-tag-plantuml --save && \
rm -rf /var/cache/apk/* /tmp/*

COPY docker-entrypoint.sh /docker-entrypoint.sh

RUN hexo version

# 注意命令执行顺序
ENTRYPOINT ["/docker-entrypoint.sh"]

容器运行初始化脚本

entrypoint.sh

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
set -x

HEXO_DIR=/hexo

cd $HEXO_DIR

npm install --no-optional --no-bin-links

exec "$@"

使用

将Dockerfile,entrypoint.sh存为文件,放在在同一目录下
执行命令

1
2
3
4
5
docker build -t hexo:node-lts-alpine .
docker run -d --restart=always --name hexo-server -p 4000:4000 -v /path-to-hexo:/hexo hexo:node-lts-alpine hexo s

# 配置别名,方便本地化使用hexo命令
alias hexo="docker exec hexo-server hexo"

注意事项

  • hexo博客文件的 title 中不能包含特殊字符,如%$等,会导致执行 hexo g 时报错 ERROR Template render error:URIError: URI malformed
    • 可以使用HTML字符实体进行替换 如 $ $% %
  • 文件名不能包含 &,会导致 sitemap 出错 xmlParseEntityRef: no name
  • 如果自定义域名了,CNAME 要放在 source 目录下,否则 generate 后 deploy 时会被删除

原文链接