0%

将Hexo的编写环境放到Docker里

Hexo 使用的nodejs、npm 最近的版本升级非常快,而且版本依赖也很不稳定,一直想把用Hexo 写博客的环境固化到Docker里,在任何地方只要把博客的源文件拉下来,映射到docker里就能写、能编译、能发布。

今天就想花点时间把它搞定,没想到花了大约一整天的时间。先出完整的解决方法,后面再分析遇到的问题。

完整的解决方法

构建docker镜像

dockerfile 完整内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM node:16-buster
LABEL maintainer="thinker<156884481@qq.com>"
LABEL version="1.0"

RUN sed -i 's#http://deb.debian.org#https://mirrors.tuna.tsinghua.edu.cn#g' /etc/apt/sources.list
RUN apt-get update && \
apt-get install -y gnutls-bin vim && \
npm config set registry https://registry.npm.taobao.org --global && \
npm config set disturl https://npm.taobao.org/dist --global && \
npm install -g hexo-cli && \
npm install -g hexo

RUN git config --global http.sslVerify false && \
git config --global http.postBuffer 9048576000
# git config --global user.email "156884481@qq.com" && \
# git config --global user.name "thinker"

WORKDIR /data

ENTRYPOINT bash

dockerfile 还是比较简单的,基于node官方的镜像,安装hexo,然后设置工作目录为/data,使用了清华的源来安装软件包,其中 gnutls-bin 这个包是关键,后面细说。

然后在dockefile同级目录下执行构建镜像的命令

1
docker build -t node-hexo:16-buster .
1
2
3
4
blog on  master
❯ docker images |grep node-hexo
node-hexo 16-buster 953e168263a7 43 minutes ago 937MB

运行

在运行容器时将blog 的源码挂载到/data 目录下,然后在容器里进行编写md 和在外面(宿主机)编写md都可以,在容器里进行编译和发布。

运行容器,挂载源码

1
docker run -it -v /blog:/data -p 4000:4000  node-hexo:16-buster

在容器内的/data/blog 目录可以看到博客的源码,如果是新建的博客可以参照hexo的使用文档。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@8af85682a9e5:/data/blog# ls -l
total 1092
-rw-r--r-- 1 root root 13 Aug 29 11:12 README.md
-rw-r--r-- 1 root root 0 Aug 28 13:42 _config.landscape.yml
-rw-r--r-- 1 root root 2860 Aug 29 11:45 _config.yml
-rw-r--r-- 1 root root 600204 Aug 29 12:28 db.json
drwxr-xr-x 342 root root 10944 Aug 29 01:11 node_modules
-rw-r--r-- 1 root root 327974 Aug 29 01:12 package-lock.json
-rw-r--r-- 1 root root 688 Aug 29 01:12 package.json
drwxr-xr-x 19 root root 608 Aug 29 11:41 public
drwxr-xr-x 5 root root 160 Aug 28 13:42 scaffolds
drwxr-xr-x 9 root root 288 Aug 29 01:01 source
drwxr-xr-x 5 root root 160 Aug 29 00:31 themes
-rw-r--r-- 1 root root 113211 Aug 29 01:12 yarn.lock

执行下面的命令可以在本地启动代理 ,随时查看正在编写的博客内容

1
hexo s 

编写完后,执行下面命令进行发布

1
hexo d

发布的时候要注意_config.yml里面delpoy的配置,配置如下:

1
2
3
4
5
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
- type: git
repo: https://[这里是在github上生成的一个key,方法参见文章结尾]@github.com/iamlibo/iamlibo.github.io.git

deploy是hexo的一个插件,使用方法参见官网

1
npm install hexo-deployer-git --save

这样编写、预览、发布的整体的流程就跑通了,源代码在宿主机中管理,编译环境在docker中。

遇到的问题

1、 使用的Node的镜像版本低,会导致访问github出错

1
2
unable to access 'https://github.com/iamlibo/iamlibo.github.io.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.

这个问题网上也有很多的资料,主要原因是git 使用的SSL 依赖包变化导致的,网上的方法多数重新编译git 使用open-ssl 然后再替换原有的程序包。在构建docker 镜像的时候去重新编译git 还是挺复杂的,缺少的依赖太多。

这个问题的解决方法如下,在dockerfile 中都有体现

1
2
3
4
apt-get install gnutls-bin
git config --global http.sslVerify false
git config --global http.postBuffer 1048576000

2、是因为github提交要求把密码换成token,提示信息大概是这样:

1
2
3
4
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access 'https://github.com/iamlibo/iamlibo.github.io.git/': The requested URL returned error: 403

这个问题本来应该挺简单,但是我没有仔细看,把解决的思路搞错了,以为是要自己生成一个ssh-key 上传到github 的Deploy keys 中去,在这块浪费了好长时间。后来在github 上生成一个key ,做为提交时候的密码就可以了。

参考资料

在github上配置token的方法,github开发人员在七夕搞事情:remote: Support for password authentication was removed on August 13, 2021

Git Clone错误解决:GnuTLS recv error (-110): The TLS connection was non-properly terminated