in 工具 docker ~ read.

Docker 部署使用Ghost博客

因为之前多次进行vps的迁移,使得博客也不停的迁移,每次随着迁移基本都会把之前的历史文章丢失,因此,这次选择docker的方式,尽可能保住以后的数据。
本文纯安装使用过程,不予讲解,后面会继续介绍各个组件

基础环境

安装docker

yum install docker
service docker start
chkconfig docker on

安装docker-compose

curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

ghost部署路径

/home/opt/myghost
cd /home/opt/myghost
mkdir docker-nginx-ghost-mysql

部署

编写docker-compose自动化文件

cd docker-nginx-ghost-mysql
vim docker-compose.yml

文件内容如下

version: '2'
services:
  mysql:
    image: mysql:5.7
    restart: always
    ports:
      - "33306:3306"
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --init-connect='SET NAMES utf8mb4;' --innodb-flush-log-at-trx-commit=0
    networks:
      blog-network:
        aliases:
          - mysql
    environment:
      - MYSQL_ROOT_PASSWORD=Root-123
      - MYSQL_DATABASE=ghostdb
      - MYSQL_USER=ghost_user
      - MYSQL_PASSWORD=Ghost-123
    volumes:
      - ./mysql/data/:/var/lib/mysql
  nginx:
    image: nginx:1.14-alpine
    restart: always
    ports:
      - "80:80"
      - "443:443"
    networks:
      blog-network:
        aliases:
          - nginx
    volumes:
      - ./nginx/config/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/config/conf.d:/etc/nginx/conf.d
      - ./nginx/ssl:/etc/nginx/ssl/blog
      - ./nginx/log:/var/log/nginx
  ghost:
    image: ghost
    depends_on:
      - mysql
      - nginx
    expose:
      - "2368"
    ports:
      - "2368:2368"
    volumes:
      - ./ghost/config/config.json:/var/lib/ghost/config.production.json
      - ./ghost/content:/var/lib/ghost/content
    networks:
      blog-network:
        aliases:
          - ghost
networks:
  blog-network:
    driver: bridge

其中,配置了MySQL/nginx/ghost的基础配置,包括外挂(volumes)的路径,即可以在本地路径看到docker内的文件;

配置ghost

在上一步的配置中,把ghost的config.production.json链接到了当前目录./ghost/config/config.json,内容如下

  {
    "url": "http://www.ccyuki.com",
    "server": {
      "port": 2368,
      "host": "0.0.0.0"
    },
    "database": {
        "client": "mysql",
        "connection": {
          "host": "mysql",
          "port": 3306,
          "user": "ghost_user",
          "password": "Ghost-123",
          "database": "ghostdb",
          "charsert":"utf8"
        }
    },
    "mail": {
      "transport": "SMTP",
      "from":"ccyuki@qq.com",
      "options": {
          "service": "QQ",
          "auth": {
              "user": "1073071892@qq.com",
              "pass": "cdhcq****vbffc"
          }
      }
    },
    "logging": {
      "transports": [
        "file",
        "stdout"
      ]
    },
    "process": "systemd",
    "paths": {
      "contentPath": "/var/lib/ghost/content"
    }
  }

配置nginx

同上一步,我外挂了nginx的配置文件:

  • nginx/config/conf.d/ghost.conf
upstream ghost-blog {
       server 19.19.6.73:2368;
}

server {  
    listen          80;
    server_name     www.ccyuki.com ccyuki.com 19.19.6.73;
    access_log      /var/log/nginx/access.log;
    error_log      /var/log/nginx/error.log;
    charset         utf-8;

    # 反向代理博客请求到服务端
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://ghost-blog;
    }
}
  • nginx/config/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    client_max_body_size 5m;
}

从docker下载安装

按照yml中的配置,下载安装镜像

cd docker-nginx-ghost-mysql
docker-compose up -d
docker images
docker ps -a

使用

  • 查看日志docker logs dockernginxghostmysql_ghost_1
  • 移除镜像docker-compose down
  • 启动镜像docker-compose start
  • 停止镜像docker-compose stop

备份

img/tar两者择一,我选择tar,因为我对docker的理解还很浅薄,两者的明显区别不是很明确,但是总有一种感觉,tar应该是包括了我的改动的,img只是原始镜像...not sure

  • 保存ghost镜像docker save 4d45f27fed83 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/ghost.img

  • 保存MySQL镜像docker save 1e4405fe1ea9 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/mysql.img

  • 保存nginx镜像docker save 8a2fb25a19f5 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/nginx.img

  • 保存ghost包docker export 235b2f00c498 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/ghost.tar

  • 保存mysql包docker export 6bbcdcdc0db6 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/mysql.tar

  • 保存nginx包docker export 0fe2017d5e42 > /home/Harvey/opt/myghost/docker-nginx-ghost-mysql/dockerImgBak/nginx.tar

  • 保存外挂的整个目录结构,因为MySQL的数据,ghost的数据,nginx的数据都在本地保存

恢复

  • 加载ngix镜像docker load nginx.img

  • 加载MySQL镜像docker load mysql.img

  • 加载ghost镜像docker load ghost.img

    • docker images
    • docker tag 8a2fb25a19f5 nginx:1.14-alpine
    • docker tag 1e4405fe1ea9 mysql:5.7
    • docker tag 4d45f27fed83 ghost:latest
    • docker run -itd nginx:1.14-alpine
    • docker run -itd mysql:5.7
    • docker run -itd ghost:latest
  • 导入nginx包docker import -docker.io/ngindx:1.14-alpine nginx.tar

  • 导入MySQL包docker import -docker.io/mysql:5.7 mysql.tar

  • 导入ghost包docker import -docker.io/ghost:latest ghost.tar

其它

  • 主题 uno-zen