19

I can not request to my flask app inside docker container. It doesn't responding.

there is my flask app file:

import json
from flask import Flask, jsonify
from flask import request

from trained_model import predict
app = Flask(__name__)

@app.route("/", methods=['POST'])
def main():
    res = []

    for obj in request.json:
        item = str(obj['item'])
        print item
        predicted = predict(item)
        print predicted
    res.append({'item': item, 'correct': predicted})

    return json.dumps({'results': res})

if __name__ == "__main__":
    app.run(host='0.0.0.0')

there is my dockerfile:

FROM tensorflow/tensorflow

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Bundle app source
COPY . /usr/src/app

RUN sh docker_install.sh

EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["api.py"]

there is my docker run command:

sudo docker run -p 5000:5000 -d my-image

when I try to send post request it doesn't work.

Dmytro Nalyvaiko
  • 1,664
  • 4
  • 16
  • 27
  • Two things to add to help here: 1) however you're sending the POST request and the output from it (so we can see exactly what the error is), 2) maybe a little of the log output from your container (you never know, maybe there's a Flask misconfiguration somewhere) You could also perhaps add a simple GET route and call that, just to make sure there's nothing funny going on in parameter handling/whatever in your POST route. – otupman May 18 '17 at 10:53
  • 1) there no any suspicious log in running container 2) with GET it is also doesn't work – Dmytro Nalyvaiko May 18 '17 at 11:24
  • Possible duplicate of [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues) – emecas Mar 27 '18 at 16:16

3 Answers3

21

I've had success with flask run --host=0.0.0.0. Somehow that seems to be different than app.run(host='0.0.0.0'), but I don't know why. See https://stackoverflow.com/a/43015007/9484954

6

flask run --host=0.0.0.0 is useful in case you want to access flask server externally. I had similar kind of issue when I was running flask app inside docker container. Though I modified my flask app and added app.run(host:'0.0.0.0'), server was running but I can not access it in browser.then I ran flask server externally by providing same port o.o.o.o.

Gsk
  • 2,929
  • 5
  • 22
  • 29
Megha
  • 61
  • 1
  • 1
5

I was having the same problem. I did app.run(debug=True,host='0.0.0.0') from app.run() and it worked. Not sure what was the issue with the app.run() though

Maunish Dave
  • 359
  • 4
  • 9