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

时间:2017-09-04 09:45:41

标签: docker nginx flask dockerfile

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

我有一个需要与nginx服务器交互的Flask应用服务器。传统上我们使用充当uWSGI的Gunicorn,但是我们如何使用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')

这是我的templates / 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来构建这个容器。

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容器通信。      - &GT;每当用户点击提交按钮通知我开始处理时,我都需要通知我的应用程序容器。      - &GT;接下来,一旦处理完成,它应通知nginx服务器已完成正常处理。

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

谢谢

1 个答案:

答案 0 :(得分:0)

  1. 您应该在应用程序容器中启动Gunicorn进程。然后,这将在localhost:8000(或您选择的端口)上运行该过程。
  2. 然后,您将创建一个代理传递给您的应用程序的nginx容器。在您的服务器块中,您将拥有以下内容:

    proxy_pass http://web:8000;

  3. 运行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的this教程。即使它的Django,它很容易类似于Flask。