使用Docker在阿里云ECS上面搭建Ghost轻博客


环境准备

需要阿里云ECS一台,使用的是镜像市场的镜像“Docker运行环境(Ubuntu 64位)”。

升级最新版本Docker

由于自带的版本为docker 1.2,所以升级最新版本

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
$ sudo sh -c "echo deb https://get.docker.com/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install lxc-docker

升级成功如下:

# docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8
OS/Arch (client): linux/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8

下载images

系统自带的images为:

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6             redis               5be6d903898f        10 weeks ago        350.5 MB
centos              centos6             68edf809afe7        3 months ago        212.7 MB

下载最新ubuntu系统image
# docker pull ubuntu:14.04
由于国内网络原因下载原image异常慢,建议使用国内的源比如docker.cn或者使用https://www.daocloud.io的加速器。

# docker pull docker.cn/docker/ubuntu:14.04
下载完成如下

# docker pull ubuntu:14.04
Pulling repository ubuntu
8eaa4ff06b53: Download complete 
511136ea3c5a: Download complete 
3b363fd9d7da: Download complete 
607c5d1cca71: Download complete 
f62feddc05dc: Download complete 
Status: Downloaded newer image for ubuntu:14.04

可以再看下images

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               8eaa4ff06b53        3 days ago          188.3 MB
ubuntu              14.04.1             8eaa4ff06b53        3 days ago          188.3 MB
ubuntu              latest              8eaa4ff06b53        3 days ago          188.3 MB
ubuntu              trusty              8eaa4ff06b53        3 days ago          188.3 MB
centos6             redis               5be6d903898f        10 weeks ago        350.5 MB
centos              centos6             68edf809afe7        3 months ago        212.7 MB

启动一个container

# docker run -it ubuntu:14.04 /bin/bash
再打开一个ssh窗口查看已经启动的container

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES   
d67282e72ec6        ubuntu:14.04        "/bin/bash"         10 hours ago        Up 4 seconds                               condescending_pare   

启动之后就进入容器内部了,可以执行linux里面的命令

root@d67282e72ec6:/# uname -a
Linux d67282e72ec6 3.13.0-30-generic #54-Ubuntu SMP Mon Jun 9 22:45:01 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

开始安装Ghost

  • 安装基础环境
    还是在上一步启动的container中安装必要的软件环境
apt-get update
apt-get install curl
apt-get install unzip
apt-get install git
  • 安装Node.js环境
curl -sL https://deb.nodesource.com/setup | sudo bash -
apt-get install -y nodejs
apt-get install -y build-essential
  • 安装并运行 Ghost
    下载最新版的Ghost安装包
# curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip

解压缩

# unzip -uo ghost.zip -d ghost

进入解压缩的ghost文件里面进行安装

# cd /ghost
# npm install --production

安装成功之后启动ghost

# npm start
> [email protected] start /ghost
> node index
Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Complete
Ghost is running in development... 
Listening on 127.0.0.1:2368 
Url configured as: http://localhost:2368 
Ctrl+C to shut down

此时程序的监听ip为127.0.0.1,需要修改config.js

sed -i "s/127.0.0.1/0.0.0.0/g" config.js 

安装完毕,我们需要写一个简单的启动脚本start.sh

#! /bin/bash
cd /ghost
npm start --production

以后启动容器的时候执行此脚本就可以了。

/ghost/start.sh

制作新的image

退出之前的操作,然后这个容器就自动停止了

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS                  NAMES    
d67282e72ec6        ubuntu:14.04        "/bin/bash"         11 hours ago        Exited (0) 48 minutes ago                          condescending_pare   

commit一个新的image

docker commit d67282e72ec6 ghost:v1

查询下新的images

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ghost               v1                  e66b3df554f4        10 hours ago        423.1 MB

然后重新运行一个容器,并把宿主机的80端口映射上去

docker run -d -p 80:2368 ghost:v1 /ghost/start.sh

通过http://182.92.100.246/ 就可以访问博客了。

ghost20150105092040.jpg

后记

  • 以上操作可以直接使用一个dockerfile来创建,后续补上。
  • 由于ghost使用谷歌字体,所以打开很慢,建议更换360CDN的字体加速。
cd /ghost/content/themes/StayPuft
# vi default.hbs
 

你可能感兴趣的:(使用Docker在阿里云ECS上面搭建Ghost轻博客)