Hexo in a container

A bit of context
It’s July 2024 and I finally decided to create my personal blog using a static website builder.
For that, I want to develop a simple tool to ease the workflow.
With the help of containerization and make, I aim to abstract away the complexities of setting up the development environment across different computers.
Here is what I did:
Prerequisites
We need to have the following tools in our environment beforehand:
- Docker: docker.com
- or alternatively, we can use Podman: podman.io
- The Make tool: gnu.org/software/make
- and of course Git: git-scm.com
Setting up the environment
I have previous experience with HEXO static website generator and I decided to use it for this task.
I explored what Hugo and Jekyll have to offer before choosing HEXO, but I didn’t find any compelling reasons to switch to either of them.
While I might consider them for learning purposes in the future, for now, HEXO does the job just fine!
To get everything up and running, we’ll need to create a total of four different files (Yes, just 4 files and we are ready to rock with HEXO!)
For the Docker image
The Dockerfile
is relatively simple. we just have to install hexo-cli
globally in the node:lts-alpine
image.
1 | # Use an official Node.js runtime as a parent image |
I purposefully omitted the command to run the HEXO server here because we will also use this image to create the hexo project.
This part will be handled by the docker-compose.yml
file.
The environment config file
The .env
file will contains our blog name, and is used by docker-compose.yml
and Makefile
1 | BLOG_NAME=my_blog |
As a commun sense, avoid using blank spaces in the naming.
For docker-compose
1 | version: '3' |
As mentionned previously, this file contains the command to be run by the image once the container is created and started.
In our case either npm install
or hexo server
.
Makefile
The real magic happens with Make tool!
In the Makefile
we abstract away the complexities of Docker
(or Podman
) and HEXO
commands.
1 | include .env |
How to use the tool
Once the Makefile
done, it is very straightforward to use:
- To create hexo website project from scratch, change the name in the
.env
file then run:
1 | make from-scratch |
- To start an existing project, assuming the project is in the directory and set the
.env
file accordingly, then we run:
1 | make start |
It will run an npm install
on the project before running the HEXO server
The website should then be available on http://localhost:4000.
Everything is in place and ready to be used. Good luck!
- Eventually if we need to build or rebuild the container:
1 | make build |
The rest of the commands in Makefile
follows the same logic
End Notes
This is my current setup. It’s sure not perfect but it is good enough for my use case.
In my mind, if I can forget the tools and just focus on the content then I’m happy.
And happy I am :) !