Build and Deploy a Hexo Blog with Docker

Hexo

Install and scaffold:

1
2
3
4
5
npm install hexo-cli -g
hexo init blog
cd blog
npm install
hexo server

Docs: https://hexo.io

Docker Deployment

Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
FROM node:latest AS build-env
RUN mkdir -p /usr/src/hexo-blog
WORKDIR /usr/src/hexo-blog
COPY . .
RUN npm --registry=https://registry.npm.taobao.org install hexo-cli -g && npm install
RUN hexo clean && hexo g

FROM nginx:latest
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/share/nginx/html
COPY --from=build-env /usr/src/hexo-blog/public /usr/share/nginx/html
EXPOSE 80

Build and run:

1
2
docker build -t hexo-blog:latest .
docker run -p 80:80 -d hexo-blog:latest

Nginx TLS Config

Obtain a cert (e.g., via https://freessl.org/), then use an nginx.conf like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
events { worker_connections 1024; }
http {
  server {
    listen 443;
    server_name example.com;
    root /usr/share/nginx/html;
    index index.html index.htm;
    ssl on;
    ssl_certificate /etc/nginx/full_chain.pem;
    ssl_certificate_key /etc/nginx/private.key;
    include /etc/nginx/mime.types;
    gzip on; gzip_min_length 5k; gzip_buffers 4 16k; gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types text/plain application/json application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
    gzip_vary on;
  }
  server {
    listen 80; server_name example.com;
    root /usr/share/nginx/html; index index.html index.htm;
    include /etc/nginx/mime.types;
  }
}

Run with mounts for config and certs:

1
2
3
4
5
6
docker run -p 80:80 -p 443:443 \
  --name hexo-site \
  -v ~/docker-data/site/nginx.conf:/etc/nginx/nginx.conf \
  -v ~/docker-data/site/ssl/full_chain.pem:/etc/nginx/full_chain.pem \
  -v ~/docker-data/site/ssl/private.key:/etc/nginx/private.key \
  --restart=always -d hexo-blog:latest

Example reverse-proxy config (non-static): define upstreams and proxy_pass in server blocks as needed.

使用 Hugo 构建
主题 StackJimmy 设计