使用Docker:如何扭曲Web应用程序与nginx Web服务器进行通信

我是使用Docker的新手。 我想用微服务架构方法重build我的单一应用程序。

我有一个需要与nginx服务器交互的Flask应用程序服务器。 传统上我们使用Gunicorn作为uWSGI,但是我们怎么能用Docker做同样的事?

以下是我的代码,

我有一个Flask应用程序,要求用户上传一个Excel文件

from flask import Flask, request, render_template import os app = Flask(__name__) default_key = '1' app.config["UPLOAD_FOLDER"] = "/app" @app.route('/', methods=['GET', 'POST']) def mainpage(): if request.method == 'POST': print request.form if request.method == 'POST' and request.form['submit'] == 'Check Results' : #TODO: copy the file into named volume f = request.files['file'] filename = f.filename print os.getcwd() print os.listdir(os.getcwd()) file1 = os.path.join(app.config['UPLOAD_FOLDER'], filename) f.save(file1) #TODO: ping the Classifier container return render_template('index.html') #def receive_classifier_info(): #TODO: the file has been received so succesfully display the message. #pass if __name__ == '__main__': app.run(host='0.0.0.0') 

这是我的模板/ index.html

 <html> <head> <title>key value lookup service</title> </head> <body> <form method="POST" enctype = "multipart/form-data"> <br> <h3>Select an input file</h3> <input type="file" name="file" value="Browse"> <br> <h3>Insert a pic of the sample format</h3> <br> <input type="submit" name="submit" value="Check Results"> </form> </body> </html> 

接下来,这是我的Dockerfile来构build这个容器。

 FROM python:2.7 RUN pip install Flask==0.11.1 RUN useradd -ms /bin/bash admin COPY app /app WORKDIR /app RUN chown -R admin:admin /app RUN chmod 755 /app USER admin CMD ["python", "app.py"] 

接下来,我有我的nginx服务器充当反向代理。

我坚持如何从这里开始。 🙁

我的问题是:

1)我应该如何包装我的应用程序服务器,以确保它与nginx容器通信。 – >我需要通知我的应用程序容器,每当用户点击提交button通知它开始处理。 接下来,一旦处理完成,它应该通知nginx服务器确定处理完成。

2)我应该将index.html复制到/ var / www / nginx / html吗?

谢谢

  1. 您应该在您的应用程序容器中启动一个Gunicorn进程。 这将在localhost:8000 (或您select的端口)上运行该进程。
  2. 然后你将创build一个nginx容器,将代理传递给你的应用程序。 在你的服务器块你会有这样的东西:

    proxy_pass http://web:8000;

它可能更容易运行一个docker-compose进程。

下面是我的web服务的Dockerfile代码片段:

 FROM ubuntu:16.04 # Update OS RUN sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list RUN apt-get update RUN apt-get -y upgrade # Install Python RUN apt-get install -y python3 python3-pip python3-dev COPY . / # Install app requirements RUN pip3 install -r requirements.txt # Set the default directory for our environment ENV HOME / ENV PYTHONPATH=`$PWD`/.. WORKDIR / ENTRYPOINT ["/usr/local/bin/gunicorn"] 

下面是我的烧瓶应用程序的docker-compose文件的代码片段:

 version: '2.2' services: web: restart: always build: . ports: - "8000:8000" command: ["app:app", "-w 4", "-t 90", "--log-level=debug", "-b 0.0.0.0:8000", "--reload"] nginx: restart: always image: nginx:1.13.1 ports: - "80:80" - "443:443" depends_on: - web links: - web:web 

我真的很喜欢这个教程来开始使用docker。 尽pipe它的Django,它很容易类似于Flask。