Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows + Docker + Port not exposed/reachable

Problem:

From the windows host machine. Port mapping/exposing does not work.

Container app is not reachable.

curl http://localhost:PORT (127.0.0.1 / 0.0.0.0)
-> Could not get any response

curl http://DOCKER-IP:PORT (127.17.0.1 / 127.17.0.2)
-> Could not get any response

curl http://DOCKER-SUBNET-ADDRESS:PORT (10.0.75.0)
-> Could not get any response

Setup:

Windows10 host system + Docker container (with simple helloworld python flask app)

Docker container is running. Inside the docker container the flask app behaves as expected.

Reproduction:

  1. get the hello world repo

    git clone https://github.com/shekhargulati/python-flask-docker-hello-world

  2. Build and run docker container

    docker build -t simple-flask-app:latest . docker run -d -p 5000:5000 simple-flask-app --name simple-flask-app

  3. exec into container & check flask is running

    docker exec -it simple-flask-app apt-get install curl curl http://127.0.0.1:5000

    -> See 200 OK + Flask is running (or some text like this)

  4. Exit docker container

    exit

  5. curl on your windows host machine

    curl http://127.0.0.1:5000

    -> See Could not get any response (or something like this)

like image 584
Benedikt Schmeitz Avatar asked Jan 16 '18 09:01

Benedikt Schmeitz


1 Answers

You need to set the app to run on host='0.0.0.0'. That is:

app.run(host='0.0.0.0')

So when you run:

> docker run -d -p 5000:5000 simple-flask-app --name simple-flask-app
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat

Then curl http://localhost:5000 works.

Credit goes to: https://codefresh.io/docker-tutorial/hello-whale-getting-started-docker-flask/

like image 147
Yogi Valani Avatar answered Sep 19 '22 06:09

Yogi Valani