3

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?

3
  • 2
    Leave out the localhost ip like -p 8080:8000 instead of -p 127.0.0.1:8080:8000 and try it again. Commented Dec 11, 2020 at 6: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 describes this more. Commented Dec 11, 2020 at 12:29
  • @jdickel I already tried that. Commented Dec 11, 2020 at 16:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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. <link rel="stylesheet" type="text/css" href='/Stocks/css/style.css'>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.