Reputation: 871
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
Upvotes: 4
Views: 27672
Reputation: 1558
The answer of OSError [Errno 99] - python applies here also.
If it works using the ip address but not using hostname.
Removing double localhost in /etc/hosts
should be solution. hosts file should look something like this (mapping ip to hostname)
127.0.0.1 localhost
127.0.1.1 your_hostname_here
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Answer by @'Artsiom Praneuski' is only concerned with Docker configuration, which is all relevant in docker container setup but does not point to Python environment fix(both container and normal setup).
Upvotes: 0
Reputation: 2319
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.
Upvotes: 3