Docker Community Forums

Share and learn in the Docker community.

Why are exposed ports not open on host?


(Littlesandra88) #1

If I set up a webserver like so

docker run --name landing -v /docker/landing:/usr/share/nginx/html:ro -d -p 10000:80 nginx

which have the ip 172.17.0.2 and do

# curl -i http://172.17.0.2:10000            
curl: (7) Failed to connect to 172.17.0.2 port 10000: Connection refused
root@8343dfde24fd:/# 

If I access the public IP of the host with example.com:10000 when it works.

Is there a way to make the ports available on the Docker host and the other containers without linking them?


(Jeff Anderson) #2

Hello,

the -p 10000:80 option will set up a port forward. port 10000 on the host will forward to port 80 on the container. The 172.17.0.2 ip address looks like it is the ip address of the container, not the host. I would expect that you can use port 10000 on any host ip address, including the host’s 127.0.0.1/localhost.

Normally if you want to access a container’s service from the host or outside the host, you will want to use the port publishing feature, and connect to the published port, not the container’s IP.

If you want to connect to a container from another container, --link helps make it easy to resolve the ip and figure out what port to make the connection on.

Additionally, if you have containers connected to a docker network create custom network, they can find eachother by name.

For example:

docker network create test
docker run --name webtest --net=test -d nginx:alpine
docker run --rm -it --net=test alpine ping -c 1 webtest
PING webtest (172.22.0.2): 56 data bytes
64 bytes from 172.22.0.2: seq=0 ttl=64 time=0.142 ms

--- webtest ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.142/0.142/0.142 ms

(Littlesandra88) #3

Thanks a lot. localhost:10000 works perfectly =)

Your net example is very interesting! That is just what I am looking for the nginx reverse proxy I am trying for set up =)