nginx做為伺服器,mongo為資料庫支持,flask為python語言的web框架,利用docker的容器特性,可以簡單地部署在linux伺服器上
##專案準備
專案主要目錄如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | __ project-name
|__ docker-file
|__ ningx
|__ dockerfile
|__ conf
|__ nginx.conf
|__ flask
|__ dockerfile
|__ requirements.txt
|__ mongo
|__ dockerfile
|__ setup.sh
|__ docker-compose.yml
|__ src
|__ app
|__ ...
|__ run.py
|
登入後複製
簡要說明
docker-file目錄為docker部署的設定檔
src目錄為flask應用的python程式碼
docker的詳細配置
docker-compose配置
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 | version: '2.2'
services:
mongo:
build: ./mongo
volumes:
- "./mongo/db:/data/db"
restart: always
ports:
- "27017:27017"
environment:
mongo_initdb_root_username: root
mongo_initdb_root_password: 123456
flask:
build: ./flask
links:
- mongo
ports:
- "5000:5000"
expose:
- "5000"
volumes:
- ../src:/home/web
nginx:
build: ./nginx
links:
- flask
volumes:
- "./nginx/log:/ var /log/nginx"
- "../:/usr/share/nginx/html"
ports:
- "80:80"
- "8080:8080"
- "443:443"
restart: always
|
登入後複製
mongodb的配置
/mongo/dockerfile的內容如下
1 2 3 4 5 6 7 8 9 10 11 12 | from mongo:3.6
# 设置时区
env tz=asia/shanghai
run ln -snf /usr/share/zoneinfo/ $tz /etc/localtime && echo $tz > /etc/timezone
# 设置工作目录
env workdir /usr/local/work
env auto_run_dir /docker-entrypoint-initdb.d
env install_mongo_shell setup.sh
run mkdir -p $workdir
# 复制数据库的初始化命令
copy ./ $install_mongo_shell $auto_run_dir /
run chmod +x $auto_run_dir / $install_mongo_shell
|
登入後複製
/mongo/setup.sh的內容如下
該檔案的目的是,啟動mongodb後建立一個密碼為test的使用者test,並賦予它資料庫test的讀寫操作
1 2 3 4 5 6 7 8 | #!/bin/bash
mongo <<eof
use admin;
db.auth('root', '123456');
use dmx_aluminum;
db.createuser({user:'test',pwd:'test',roles:[{role:'readwrite',db:'test'}]});
db.createcollection("user");
eof
|
登入後複製
flask應用的配置
/flask/dockerfile的內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from python:3.6
# 设置时区
env tz=asia/shanghai
run ln -snf /usr/share/zoneinfo/ $tz /etc/localtime && echo $tz > /etc/timezone
# 设置工作区
run mkdir -p /home/web
workdir /home/web
# 添加依赖
add requirements.txt /home/web/requirements.txt
run pip3 install -i https:
# 使用gunicorn启动应用
cmd gunicorn -w 4 -b 0.0.0.0:5000 run:app
|
登入後複製
/src/app/run.py的程式碼
此處註解了調試用的app.run(),發佈時用gunicorn啟動
1 2 3 4 5 | from app import create_app
app = create_app(' default ')
app.debug=false
# if __name__ == '__main__':
# app.run()
|
登入後複製
nginx的設定
/nginx/dockerfile的內容如下
1 2 3 4 5 6 | from nginx:1.14
# 设置时区
env tz=asia/shanghai
run ln -snf /usr/share/zoneinfo/ $tz /etc/localtime && echo $tz > /etc/timezone
# 复制配置
copy conf/nginx.conf /etc/nginx/nginx.conf
|
登入後複製
/nignx/conf/nginx.conf的內容如下
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 65 66 | 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
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 1;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "msie [1-6]\.";
server {
listen 80;
server_name localhost;
keepalive_timeout 5;
root /usr/share/nginx/html;
location / static / {
alias /usr/share/nginx/html/src/app/ static /;
}
location / {
# checks for static file, if not found proxy to app
try_files $uri @flask_app;
}
location @flask_app {
proxy_pass http:
proxy_redirect off;
proxy_set_header x-forwarded- for $proxy_add_x_forwarded_for ;
proxy_set_header host $http_host ;
proxy_set_header x-real-ip $remote_addr ;
#proxy_buffers 8 32k;
#proxy_buffer_size 64k;
}
}
}
|
登入後複製
#啟動部署
##進入docker-flie目錄cd docker- flie 啟動docker docker-compose up #檢視容器狀態docker ps- ##本機部署瀏覽器輸入127.0.0.1即可
- 最後出現類似docker_file_nginx_1,docker_file_mongo_1, docker_file_flask_1的3個容器,說明成功! ! !
踩坑吐槽
1 mongol容器中的初始化檔需要放在docker-entrypoint-initdb.d 目錄下
#本人做過以下嘗試,會顯示mongdb未啟動。
1 2 3 | add setup.sh /data/setup.sh
run chmod +x /data/setup.sh
cmd ["/data/setup.sh"]
|
登入後複製
2 flask應用程式無法連接mongo,本文使用link方式。
在資料庫的配置應相應寫成:
1 2 3 4 5 6 7 | mongodb_settings = {
'db': 'test',
'host': 'mongo', # 127.0.0.1 host地址一定要写你配置的--link的名字
'username': 'test',
'password': 'test',
'port': 27017
}
|
登入後複製
本地測試時改回127.0.0.1
3 nginx中配置使用的代理模式,其中執行flask應用的ip,應為內網ip
#
以上是如何使用Docker部署Nginx+Flask+Mongo的應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!