Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Docker deploys Nginx+Flask+Mongo

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to deploy Docker Nginx+Flask+Mongo, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Deploy Nginx+Flask+Mongo applications using Docker

Nginx is the server, Mongo is the database support, and Flask is the Web framework of Python language. Using the container feature of Docker, it can be simply deployed on the linux server.

Project preparation

The main catalogue of the project is as follows

_ _ project-name | _ _ docker-file | _ _ ningx | _ _ Dockerfile | _ _ conf | _ _ nginx.conf | _ _ flask | _ _ Dockerfile | _ _ requirements.txt | _ _ mongo | _ _ Dockerfile | _ _ setup.sh | _ _ docker-compose.yml | _ _ src | | _ _ app | _ _... | _ _ run.py |

Brief description

The docker-file directory is the configuration file for the docker deployment

The src directory is the python code of the flask application

Detailed configuration of Docker

Docker-compose configuration

Version: '2.2'services: mongo: build:. / mongo volumes:-". / mongo/db:/data/db" restart: always ports:-"27017 flask 27017" environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: 123456 flask: build:. / flask links:-mongo ports:-"5000 build" 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 nginx links 8080" -443 restart: always

Configuration of MongoDB

/ mongo/Dockerfile is as follows

FROM mongo:3.6# sets the time zone ENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezone# sets the working directory ENV WORKDIR / usr/local/workENV AUTO_RUN_DIR/ docker-entrypoint-initdb.dENV INSTALL_MONGO_SHELL setup.shRUN mkdir-p $WORKDIR# initialization command for replication database COPY. / $INSTALL_MONGO_SHELL $AUTO_RUN_DIR/RUN chmod + x $AUTO_RUN_DIR/$INSTALL_MONGO_SHELL

/ mongo/setup.sh is as follows

The purpose of this file is to create a user test with password test after starting MongoDB, and give it the read and write operation of database test #! / bin/bashmongo / etc/timezone# replication configuration COPY conf/nginx.conf / etc/nginx/nginx.conf

/ nignx/conf/nginx.conf is as follows

User nginx;worker_processes 1 errorists 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 # enable 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; # is published on Aliyun. You should enter the private network 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;}} start deployment to enter docker-flie directory cd docker-flie launch docker docker-compose up to view container status docker ps local deployment browser enter 127.0.0.1

Finally, there are 3 containers like docker_file_nginx_1,docker_file_mongo_1 and docker_file_flask_1, indicating success!

Step on the pit to complain that the initialization files in the 1 mongol container need to be placed in the docker-entrypoint-initdb.d directory

I have made the following attempt, which will show that mongdb is not started.

ADD setup.sh / data/setup.shRUN chmod + x / data/setup.shCMD ["/ data/setup.sh"] 2 flask applications cannot connect to mongo. This article uses link.

The configuration in the database should be written as follows:

MONGODB_SETTINGS = {'db':' test', 'host':' mongo', # 127.0.0.1 host address must be written in your configuration-- link's name 'username':' test', 'password':' test', 'port': 27017}

Change it back to 127.0.0.1 when testing locally

3. The proxy mode used in nginx configuration, in which the IP executing flask application should be private network IP

These are all the contents of the article "how Docker deploys Nginx+Flask+Mongo". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report