在 Docker 中安装 Hexo

为了不污染本地环境,尝试在 Docker 中安装 Hexo,结果遇到不少坑。


旧版 Docker 需要 VirtualBox,网上教程大多使用 Boot2DockerDocker Toolbox,但新版以 HyperKit 方式直接支持,更加方便。


什么是 Docker

Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护。

Docker 自开源后受到广泛的关注和讨论,以至于 dotCloud 公司后来都改名为 Docker Inc。Redhat 已经在其 RHEL6.5 中集中支持 Docker;Google 也在其 PaaS 产品中广泛应用。

Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案。Docker 的基础是 Linux 容器(LXC)等技术。

在 LXC 的基础上 Docker 进行了进一步的封装,让用户不需要去关心容器的管理,使得操作更为简便。用户操作 Docker 的容器就像操作一个快速轻量级的虚拟机一样简单。

安装 Docker

1
2
brew cask install docker
brew cask install kitematic

Kitematic 非必须,是用于管理镜像(image)和容器(container)的 GUI。更推荐在 CLI 中进行操作。

编写 Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# https://docs.docker.com/engine/reference/builder/

# The `FROM` instruction sets the Base Image for subsequent instructions.
FROM node

# The `MAINTAINER` instruction allows you to set the Author field of the generated images.
MAINTAINER nuomi1 <[email protected]>

# The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD`, `ENTRYPOINT`, `COPY` and `ADD` instructions that follow it in the Dockerfile.
WORKDIR /blog

# The `RUN` instruction will execute any commands in a new layer on top of the current image and commit the results.
RUN npm install hexo-cli --global

# The `CMD` instruction is to provide defaults for an executing container.
CMD ["bash"]

# The `EXPOSE` instruction informs Docker that the container listens on the specified network ports at runtime.
EXPOSE 4000

# The `VOLUME` instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.
VOLUME ["/blog"]

基于 Node.js 镜像制作 Hexo 镜像。EXPOSE 监听容器端口,实现宿主机访问容器。VOLUME 把宿主机文件夹与容器绑定,实现数据交换。

制作 Hexo 镜像

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
31
32
33
34
35
➜ ~ docker build -t hexo:3.2.2 .
Sending build context to Docker daemon 2.56 kB
Step 1 : FROM node
---> e3e7156ded1f
Step 2 : MAINTAINER nuomi1 <[email protected]>
---> Running in ec112a08d029
---> 8f879fde3b92
Removing intermediate container ec112a08d029
Step 3 : WORKDIR /blog
---> Running in c38a1ed24e99
---> fc7af853de2d
Removing intermediate container c38a1ed24e99
Step 4 : RUN npm install hexo-cli --global
---> Running in 443bc5a93f6f

...

npm WARN optional Skipping failed optional dependency /hexo-cli/chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: [email protected]
npm info ok
---> 822836fccda7
Removing intermediate container 443bc5a93f6f
Step 5 : CMD bash
---> Running in 54aaa1b027cc
---> 16355691b010
Removing intermediate container 54aaa1b027cc
Step 6 : EXPOSE 4000
---> Running in 63d3a94bbec8
---> 76e4bb5cb68c
Removing intermediate container 63d3a94bbec8
Step 7 : VOLUME /blog
---> Running in 570626f0c9d3
---> 1665568a3712
Removing intermediate container 570626f0c9d3
Successfully built 1665568a3712

fsevents 用于 OS X,但 Node.js 镜像为 Debian,忽略即可。

制作 Hexo 容器

1
docker run -i --name hexo -p 4000:4000 -t -v ~/blog:/blog hexo:3.2.2

容器 4000 端口宿主机 4000 端口绑定,宿主机访问 http://localhost:4000 即可预览博客。宿主机 ~/blog 与容器 /blog 绑定,实现数据交换。

运行 Hexo 容器

1
docker start -i hexo