Home Operation and Maintenance Nginx How to use Docker to deploy Nginx+Flask+Mongo applications

How to use Docker to deploy Nginx+Flask+Mongo applications

May 14, 2023 pm 04:16 PM
docker nginx flask

nginx is used as the server, mongo is the database support, and flask is the web framework of the python language. Using the container features of docker, it can be simply deployed on the Linux server

Project preparation

The main directory 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
Copy after login

Brief description

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

The src directory is the python code of the flask application

Docker’s 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
Copy after login

mongodb configuration

The contents of/mongo/dockerfile are as follows

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
Copy after login

/mongo/setup.sh The content is as follows

The purpose of this file is to create a user test with a password of test after starting mongodb, and give it the read and write operations of the database test


#!/bin/bash
mongo <<eof
use admin;
db.auth(&#39;root&#39;, &#39;123456&#39;);
use dmx_aluminum;
db.createuser({user:&#39;test&#39;,pwd:&#39;test&#39;,roles:[{role:&#39;readwrite&#39;,db:&#39;test&#39;}]});
db.createcollection("user");
eof
Copy after login

flask application The configuration

/flask/dockerfile content is as follows

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
Copy after login

/src/app/run.py code

Commented here for debugging app.run(), start with gunicorn when publishing


from app import create_app
app = create_app(&#39;default&#39;)
app.debug=false
# if __name__ == &#39;__main__&#39;:
#  app.run()
Copy after login

nginx configuration

/nginx/dockerfile content is as follows

The contents of

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
Copy after login

/nignx/conf/nginx.conf are 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 &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
           &#39;$status $body_bytes_sent "$http_referer" &#39;
           &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;

  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;

    }
  }


}
Copy after login

Start deployment

  1. Enter the docker-flie directory cd docker- flie

  2. Start docker docker-compose up

  3. View container status docker ps

  4. Local deployment Enter 127.0.0.1 in the browser

Finally, three containers similar to docker_file_nginx_1, docker_file_mongo_1, docker_file_flask_1 appear, indicating success! ! !

Trampling on pitfalls

1 The initialization file in the mongol container needs to be placed in the docker-entrypoint-initdb.d directory


I have tried the following, but it will show that mongdb has not started.

add setup.sh /data/setup.sh
run chmod +x /data/setup.sh
cmd ["/data/setup.sh"]
Copy after login

2 The flask application cannot connect to mongo. This article uses the link method.


The database configuration should be written accordingly:

mongodb_settings = {
    &#39;db&#39;: &#39;test&#39;,
    &#39;host&#39;: &#39;mongo&#39;, # 127.0.0.1 host地址一定要写你配置的--link的名字
    &#39;username&#39;: &#39;test&#39;,
    &#39;password&#39;: &#39;test&#39;,
    &#39;port&#39;: 27017
  }
Copy after login
Change back to 127.0.0.1 during local testing

3 The proxy mode used in nginx configuration, in which flask is executed The application ip should be the intranet ip


The above is the detailed content of How to use Docker to deploy Nginx+Flask+Mongo applications. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles