flask应用程序正在docker上运行,但浏览器显示无法访问该网站

时间:2018-12-16 20:01:26

标签: flask deployment

我是docker的新手,我已经使用flask开发了一个应用程序,并且运行良好。当我尝试在docker上部署该应用程序时,控制台说它正在http://127.0.0.1:5000/上运行,但是当我尝试登录时在浏览器上看到此地址后,它显示“该网站无法访问”

这是我的Dockerfile:

FROM python:3.6

RUN mkdir /web WORKDIR /web ADD . /web/ RUN pip install -r requirements.txt


ENV FLASK_ENV="docker" EXPOSE 5000 CMD ["python", "mongo.py"]

我的dockercompose.yaml文件:

version: '3'
services:
 # Define the Flask web application
 flaskapp:

   # Build the Dockerfile that is in the web directory
   build: ./web

   # Always restart the container regardless of the exit status; try and restart the container indefinitely
   restart: always

   # Expose port 8000 to other containers (not to the host of the machine)
   expose:
     - "8000"

   # Mount the web directory within the container at /home/flask/app/web
  #  volumes:
  #    - ./web:/homvole/flask/app/web

   # Don't create this container until the redis and mongo containers (below) have been created
   depends_on:
     - redis
     - mongo

   # Link the redis and mongo containers together so that they can talk to one another
   links:
     - redis
     - mongo

   # Pass environment variables to the flask container (this debug level lets you see more useful information)
   environment:
     FLASK_DEBUG: 1

   # Deploy with 3 replicas in the case of failure of one of the containers (only in Docker Swarm)
   deploy:
     mode: replicated
     replicas: 3

 # Define the redis Docker container
 redis:

   # use the redis:alpine image: https://hub.docker.com/_/redis/
   image: redis:alpine
   restart: always
   deploy:
     mode: replicated
     replicas: 3

 # Define the redis NGINX forward proxy container


 # Define the mongo database
 mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root

 mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root

   # Expose port 5432 to other Docker containers
    expose:
      - "8081"

我的mongo.py文件:

from flask import Flask, jsonify, request
#from flask.ext.pymongo import PyMongo
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'databasename'
#app.config['MONGO_URI'] = 'mongodb://username:password@hostname:port/databasename'
app.config['MONGO_URI'] = 'mongodb://root:root@localhost:27017/databasename'

mongo = PyMongo(app)

@app.route('/framework/', methods=['GET'])
def get_all_frameworks():
    framework = mongo.db.framework 

    output = []

    for q in framework.find():
        output.append({'name' : q['name'], 'language' : q['language']})

    return jsonify({'result' : output})

@app.route('/framework/find/<name>', methods=['GET'])
def get_one_framework(name):
    framework = mongo.db.framework

    q = framework.find_one({'name' : name})

    if q:
        output = {'name' : q['name'], 'language' : q['language']}
    else:
        output = 'No results found'

    return jsonify({'result' : output})

@app.route('/framework/', methods=['POST'])
def add_framework():
    framework = mongo.db.framework 

    name = request.json['name']
    language = request.json['language']

    framework_id = framework.insert({'name' : name, 'language' : language})
    new_framework = framework.find_one({'_id' : framework_id})

    output = {'name' : new_framework['name'], 'language' : new_framework['language']}

    return jsonify({'result' : output})


@app.route('/framework/update/', methods=['POST'])
def update_framework():
    framework = mongo.db.framework
    name = request.json['name']
    language = request.json['language']
    myquery = { "name": name }
    newvalues = { "$set": {"language":language } }

    q=framework.update_one(myquery, newvalues)
    if q:
        output = {'name' : q['name'], 'language' : q['language']}
    else:
        output = 'No results found'

    return jsonify({'result' : output})

@app.route('/framework/delete/<name>', methods=['GET'])
def delete_one_framework(name):
    framework = mongo.db.framework
    #name = request.json['name']
    myquery = { "name": name }
    q=framework.delete_one(myquery)
    if q:
        output = 'element deleted successfully '
    else:
        output = 'No results found'

    return jsonify({'result' : output})


if __name__ == '__main__':
    app.run(debug=False)
    app.run(threaded=True) 
    app.run(host='0.0.0.0')

要部署我的应用,我使用了以下命令:

docker build -t moapp4。

docker run -p 5000:5000 moapp4

这表明:

Serving Flask app "mongo" (lazy loading)  
 Environment: docker 
 Debug mode: off  
 Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

0 个答案:

没有答案