hexo容器化部署

hexo容器化部署,从此再无系统环境迁移的烦恼。

简介

  • 本文介绍从hexo windows环境改造为docker容器化运行环境。

改造过程

hexo宿主机环境部署和docker的使用就不再赘述了,google大法既可。

  • 本着一切从简原则,直接搜了dockerhub,选了个最多下载的ipple1986/hexo ,发现直接拉取镜像是可以用的:
1
docker pull ipple1986/hexo
  • 但我往往更倾向于对dockerfile重新定制,毕竟直接FROM第三方库多少会有点不透明性
  • 于是拿着对方的dockerfile尝试重新构建镜像
1
2
3
4
5
6
7
8
9
10
FROM centos:7
ENV LANG C.UTF-8
LABEL author=ipple1986 email=ipple1986@gmail.com site=https://zhaozhiwen.net.cn
WORKDIR /opt/hexo
RUN yum install -y epel-release && \
yum install -y nodejs && npm config set registry https://registry.npm.taobao.org \
&& npm install hexo-cli -g && hexo init ipple1986 && cd ipple1986 && npm install
WORKDIR ipple1986
EXPOSE 4000
ENTRYPOINT ["hexo","server"]
  • 结果hexo使用直接报错

nodejs版本过低异常.png

  • google分析不难看出是nodejs版本过低,通过yum指定安装node版本即可
1
curl --location https://rpm.nodesource.com/setup_10.x | bash
  • 于是,最终的dockerfile为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM centos:7
ENV LC_ALL en_US.UTF-8
ENV TZ=Asia/Shanghai

LABEL maintainer="liwei <839728919@qq.com>"

WORKDIR /opt/hexo
RUN curl --location https://rpm.nodesource.com/setup_10.x | bash && \
yum install -y epel-release nodejs git && \
npm config set registry https://registry.npm.taobao.org && \
npm install hexo-cli -g

EXPOSE 4000
CMD ["hexo", "server"]
  • 使用指令如下:
1
2
3
4
5
6
7
8
9
10
11
# 编译镜像
docker build -t blog .

# 运行容器
docker run -it -v your-code-path:/opt/hexo --name blog blog:latest

# 进入容器
docker exec -it blog bash

# hexo部署
hexo d # 注意需要配置git,亦可手动输入账号密码

其他问题

  • hexo在其他主机上的docker容器中hexo d部署失败(gitpage bulid failed),已比对发现是.deploy_git没有同步更新,手动删除并hexo g -d,成功修复
-------------The End-------------
坚持原创技术分享,您的支持将鼓励我继续创作!