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.
To run the app outside of the VS Code debugger, use the following steps from a terminal: Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .
To change the host and port that the Python flask command uses, we can use the -h flag to change the host and the -p flag to change the port. to run our flask app with the host set to localhost and the port set to 3000.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With