我的环境是 ubuntu 24.04 + docker,自己不喜欢在系统上安装 nodejs 因为配置麻烦,需要管理环境变量和版本。真的很烦人,但是又想使用 hexo 博客系统,所以我就在想能否在 Docker 容器中运行 hexo 。经过实验是可行的。
在次分享出我的 dockerfile 和 docker-compose.yml 两个文件的内容。
dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| FROM node:18-slim
RUN apt-get update && apt-get install -y git openssh-client
WORKDIR /app
RUN npm install hexo-cli -g
ENV GIT_USER="username" ENV GIT_EMAIL="user@163.com" ENV GIT_DEFAULT_BRANCH="main"
RUN git config --global user.name ${GIT_USER} && \ git config --global user.email ${GIT_EMAIL} && \ git config --global --add safe.directory /app/.deploy_git && \ git config --global init.defaultBranch ${GIT_DEFAULT_BRANCH}
EXPOSE 4000
CMD ["hexo", "server"]
|
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| services: hexo: build: . container_name: hexo-blog image: sf/hexo-blog:latest user: "${UID}:${GID}" env_file: - .env ports: - "4000:4000" volumes: - ./:/app - /home/zsf/.gitconfig:/home/node/.gitconfig - /home/zsf/.ssh/known_hosts:/home/node/.ssh/known_hosts - /home/zsf/.ssh:/home/node/.ssh restart: always environment: - GIT_USER=小土坡 - GIT_EMAIL="xiaotupo@163.com" - GIT_DEFAULT_BRANCH=main - HTTP_PROXY="http://192.168.0.102:7897" - HTTPS_PROXY="http://192.168.0.102:7897"
|
把 dockerfile 和 docker-compose.yml 文件方的 hexo 项目的根目录,然后运行 docker compose up
来开始构建并运行容器。
执行完成后就可以打开 http://127.0.0.1:4000
看到自己的博客了。
新建文章:
docker exec -it hexo-blog bash
,进入容器后正常执行 hexo 命令即可,还可以 docker exec -it hexo-blog hexo new post "post name"
的方式创建文章。