从零开始Docker化Ghost博客系统


#其他#


2015-12-06

Ghost是一基于node的博客系统,速度快,够简洁。

注意Ghost支持的Node的版本:

$ node index.js 
ERROR: Unsupported version of Node
Ghost needs Node version ~0.10.0 || ~0.12.0 you are using version 4.2.2

将容器转化为镜像

这一节把node安装在/node下,把ghost安装在/ghost目录下。

拉取一个镜像

$ sudo docker pull ubuntu:14.04

使用sudo docker images查看本机有哪些容器。如果一个镜像有多个标签,则会显示多个。

启动一个容器

例如:

$ sudo docker run -ti ubuntu:14.04 /bin/bash
root@5989ceee9a40:/# uname -a
Linux 5989ceee9a40 3.16.0-38-generic #52~14.04.1-Ubuntu SMP Fri May 8 09:43:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

可以在宿主机上运行sudo docker ps查看有哪些容器正在运行。

安装curl等

root@5989ceee9a40:/# apt-get install -y curl
root@5989ceee9a40:/# apt-get install -y zip

下载node

root@5989ceee9a40:/node# curl -SLO https://nodejs.org/dist/v0.12.9/node-v0.12.9-linux-x64.tar.gz
root@5989ceee9a40:/node# tar -xzf node-v0.12.9-linux-x64.tar.gz
root@5989ceee9a40:/node# mv node-v0.12.9-linux-x64/* .
root@5989ceee9a40:/node# rm -rf node-v0.12.9-linux-x64
root@5989ceee9a40:/node# ls
ChangeLog  LICENSE  README.md  bin  etc  include  lib  node-v0.12.9-linux-x64.tar.gz  share

下载ghost

root@5989ceee9a40:/node# mkdir /ghost
root@5989ceee9a40:/node# cd /ghost/
root@5989ceee9a40:/ghost# curl -SLO https://ghost.org/zip/ghost-0.7.2.zip 
root@5989ceee9a40:/ghost# unzip ghost-0.7.2.zip
root@5989ceee9a40:/ghost# ls
Gruntfile.js  LICENSE  PRIVACY.md  README.md  config.example.js  config.js  content  core  ghost-0.7.2.zip  index.js  node_modules  npm-shrinkwrap.json  package.json
root@5989ceee9a40:/ghost# npm install -g npm@latest
root@5989ceee9a40:/ghost# npm install --production   # 比较耗时
root@5989ceee9a40:/ghost# sed 's/127.0.0.1/0.0.0.0/' config.example.js > config.js

退出容器

exit后会容器不再运行,通过下面方法重启容器:

$ sudo docker start 5989ceee9a40
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
5989ceee9a40        ubuntu:14.04        "/bin/bash"              4 hours ago         Up 16 seconds                                desperate_dijkstra

$ sudo docker attach 5989ceee9a40
root@5989ceee9a40:/# 

将容器转换为镜像

commit的帮助文档如下:

$ docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

  -a, --author=       Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change=[]     Apply Dockerfile instruction to the created image
  --help=false        Print usage
  -m, --message=      Commit message
  -p, --pause=true    Pause container during commit

在主机上,将刚才的容器转换为镜像:

$ sudo docker commit -m "ghost 0.7.2 on ubuntu" -a "letian" 5989ceee9a40 letian/ghost:v1
33eb15dca18e4d7a77673b1a12282699a0dbaaa51c38f5db45d51ffbe77af08d

$ sudo docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
letian/ghost                       v1                  33eb15dca18e        4 minutes ago       457.5 MB
..........

个头有点大。。

测试

启动一个ghost服务:

$ sudo docker run -d -p 8000:2368 letian/ghost:v1  /node/bin/node /ghost/index.js
88124fab0a0d43603ee50113a2380a5a22890d4040f27d144c412b4dedb66e7b

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
88124fab0a0d        letian/ghost:v1     "/node/bin/node /ghos"   38 seconds ago      Up 37 seconds       0.0.0.0:8000->2368/tcp   loving_colden

上面忘了把NODE_ENV设置为production了,加-e指定就行了:

$ sudo docker run -d -e NODE_ENV=production -p 8008:2368 letian/ghost:v1  /node/bin/node /ghost/index.js

在宿主机上,用浏览器打开http://<your-ip>:8008/,可以查看ghost是否正常运行。

使用Dockerfile构建镜像

现在我们尝试使用Dockerfile构建镜像。同时,为了解决上面遇到的npm install比较耗时的问题,将Ghost 中文集成版直接作为文件添加进镜像。这个ghost中文集成版在汉化的基础上把所有的依赖都放进去了,所以不需要npm install

创建目录ghost-0.7.0,其中的文件如下:

$ ls
Dockerfile
Ghost-0.7.0-zh-full.zip
start.sh

Dockerfile

FROM ubuntu:14.04

ADD Ghost-0.7.0-zh-full.zip /tmp/Ghost-0.7.0-zh-full.zip
ADD start.sh /start-ghost.sh

ENV NODE_VERSION 0.12.9

RUN \
    apt-get update && \
    apt-get install -y curl && \
    apt-get install -y zip  && \
    cd /tmp && \
    curl -SLO "https://nodejs.org/dist/v0.12.9/node-v$NODE_VERSION-linux-x64.tar.gz"  && \
    tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1  && \
    unzip Ghost-0.7.0-zh-full.zip -d /ghost && \
    rm /tmp/Ghost-0.7.0-zh-full.zip && \
    cd /ghost && \
    sed 's/127.0.0.1/0.0.0.0/' config.example.js > config.js

WORKDIR /ghost

CMD ["bash", "/start-ghost.sh"]

EXPOSE 2368

Ghost-0.7.0-zh-full.zip被放到/tmp目录,start.sh添加到/start-ghost.sh,创建容器时候会运行bash /start-ghost.sh

start.sh

# !/bin/bash

GHOST="/ghost"

cd "$GHOST"

NODE_ENV=${NODE_ENV:-production} npm start

构建镜像

$ cd ghost-0.7.0
$ sudo docker build -t letian/ghost:v2 .

创建容器、测试

$ sudo docker run -d -p 8080:2368 letian/ghost:v2

在宿主机上用浏览器打开http://<your-ip>:8080/,可以看到ghost正常运行。

优化

ghost的数据库和图片等最好是放在数据卷中(VOLUMN)。

Dockerfile

FROM ubuntu:14.04

ADD Ghost-0.7.0-zh-full.zip /tmp/Ghost-0.7.0-zh-full.zip
ADD start.sh /start-ghost.sh

ENV NODE_VERSION 0.12.9

RUN \
    apt-get update && \
    apt-get install -y curl zip && \
    cd /tmp && \
    curl -SLO "https://nodejs.org/dist/v0.12.9/node-v$NODE_VERSION-linux-x64.tar.gz"  && \
    tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1  && \
    unzip Ghost-0.7.0-zh-full.zip -d /ghost && \
    rm /tmp/Ghost-0.7.0-zh-full.zip && \
    cd /ghost && \
    sed 's/127.0.0.1/0.0.0.0/' config.example.js > config.js

VOLUME /ghost-external

WORKDIR /ghost

CMD ["bash", "/start-ghost.sh"]

EXPOSE 2368

就是在之前的基础上加了行VOLUME /ghost-external

start.sh

# !/bin/bash

GHOST="/ghost"
GHOST_EXTERNAL="/ghost-external"

CONFIG="config.js"
DATA="content/data"
IMAGES="content/images"
THEMES="content/themes"

mkdir -p "$GHOST_EXTERNAL/$DATA"
rm -rf "$GHOST/$DATA"
ln -s "$GHOST_EXTERNAL/$DATA" "$GHOST/$DATA"

mkdir -p "$GHOST_EXTERNAL/$IMAGES"
rm -rf "$GHOST/$IMAGES"
ln -s "$GHOST_EXTERNAL/$IMAGES" "$GHOST/$IMAGES"

if [ -d "$GHOST_EXTERNAL/$THEMES" ]; then
    rm -rf "$GHOST/$THEMES"
    ln -s "$GHOST_EXTERNAL/$THEMES" "$GHOST/$THEMES" 
fi

if [ -f "$GHOST_EXTERNAL/$CONFIG" ]; then
    rm -f "$GHOST/$CONFIG"
    ln -s "$GHOST_EXTERNAL/$CONFIG" "$GHOST/$CONFIG"
fi

cd "$GHOST"

NODE_ENV=${NODE_ENV:-production} npm start

数据卷/ghost-external中的目录、文件被软链接到/ghost对应的位置上。

构建镜像

$ sudo docker build -t letian/ghost:v3 .

创建容器、测试

创建容器,将宿主机目录挂载到数据卷:

$ mkdir ~/local-ghost
$ sudo docker run -d -p 8090:2368 -v ~/local-ghost:/ghost-external letian/ghost:v3

在宿主机上用浏览器打开http://<your-ip>:8090/,可以看到ghost正常运行。~/local-ghost也有相应的文件、目录被创建。

在云服务中构建镜像

这里选择灵雀云构建镜像。

首先将Dockerfile等上传到Github(也可以是Git@OSC、BitBucket)中。

登录灵雀云,

点击“创建构建镜像仓库”,授权读取Github中的repo,选择ghost对应的项目构建即可。

然后在“服务”中选择“创建服务”,会提示用户选择镜像,选择“我的镜像”中刚才构建好的docker-ghost,

点击“配置镜像”后:

填写指定服务名称,将服务类型改为“有状态服务”,然后进入“高级配置”:

此处,需要指定存储卷大小,端口的服务地址类型需要配置成http-endpoint:

然后创建服务,查看服务详情:

浏览器打开红线处的URL就可以看到Ghost博客了。

其他

软链接

ln -s from-file to-link-file

参考

dockerfile/ghost
nodejs/docker-node
Docker 技术入门与实战


( 本文完 )