您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何使用Docker部署Nginx+Flask+Mongo的應(yīng)用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
nginx做為服務(wù)器,mongo為數(shù)據(jù)庫(kù)支持,flask為python語(yǔ)言的web框架,利用docker的容器特性,可以簡(jiǎn)單地部署在linux服務(wù)器上
項(xiàng)目準(zhǔn)備
項(xiàng)目主要目錄如下
__ project-name |__ docker-file |__ ningx |__ dockerfile |__ conf |__ nginx.conf |__ flask |__ dockerfile |__ requirements.txt |__ mongo |__ dockerfile |__ setup.sh |__ docker-compose.yml |__ src |__ app |__ ... |__ run.py
簡(jiǎn)要說(shuō)明
docker-file目錄為docker部署的配置文件
src目錄為flask應(yīng)用的python代碼
docker的詳細(xì)配置
docker-compose配置
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的內(nèi)容如下
from mongo:3.6 # 設(shè)置時(shí)區(qū) env tz=asia/shanghai run ln -snf /usr/share/zoneinfo/$tz /etc/localtime && echo $tz > /etc/timezone # 設(shè)置工作目錄 env workdir /usr/local/work env auto_run_dir /docker-entrypoint-initdb.d env install_mongo_shell setup.sh run mkdir -p $workdir # 復(fù)制數(shù)據(jù)庫(kù)的初始化命令 copy ./$install_mongo_shell $auto_run_dir/ run chmod +x $auto_run_dir/$install_mongo_shell
/mongo/setup.sh的內(nèi)容如下
該文件的目的是,啟動(dòng)mongodb后創(chuàng)建一個(gè)密碼為test的用戶test,并賦予它數(shù)據(jù)庫(kù)test的讀寫(xiě)操作
#!/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應(yīng)用的配置
/flask/dockerfile的內(nèi)容如下
from python:3.6 # 設(shè)置時(shí)區(qū) env tz=asia/shanghai run ln -snf /usr/share/zoneinfo/$tz /etc/localtime && echo $tz > /etc/timezone # 設(shè)置工作區(qū) run mkdir -p /home/web workdir /home/web # 添加依賴 add requirements.txt /home/web/requirements.txt run pip3 install -i https://pypi.douban.com/simple/ -r requirements.txt # 使用gunicorn啟動(dòng)應(yīng)用 cmd gunicorn -w 4 -b 0.0.0.0:5000 run:app
/src/app/run.py的代碼
此處注釋了調(diào)試用的 app.run(),發(fā)布時(shí)用gunicorn啟動(dòng)
from app import create_app app = create_app('default') app.debug=false # if __name__ == '__main__': # app.run()
nginx的配置
/nginx/dockerfile的內(nèi)容如下
from nginx:1.14 # 設(shè)置時(shí)區(qū) env tz=asia/shanghai run ln -snf /usr/share/zoneinfo/$tz /etc/localtime && echo $tz > /etc/timezone # 復(fù)制配置 copy conf/nginx.conf /etc/nginx/nginx.conf
/nignx/conf/nginx.conf的內(nèi)容如下
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; # 開(kāi)啟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://192.168.0.2:5000; # 發(fā)布在阿里云上,應(yīng)填寫(xiě)內(nèi)網(wǎng)ip 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; } } }
啟動(dòng)部署
進(jìn)入docker-flie目錄 cd docker-flie
啟動(dòng)docker docker-compose up
查看容器狀態(tài) docker ps
本地部署瀏覽器輸入 127.0.0.1即可
最后出現(xiàn)類似docker_file_nginx_1,docker_file_mongo_1, docker_file_flask_1的3個(gè)容器,說(shuō)明成功?。?!
踩坑吐槽
1 mongol容器中的初始化文件需要放在 docker-entrypoint-initdb.d 目錄下
本人做過(guò)如下嘗試,會(huì)顯示 mongdb未啟動(dòng)。
add setup.sh /data/setup.sh run chmod +x /data/setup.sh cmd ["/data/setup.sh"]
2 flask應(yīng)用無(wú)法連接mongo,本文使用link方式。
在數(shù)據(jù)庫(kù)的配置應(yīng)相應(yīng)寫(xiě)成:
mongodb_settings = { 'db': 'test', 'host': 'mongo', # 127.0.0.1 host地址一定要寫(xiě)你配置的--link的名字 'username': 'test', 'password': 'test', 'port': 27017 }
本地測(cè)試時(shí)改回127.0.0.1
3 nginx中配置使用的代理模式,其中執(zhí)行flask應(yīng)用的ip,應(yīng)為內(nèi)網(wǎng)ip
“如何使用Docker部署Nginx+Flask+Mongo的應(yīng)用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。