旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

Docker布置Nginx+Flask+Mongo的运用引见(附代码)【MySQL教程】,mongodb,nginx,docker

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:76评论:0


导读:本篇文章给人人带来的内容是关于Docker布置Nginx+Flask+Mongo的运用引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。运用Dock...

本篇文章给人人带来的内容是关于Docker布置Nginx+Flask+Mongo的运用引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

运用Docker布置Nginx+Flask+Mongo的运用

Nginx做为服务器,Mongo为数据库支撑,Flask为Python言语的Web框架,应用Docker的容器特征,能够简朴地布置在linux服务器上

项目预备

项目重要目次以下

__ 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设置

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的内容以下

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的读写操纵
#!/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的内容以下

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://pypi.douban.com/simple/ -r requirements.txt

# 运用gunicorn启动运用
CMD gunicorn -w 4 -b 0.0.0.0:5000 run:app

/src/app/run.py的代码

此处解释了调试用的 app.run(),宣布时用gunicorn启动
from app import create_app
app = create_app('default')
app.debug=False
# if __name__ == '__main__':
#    app.run()

Nginx的设置

/nginx/Dockerfile的内容以下

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的内容以下

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;  # 宣布在阿里云上,应填写内网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;

        }
    }


}

启动布置

  1. 进入docker-flie目次 cd docker-flie
  2. 启动docker docker-compose up
  3. 检察容器状况 docker ps
  4. 当地布置浏览器输入 127.0.0.1即可

末了涌现相似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运用没法衔接mongo,本文运用link体式格局。

在数据库的设置应响应写成:

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

本篇文章到这里就已悉数完毕了,更多其他精彩内容能够关注ki4网的mongodb视频教程栏目!

以上就是Docker布置Nginx+Flask+Mongo的运用引见(附代码)的细致内容,更多请关注ki4网别的相干文章!

标签:mongodbnginxdocker


欢迎 发表评论: