0

I've just started learning docker and I'm trying to run a very simple flask server in a container. Whenever I run it, it works just fine. However, when I access the website, chrome displays this:

This page isn’t working
127.0.0.1 didn’t send any data.
ERR_EMPTY_RESPONSE

Here is my code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

if __name__ == "__main__":
    app.run(debug=True)

The Dockerfile:

FROM python:3

RUN mkdir /test
WORKDIR /test
RUN pip3 install flask

COPY . .
EXPOSE 5000

cmd ["python3", "application.py"]

And the commands I'm using to build the image and run the container:

docker build . -t test
docker start -p 5000:5000 test

Could someone help me figure this out?

Serket
  • 3,785
  • 3
  • 14
  • 45

2 Answers2

5

localhost inside the container is different from localhost outside the container.

Change

app.run(debug=True)

to

app.run(debug=True, host='0.0.0.0')
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
1

Try exposing port 80

EXPOSE 80

And start the container as root:

sudo docker start -p 80:80 test
r3dapple
  • 431
  • 5
  • 16
  • I'm on windows (sadly) – Serket Feb 05 '21 at 22:15
  • Flask uses port 5000 as it's development server. I ran the server on my computer (not through a container) and it worked – Serket Feb 05 '21 at 22:16
  • I see. Well its hard to tell whats wrong then. Maybe full path on the python3 command? Log into the container and see if everything runs correctly: docker exec -it /bin/bash – r3dapple Feb 05 '21 at 22:18
  • What do you mean? If it helps, I tried running a webserver using Go (golang:latest) and it didn't work as well (same problem). When I tried running the golang server on my static (lan) ip address (not 127.0.0.1 or localhost) the container just exited right after starting. – Serket Feb 05 '21 at 22:19
  • I meant running cmd ["/usr/bin/python3", "application.py"] – r3dapple Feb 05 '21 at 22:21
  • when I do that, this happens:File "application.py", line 1, in from flask import Flask ModuleNotFoundError: No module named 'flask' – Serket Feb 05 '21 at 22:23
  • Just so you know, the server runs successfully in the container (I get the usual flask output telling me the URL its running on, etc.) The problem is that I am unable to access said URL from my main machine. – Serket Feb 05 '21 at 22:25
  • Hm. Are you calling the website with port 5000 in the url? "localhost:5000" ? – r3dapple Feb 05 '21 at 22:26
  • yes i am ______ – Serket Feb 05 '21 at 22:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228327/discussion-between-r3dapple-and-serket). – r3dapple Feb 05 '21 at 22:27