Deploy applications of Nginx+Flask+Mongo using Docker
- 2021-01-18 06:46:26
- OfStack
Nginx as the server, Mongo as the database support, Flask as the Web framework for Python language, using Docker container features, can be easily deployed on linux server
Project preparation
The main project directory 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 docker deployment
The src directory is the python code for flask applications
Docker detailed configuration
docker - compose configuration
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 configuration
The contents of /mongo/Dockerfile are as follows
FROM mongo:3.6
# Set the time zone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Set up the working directory
ENV WORKDIR /usr/local/work
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
ENV INSTALL_MONGO_SHELL setup.sh
RUN mkdir -p $WORKDIR
# Copy the initialization command of the database
COPY ./$INSTALL_MONGO_SHELL $AUTO_RUN_DIR/
RUN chmod +x $AUTO_RUN_DIR/$INSTALL_MONGO_SHELL
The contents of /mongo/ setup. sh are as follows
The purpose of this file is to create a user test with password test after starting MongoDB and give it read and write operation of database 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 application configuration
The contents of /flask/Dockerfile are as follows
FROM python:3.6
# Set the time zone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Setting Workspace
RUN mkdir -p /home/web
WORKDIR /home/web
# Add the dependent
ADD requirements.txt /home/web/requirements.txt
RUN pip3 install -i https://pypi.douban.com/simple/ -r requirements.txt
# use gunicorn Start the application
CMD gunicorn -w 4 -b 0.0.0.0:5000 run:app
/ src app/run py code
The debugging app.run () is commented here and launched with gunicorn at publishing time
from app import create_app
app = create_app('default')
app.debug=False
# if __name__ == '__main__':
# app.run()
Nginx configuration
The contents of /nginx/Dockerfile are as follows
FROM nginx:1.14
# Set the time zone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Copy the configuration
COPY conf/nginx.conf /etc/nginx/nginx.conf
/ nignx conf/nginx conf content is as follows
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;
# open 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; # Release on Ali cloud, should fill in the Intranet 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 the deployment
Enter docker-flie directory cd docker-flie Start docker docker-compose up View the container status docker ps Type 127.0.0.1 in the local deployment browserdocker_file_nginx_1,docker_file_mongo_1, docker_file_flask_1, docker_file_flask_1
Hit the pit ridicule
1 The initialization file in the mongol container should be placed in the directory docker-entrypoint-initdb.d
I have tried the following and it will show that mongdb is not started.
ADD setup.sh /data/setup.sh
RUN chmod +x /data/setup.sh
CMD ["/data/setup.sh"]
2 flask application cannot connect to mongo, this article uses link mode.
The configuration in the database should be written as follows:
MONGODB_SETTINGS = {
'db': 'test',
'host': 'mongo', # 127.0.0.1 host address 1 Be sure to write what you configured --link The name of the
'username': 'test',
'password': 'test',
'port': 27017
}
Change back to 127.0.0.1 for local testing
3 Proxy mode used in nginx configuration, where IP running flask application should be Intranet IP