简体   繁体   中英

Using Docker : How to warp a web application to communicate with a nginx web server

I am new to using Docker. I want to rebuild my monolithic application using micro-services architecture approach.

I have a Flask application server that needs to interact with nginx server. Traditionally we use Gunicorn that acts as uWSGI, but how can we do the same using Docker??

Below is my code,

I have a Flask application, that asks the user to upload an excel file

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')

Here is my 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>

Next, Here is my Dockerfile to build this container.

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"] 

Next, I have my nginx server that acts as a reverse proxy.

I am stuck how to proceed from here. :(

My Questions are :

1) How should I wrap my application server to ensure that it communicates with the nginx container. -> I need to inform my application container whenever user clicks the submit button informing it to start processing. -> Next, once the processing is done it should inform the nginx server that ok processing is done.

2) Should I copy the index.html into /var/www/nginx/html ??

Thank you

  1. You should start a Gunicorn process in your application container. This will then run the process on localhost:8000 (or your port of choice).
  2. Then you'll create an nginx container that will proxy pass to your application. In your server block youll have something like this:

    proxy_pass http://web:8000;

Its probably easier to run a docker-compose process.

Heres a snippet of the Dockerfile I have for my web service:

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"]

Heres a snippet from a docker-compose file I have for my flask app:

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

I really liked this tutorial for getting started with docker. Even though its Django, its easily analogous to Flask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM