服務(wù)器
Nginx做為服務(wù)器,Mongo為數(shù)據(jù)庫支持,F(xiàn)lask為Python語言的Web框架,利用Docker的容器特性,可以簡單地部署在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
簡要說明
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è)置時區(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ù)庫的初始化命令 COPY ./$INSTALL_MONGO_SHELL $AUTO_RUN_DIR/ RUN chmod +x $AUTO_RUN_DIR/$INSTALL_MONGO_SHELL
/mongo/setup.sh的內(nèi)容如下
該文件的目的是,啟動MongoDB后創(chuàng)建一個密碼為test的用戶test,并賦予它數(shù)據(jù)庫test的讀寫操作
#!/bin/bash mongo <Flask應(yīng)用的配置
/flask/Dockerfile的內(nèi)容如下
FROM python:3.6 # 設(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啟動應(yīng)用 CMD gunicorn -w 4 -b 0.0.0.0:5000 run:app/src/app/run.py的代碼
此處注釋了調(diào)試用的 app.run(),發(fā)布時用gunicorn啟動
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è)置時區(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; # 開啟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)填寫內(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; } } }啟動部署
進(jìn)入docker-flie目錄 cd docker-flie 啟動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個容器,說明成功?。?!
踩坑吐槽
1 mongol容器中的初始化文件需要放在 docker-entrypoint-initdb.d 目錄下
本人做過如下嘗試,會顯示 mongdb未啟動。
ADD setup.sh /data/setup.sh RUN chmod +x /data/setup.sh CMD [/data/setup.sh]2 flask應(yīng)用無法連接mongo,本文使用link方式。
在數(shù)據(jù)庫的配置應(yīng)相應(yīng)寫成:
MONGODB_SETTINGS = { \'db\': \'test\', \'host\': \'mongo\', # 127.0.0.1 host地址一定要寫你配置的--link的名字 \'username\': \'test\', \'password\': \'test\', \'port\': 27017 }本地測試時改回127.0.0.1
3 nginx中配置使用的代理模式,其中執(zhí)行flask應(yīng)用的IP,應(yīng)為內(nèi)網(wǎng)IP
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞名稱:使用Docker部署Nginx+Flask+Mongo的應(yīng)用
網(wǎng)站地址:http://weahome.cn/article/cgcogs.html