socket.error:[errno 99] cannot assign requested address : flask and python

16,826

To let other computers on the LAN connect to your service just use 0.0.0.0 address in app.run() function and expose desired port from your docker container to your host PC.

To expose port you need to

1) specify EXPOSE directive in Dockerfile

2) run container with -p <port_on_host>:<port_in_container> parameter.

For example:

Dockerfile:

FROM ubuntu:17.10

RUN apt-get update && apt-get install -y apache2

EXPOSE 80

ENTRYPOINT ["/usr/sbin/apache2ctl"]
CMD ["-D", "FOREGROUND"]

Build:

docker build -t image_name .

Run:

docker run -d -p 80:80 image_name

Check:

curl http://localhost

P.S. make sure that 80 port is not used by another app on your host PC before running container. If this port is already in use - specify another port, for example 8080:

docker run -d -p 8080:80 image_name

And then check:

curl http://localhost:8080

Docs are here.

Share:
16,826
dhinar1991
Author by

dhinar1991

:)

Updated on June 04, 2022

Comments

  • dhinar1991
    dhinar1991 almost 2 years

    I am having the same problem like here and here

    I am Trying to run a flask app inside docker container.It works fine with '0.0.0.0' but it throws error with my ip address

    I am behind a corporate proxy. When i checked my ip address with ipconfig, it showed IP Address as : 10.***.**.** And I am using docker toolbox where my container ip is 172.17.0.2 and VM IP Address is 192.168.99.100.

    I have a flask app running inside this docker with host

    if __name__ == "__main__":
        app.run(host= '0.0.0.0')
    

    works fine. But when i change it to my ip address

    if __name__ == "__main__":
            app.run(host= '10.***.**')
    

    throws error :

    socket.error:[errno 99] cannot assign requested address

    I checked again the ip address with a simple flask application which is running on local (i.e without docker)

     if __name__ == "__main__":
                app.run(host= '10.***.**')
    

    It worked fine.

    So the problem is coming only when running inside the docker. And that's because i am behind a router running NAT with internal ip address. And how do i find this internal ip address with NAT? I have already done port forwarding for flask application with port 5000.

    > iptables -t nat -A DOCKER -p tcp --dport 5000 -j DNAT --to-destination 172.17.0.2:5000
    > iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source 172.17.0.2 --destination 172.17.0.2 --dport https
    > iptables -A DOCKER -j ACCEPT -p tcp --destination 172.17.0.2 --dport https
    
    • Artsiom Praneuski
      Artsiom Praneuski about 6 years
      Looks like such action is incompatible with docker container network driver (docs.docker.com/network/#network-drivers). Try to change it. And secondly, why do you need to specify some IP address instead of 0.0.0.0 that means "listen all interfaces" ? So you can use 0.0.0.0 if you need to gain access to your app on the LAN, and use 127.0.0.1 to gain access to your service from localhost only.
    • dhinar1991
      dhinar1991 about 6 years
      I want other computers to connect to this api . Is there a way i can do this with '0.0.0.0' or with ''127.0.0.1?
    • Artsiom Praneuski
      Artsiom Praneuski about 6 years
      Yes, to let other computers on the LAN connect to your service just use 0.0.0.0 address in app.run() function and expose desired port from your docker container to your host PC. To expose port you need to 1) specify EXPOSE directive in Dockerfile and 2) run container with -p <port_on_host>:<port_in_container>. Here you can find simple example: docs.docker.com/engine/reference/builder/#expose Let me know if I can post this comment as an answer for your question.
    • dhinar1991
      dhinar1991 about 6 years
      I got it. Thank you