Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 583
dhinar1991 Avatar asked Oct 26 '25 06:10

dhinar1991


1 Answers

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.

like image 165
Artsiom Praneuski Avatar answered Oct 27 '25 18:10

Artsiom Praneuski