2

I am running a flask application inside a docker container. I would like to access the containers port 8000 * Running on http://127.0.0.1:8000/

from my host machine.

I am running the docker container via

docker run -p 127.0.0.1:8080:8000 --rm $(IMAGE_NAME) /Stocks/startserver.sh

When I do docker ps I see the following text

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
85168e4c963e        stocks              "/Stocks/startserver…"   12 seconds ago      Up 11 seconds       127.0.0.1:8080->8000/tcp   beautiful_bartik

When I open localhost:8080 on my local machine I see nothing. From my understanding the port is open, any idea why I am not able to connect to it from my host machine? Any tips on how to debug a solution for this?

Shivangi Singh
  • 1,001
  • 2
  • 11
  • 20
  • 2
    Leave out the localhost ip like `-p 8080:8000` instead of `-p 127.0.0.1:8080:8000` and try it again. – jdickel Dec 11 '20 at 06:16
  • The "running on 127.0.0.1" part suggests the Flask application isn't configured to listen on the 0.0.0.0 "all interfaces" address, which means it won't be reachable from outside the container; [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) describes this more. – David Maze Dec 11 '20 at 12:29
  • @jdickel I already tried that. – Shivangi Singh Dec 11 '20 at 16:01

1 Answers1

2

Running on http://127.0.0.1:8000/

If that's the flask server's console output, then it sounds like you're launching it incorrectly within the container. This looks like Flask is running on 127.0.0.1 (localhost) inside the container. It needs to run on the container's external interface!

Acheive this by launching with:

flask run -h 0.0.0.0

Or if using the (outdated) method, app.run within the app, pass it a host argument:

if __name__ == '__main__':
    app.run(host='0.0.0.0',
        # Any other launch args you have
        )

0.0.0.0 is special address which means "all interfaces".

I'd also take @jdickel's advice, and omit 127.0.0.1 from the run command.

v25
  • 7,096
  • 2
  • 20
  • 36
  • Doing this invalidated my CSS import. I tried doing a relative path and an absolute path. I get this following error. "GET /Stocks/css/style.css HTTP/1.1" 404 -" I have imported it like this. – Shivangi Singh Dec 11 '20 at 16:17