Dokku 项目部署

一、系统环境安装

1. Nginx

安装最新版本 nginx

如果已经安装,请先卸载

sudo apt-get remove nginx

安装方法:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:nginx/stable 
sudo apt-get update
sudo apt-get install nginx

查看nginx 版本

nginx -v
nginx version: nginx/1.12.0

安装完成

2. 安装 Docker 与 Dokku

# install docker

wget -nv -O - https://get.docker.com/ | sh

# setup dokku apt repository
wget -nv -O - https://packagecloud.io/gpg.key | apt-key add -

export SOURCE="https://packagecloud.io/dokku/dokku/ubuntu/"

export OS_ID="$(lsb_release -cs 2> /dev/null || echo "trusty")"

echo "utopicvividwilyxenialyakketyzestyartfulbionic" | grep -q "$OS_ID" || OS_ID="trusty"

echo "deb $SOURCE $OS_ID main" | tee /etc/apt/sources.list.d/dokku.list

apt-get update

# install dokku

apt-get install dokku

dokku plugin:install-dependencies --core # run with root!

# go to your server's IP and follow the web installer

3. 安装 docker-compose

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

4. 创建数据库

cd /home/.database
docker-compose docker-compose.yml

docker-compose.yml 中编排了数据库的配置信息

# mysql-db:
mysql_db:
  image: mysql:5.7
  volumes:
    - /home/.database/vrlab_db:/var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_DATABASE=cx
  ports:
    - "3306:3306"
  command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
  expose:
    - "3306"

二、创建并发布应用

1. 用 dokku 命令创建应用

dokku apps:create vrlab

2. 绑定域名

dokku domains:add vrlab vrlab.domain.com
dokku domains:add vrlab vrlab.domain1.net

3. 本地项目部署发布

1) 配置本地 host

这里主要是为了提交代码方便做一个 host 映射
将远程 dokku 服务器的 ip 映射到本地 dokku.me,可以是任何域名

124.124.124.124     dokku.me

2) 配置 Dokku 容器的 SSHKey

如未配置 sshkey 将没有权限提交本地的项目源码

cat ~/.ssh/id_rsa.pub | ssh root@dokku.me "sudo sshcommand acl-add dokku progrium"

Git 中添加 dokku 推送源

git remote add dokku dokku@dokku.me:vrlab

推送部署
dokku 推送的源码必需是 master 分支

git push dokku master

服务器返回如下结尾的信息,即部署成功(例:)

=====> Renaming container (2545498d0122) quirky_beaver to vrlab.web.1
-----> Attempting to run scripts.dokku.postdeploy from app.json (if defined)
=====> Application deployed:
       http://vrlab.domain.net
       http://vrlab.domain1.com

To vrlab.dokku.me:vrlab
 * [new branch]      master -> master

三、常用命令

列表应用

dokku apps:list

创建应用

# on the Dokku host
dokku apps:create app-sample

销毁应用

# on the Dokku host
apps:destroy app-sample

重构

# on the Dokku host
dokku ps:rebuild app-sample

四、文件目录说明

1. docker 应用中的静态文件映射

服务器本地永久存储映射,主要用于上传的静态文件

静态文件目录
/var/lib/dokku/data/storage/vrlab/www
映射到 nginx 反向代理的静态路径
ln -s /var/lib/dokku/data/storage/vrlab/www /home/static/vrlab/www

挂载永久存储的映射命令

# we use a subdirectory inside of the host directory to scope it to just the app
dokku storage:mount vrlab /var/lib/dokku/data/storage/vrlab/www:/home/node/app/

2 nginx 配置文件

安装路径
/etc/nginx
导入配置
/etc/nginx/conf.d
现有配置文件

.
├── dokku.conf
├── dokku-installer.conf.bak
├── server_names_hash_bucket_size.conf
└── static_sites.conf