Shivangi Singh
Shivangi Singh

Reputation: 1139

Connecting to a docker localhost port from my local machine

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?

Upvotes: 3

Views: 2695

Answers (1)

v25
v25

Reputation: 7656

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.

Upvotes: 2

Related Questions