11

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)

6 Answers 6

15

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/

2
  • 3
    This! After countless searches and attempts to fix, reinstall, routing changes, trying with direct IP... This was the solution, and no one is mentioning it anywhere... Apr 18, 2020 at 22:23
  • Finally! In my case it was starting my app in docker container with vue-cli-service serve --host 0.0.0.0 Feb 7, 2021 at 21:58
9

I just changed the order of the name parameter and it worked (in Linux).

docker run -d -p 5000:5000 --name simple-flask-app simple-flask-app
2
  • 2
    This solved the problem for me. I had the -p xxxx:xxxx at the end. Jul 20, 2018 at 8:45
  • ARGH why the @#$@# does this make a difference!
    – eddiewould
    Jan 6, 2020 at 20:06
9

For me, the simplest solution was to run:

docker-machine ip default

This prints an IP address. In my case, this IP is not shown by ipconfig. I tested with Docker 18.03.0-ce on a Windows 7 Host.

I found this tip here: https://www.jhipster.tech/tips/020_tip_using_docker_containers_as_localhost_on_mac_and_windows.html

1
  • Yep, run docker-machine ip then use provided ip instead of a localhost: http://<docker-machine ip>:<exposed port>
    – klapshin
    Feb 20, 2019 at 8:26
2

Short Solution

  1. Run your docker app 1.1 (Exec into your docker and make sure your app is working as expected)
  2. On your windows host

    ipconfig

  3. One of those exposed IPv4 addresses will work. Just try all one by one.

    curl http://IPCONFIG-IPv4-TRY-OUT:PORT -> 200 OK + Flask is running

-> Docker on windows does not forward traffic to the docker container as known from linux/mac.

-> It looks like traffic is forwared to eth0.

1

You need to connect to the VM where docker is actually running:

Find the ip address by running docker-machine ip default and use that ip to connect:

curl http://<ip-address>:5000
0
0

Not related to flask but I had the same problem recently. Our host machine needed a static IP address so some network configuration was changed by our IT. I then noticed the vEthernet adapter in Control Panel\Network and Internet\Network Connections was disabled. After enabling this port forwarding to the container instance seemed to work again.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.