相关链接
下面通参考Debian Package Installation Notes改编成
Playbook
文件.因为Dokku
是要与Docker
搭配使的.上面部分安装了docker
环境.所以在下面省去了.
服务端安装
快速安装
1 | # for debian systems, installs dokku via apt-get |
使用ansible
安装
1 | # 参考位置 http://dokku.viewdocs.io/dokku/getting-started/install/debian/ 改编的文件 |
- 安装完成后会启动一个
http-server
提供 web 方式的配置,浏览器输入服务器 ip 打开页面,输入公钥和域名(或者公网 IP)就 OK.
客户端安装
dokku-client 这个客户端很久没有更新的了,不支持 python3.
官方客户端,这里基于不同语言有比较全面的客户端介绍.
这里使用官方的提供的客户端脚本,如下面所示,运行脚本前,先要确定几个重点,
- 添加
DOKKU_HOST
环境变里,或者直接写入$HOME/.dokku/contrib/dokku_client.sh
这个脚本里面. - 能使用公钥登录目标
dokku
服务器.
- 添加
在
dokku
服务器端加入一个可信的客户端公钥,操作如下:1
2
3
4
5
6
7
8
9# 列出本机加入的可信公钥
~$ dokku ssh-keys:list
SHA256:Lr2N48k+1UDb0IxxWm0AhI0fzpdpnEZtalGc+XXXXX NAME="admin1" SSHCOMMAND_ALLOWED_KEYS="no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding"
# 添加一个客户端的公钥
~$ sudo dokku ssh-keys:add admin2 /fullpath/id_rsa.pub
SHA256:diDyzHYBmugZoGkWw1RaqzGkLfdCUpmBOxXFnf/XXXX
~$ dokku ssh-keys:list
SHA256:Lr2N48k+1UDb0IxxWm0AhI0fzpdpnEZtalGc+Pf7Rvo NAME="admin1" SSHCOMMAND_ALLOWED_KEYS="no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding"
SHA256:diDyzHYBmugZoGkWw1RaqzGkLfdCUpmBOxXFnf/XXXX NAME="admin2" SSHCOMMAND_ALLOWED_KEYS="no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding"下载客户端仓库.
1
2
3
4
5
6
7
8
9~$ git clone git@github.com:dokku/dokku.git ~/.dokku
# optional: make sure that the dokku_client.sh version matches your Dokku version
~$ cd ~/.dokku
~$ git checkout <tag/branch>
# add the following to either your
# .bashrc, .bash_profile, or .profile file
~$ alias dokku='$HOME/.dokku/contrib/dokku_client.sh'正常的客户端运行如下,与
heroku
的客户端命令极其相似.如果运行dokku
无任何输出,你要查看SSH
连接信息是否正确,DOKKU_PORT,DOKKU_HOST
的的环境变量是否与目标服务器匹配.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64~$ dokku
Usage: dokku [--quiet|--trace|--rm-container|--rm|--force] COMMAND <app> [command-specific-options]
Primary help options, type "dokku COMMAND:help" for more details, or dokku help --all to see all commands.
Commands:
apps Manage Dokku apps
certs Manage Dokku apps SSL (TLS) certs
checks Manage zero-downtime settings
config Manages global and app-specific config vars
docker-options Pass options to Docker the various stages of an app
domains Manage vhost domains used by the Dokku proxy
enter Connect to a specific app container
events Show the last events (-t follows)
git Manages the git integration for an app
help Print the list of commands
logs Output app logs
network Manages network settings for an app
nginx Interact with Dokku\'s Nginx proxy
proxy Manage the proxy used by dokku on a per app
ps List processes running in app container(s)
repo Runs commands that interact with the app\'s repo
run Run a command in a new container using the current application image
scheduler-docker-local Manages the scheduler-docker-local integration for an app
shell Spawn dokku shell
ssh-keys Manage public ssh keys that are allowed to connect to Dokku
storage Mount local volume / directories inside containers
tags List all app image tags
tar Deploy applications via tarball instead of git
trace Enable dokku tracing
url Show the first URL for an application (compatibility)
urls Show all URLs for an application
version Print dokku\'s version
~$ dokku plugin:list
! Deprecated: Please use plugin:list
plugn: 0.3.0
00_dokku-standard 0.14.1 enabled dokku core standard plugin
20_events 0.14.1 enabled dokku core events logging plugin
app-json 0.14.1 enabled dokku core app-json plugin
apps 0.14.1 enabled dokku core apps plugin
build-env 0.14.1 enabled dokku core build-env plugin
certs 0.14.1 enabled dokku core certificate management plugin
checks 0.14.1 enabled dokku core checks plugin
common 0.14.1 enabled dokku core common plugin
config 0.14.1 enabled dokku core config plugin
docker-options 0.14.1 enabled dokku core docker-options plugin
domains 0.14.1 enabled dokku core domains plugin
enter 0.14.1 enabled dokku core enter plugin
git 0.14.1 enabled dokku core git plugin
logs 0.14.1 enabled dokku core logs plugin
network 0.14.1 enabled dokku core network plugin
nginx-vhosts 0.14.1 enabled dokku core nginx-vhosts plugin
plugin 0.14.1 enabled dokku core plugin plugin
proxy 0.14.1 enabled dokku core proxy plugin
ps 0.14.1 enabled dokku core ps plugin
repo 0.14.1 enabled dokku core repo plugin
scheduler-docker-local 0.14.1 enabled dokku core scheduler-docker-local plugin
shell 0.14.1 enabled dokku core shell plugin
ssh-keys 0.14.1 enabled dokku core ssh-keys plugin
storage 0.14.1 enabled dokku core storage plugin
tags 0.14.1 enabled dokku core tags plugin
tar 0.14.1 enabled dokku core tar plugin
部署Django
工程实例
下面的环境基于
Pyenv+Pipenv
.Pyenv: python 版本管理器.Pipenv: python 包管理器,更好用的 pip.pyenv的一些基本操作.
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
36
37
38
39
40
41
42
43
44
45# pyenv 安装的位置
~$ which pyenv
/home/lcy/.pyenv/bin/pyenv
# pyenv 可提供的安装版本.
~$ pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
[...]
# 本机已经安装的版本
~$ pyenv versions
system
* 3.6.6 (set by /home/lcy/.python-version)
3.6.6/envs/dokku-py3
3.6.6/envs/py3dev
dokku-py3
py3dev
# 本机默认使用的版本.
~$ pyenv version
3.6.6 (set by /home/lcy/.python-version)
# 为这个版本安装pipenv
~$ pip install pipenv
Collecting pipenv
# 列出当前安装的包.
~$ pip list
Package Version
---------------- ----------
autopep8 1.4
certifi 2018.11.29
pip 18.1
pipenv 2018.11.26
pycodestyle 2.4.0
setuptools 39.0.1
virtualenv 16.2.0
virtualenv-clone 0.4.0
yapf 0.23.0通过上述命令安装了pipenv,下面是它的一些基本操作.官方文档
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53# 创建python3的虚拟环境,也可以用 pipenv --python 3.7 指定具体的版本号.
~$ pipenv --three
Creating a virtualenv for this project…
Pipfile: /home/lcy/workspace/dokku-test/crm/Pipfile
Using /home/lcy/.pyenv/versions/3.6.6/bin/python3.6 (3.6.6) to create virtualenv…
⠸ Creating virtual environment...Already using interpreter /home/lcy/.pyenv/versions/3.6.6/bin/python3.6
Using base prefix '/home/lcy/.pyenv/versions/3.6.6'
New python executable in /home/lcy/.local/share/virtualenvs/crm-GMqHkluo/bin/python3.6
Also creating executable in /home/lcy/.local/share/virtualenvs/crm-GMqHkluo/bin/python
Installing setuptools, pip, wheel...
done.
✔ Successfully created virtual environment!
Virtualenv location: /home/lcy/.local/share/virtualenvs/crm-GMqHkluo
Creating a Pipfile for this project…
# 查看环境位置.
~$ pipenv --venv
/home/lcy/.local/share/virtualenvs/crm-GMqHkluo
~$ pipenv --py
/home/lcy/.local/share/virtualenvs/crm-GMqHkluo/bin/python
# 安装django依赖包.当前工程目录下的Pipfile.lock文件,就等同于传统的requirements.txt文件.
~$ pipenv install django django-toolbelt
Installing django…
Adding django to Pipfile\'s [packages]…
✔ Installation Succeeded
Installing django-toolbelt…
Adding django-toolbelt to Pipfile\'s [packages]…
✔ Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (c14d8c)!
Installing dependencies from Pipfile.lock (c14d8c)…
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 8/8 — 00:00:01
To activate this project\'s virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
# 列出安装包的的依赖.
~$ pipenv graph
django-toolbelt==0.0.1
- dj-database-url [required: Any, installed: 0.5.0]
- dj-static [required: Any, installed: 0.0.6]
- static3 [required: Any, installed: 0.7.0]
- django [required: Any, installed: 2.1.5]
- pytz [required: Any, installed: 2018.9]
- gunicorn [required: Any, installed: 19.9.0]
- psycopg2 [required: Any, installed: 2.7.6.1]
# 进入pipenv 虚拟环境shell
~$ pipenv shell
Launching subshell in virtual environment…
. /home/lcy/.local/share/virtualenvs/cms-uhqZcysp/bin/activate
创建Django
的示例项目.
1 | ~$ mkdir dokkutest && cd dokkutest |
Dokku
其它指南
- 设置
APP
域名.如上面所述,app 映射了一个很大端口值http://172.16.10.100:51903
,只能通过 IP+端口的方式访问,这有可能不能满足我们的实际应用需求,怎样在同一个IP里使用不同的域名访问不同的应用呢?或者说,同一个服务器里的应用都想通过80端口对外提供服务.这里就要通过nginx
代理不同的域名.命令如下:
1 | ~$ dokku domains:help |
- 为 dokkutest 这个应用设置 test.example.com 这个域名,后面就可以通过个域名 80 端就能访问到这个应用了.
1 | ~$ dokku domains:test.example.com dokkutest |
- 访问静态资源.其实上面的部署是一个基本的应用,做一个
Restful API
服务器是没有问题的.如果做成网页端,它的static
下的静态资源是不能访问的.这里要用到一些其它包与设置.具体参照这里 Django and Static Assets.具体解决如下:
1 | # 安装whitenoise |
Dokku
插件应用
letsencrypt
1 | # 安装 |
- 为
APP
安装Lets'Encrypt
,这里需要注意,这个app
设置了三个域名,其中只有一个是在 DNS 上设置的h5.lcy.wiki
,所以才会出现下面的错误.只要把其它两个域名从这个app
中删除就可以了.
1 | ~$ dokku letsencrypt h5dev |
redis
插件应用
1 | ~$ sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis |
- 连接 redis 与容器应用服务
1 | ~$ dokku redis:link dokku-redis yfh5 |
- 添加到
Django
项目中用作Sessiong Cache
,修改settings.py
的内容如下:
1 |
|
设置 Settings.py
- How to deploy Django on Dokku
- Dokku Environment Variables
- 因为
Django
开发测试需要与数据库连接,且开发环境与生产(测试)部署的环境是不一样的,这里可以通过在settings.py
判断系统环境变理来做出一些自动化.上面那个链接很有参考意.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57# We should get this from the environment, never store them in git.
# 为了安全建义不要SECRET_KEY直接写入settings.py,它会保存在git上面记录.
# 可以使用 dokku config:set appname SECRET_KEY='xxxxxx'
SECRET_KEY = os.environ.get("SECRET_KEY", 'secret')
# Set DEBUG to False if the NODEBUG env var has been set.
DEBUG = True if os.environ.get("NODEBUG") is None else False
# Set the allowed hosts based on the environment.
ALLOWED_HOSTS = ["web", "localhost"] if os.environ.get("NODEBUG") is None else [".yourdomain.com"]
if os.environ.get("IN_DOCKER"):
# Stuff for when running in Docker-compose.
# 使用Docker-compose会有IN_DOCKER这个环境变量
CELERY_BROKER_URL = 'redis://redis:6379/1'
CELERY_RESULT_BACKEND = 'redis://redis:6379/1'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': "postgres",
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': "db",
'PORT': 5432,
}
}
elif os.environ.get("DATABASE_URL"):
# Stuff for when running in Dokku.
# Parse the DATABASE_URL env var.
regex = "^postgres://(?P<username>.*?)\:(?P<password>.*?)\@(?P<host>.*?)\:(?P<port>\d+)\/(?P<db>.*?)$"
USER, PASSWORD, HOST, PORT, NAME = re.match(regex, os.environ.get("DATABASE_URL", "")).groups()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': NAME,
'USER': USER,
'PASSWORD': PASSWORD,
'HOST': HOST,
'PORT': int(PORT),
}
}
CELERY_BROKER_URL = os.environ.get("REDIS_URL", "") + "/1"
CELERY_RESULT_BACKEND = os.environ.get("REDIS_URL", "") + "/1"
else:
# Stuff for when running locally.
CELERY_TASK_ALWAYS_EAGER = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Mysql
插件应用
1 | ~$ sudo dokku plugin:install https://github.com/dokku/dokku-mysql.git mysql |
Persistent Storage
- 参考链接
- 这里需要在主机上先建一个目录,供给容器内挂载,官方推荐是
/var/lib/dokku/data/storage
,这里可以是其它文件系统目录如:cephfs,nfs
等. 关于volume
与主机共享数据使用,也可以参考http://dokku.viewdocs.io/dokku/advanced-usage/docker-options/
.如:dokku docker-options:add node-js-app deploy,run "-v /var/log/node-js-app:/app/logs"
1 | ~$ sudo dokku plugin:install storage |
常见的错误
1 | ~$ -----> Python app detected |
- 如果出现上述错误,请重新安装.
与 WebPack 集成
- django-webpack-loader
- 用 django-webpack-loader 实现 Django 和 Webpack 的绑定
- package.json 文件
- vue+django+webpack 搭建
- Angular 6|5 Tutorial: Integrating Angular with Django
谢谢支持
- 微信二维码:
- 联系作者