I'm fairly new to docker and flask. As a test case, I am trying to dockerize a flask app that returns "hello world" in a browser on MacOS. I have the following files - app.py, requirements.txt, Dockerfile. The contents of the script and files are given below.
app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "hello world!"
if __name__ == '__main__':
app.run(debug=True)
requirements.txt
Flask==1.0.3
Dockerfile
FROM python:3.7.2-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirementx.txt
CMD ["python", "app.py"]
I followed the standard procedure of dockerizing the flask app :
docker build -t flask app:latest .
docker run -d -p 5000:5000 flask app
Based on a previous issue that was reported on StackOverflow, I looked at the solution provided in following link : Docker - Failed to connect to localhost port 4000: Connection refused
curl http://$(docker-machine ip default):5000
This generated the following error message:
curl: (52) Empty reply from server
I even followed the suggestion by changing app.run()
to app.run(host='0.0.0.0')
since I want the container to be accessible from outside as mentioned in this link : Deploying a minimal flask app in docker - server connection issues
I ran the command : curl -i http://0.0.0.0:5000
which also generated the same error message -
curl: (52) Empty reply from server
I even tried : curl -H Host:0.0.0.0:5000 -i http://app:5000
which gave me the following error message:
curl: (6) Could not resolve host: app
Docker was installed and tested with the following set of commands and everything ran smoothly:
$ brew install docker docker-machine
$ brew cask install virtualbox
$ docker-machine create --driver virtualbox default
$ docker-machine env default
$ eval "$(docker-machine env default)"
$ docker run hello-world
This is an extremely simple code. But I'm not sure why I'm running into these issues over here. Any help would be greatly appreciated.