0

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.

pystart
  • 3
  • 1
  • what is the output of running `docker ps -a` on the host system? – Z4-tier Mar 05 '20 at 20:10
  • I’d expect the combination of (a) including the `app.run(host='0.0.0.0')` parameter and (b) pointing at the `docker-machine ip` address to work. Have you tried this specific pairing? – David Maze Mar 05 '20 at 20:12
  • You also might use the Docker Desktop application (`brew cask install docker`). You still need to change the `app.run()` call but then you should be able to access `http://localhost:5000` from your host browser. – David Maze Mar 05 '20 at 20:13
  • 2 other things: 1) your build command won't work, the syntax isn't valid (use `docker build -t app .`). 2) your requirements.txt file is called `requirementx.txt` in the Dockerfile, so that isn't going to work either. – Z4-tier Mar 05 '20 at 20:16
  • Ah, thanks Z4-tier! The bug was the *misspelling of requirements.txt in Dockerfile*. the *flask app* in docker build was a typographical error on my part as I was posting my question. For some reason, the image was being generated even though there was no file called *requirementx.txt*. – pystart Mar 05 '20 at 22:37

0 Answers0