Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker nginx container not receiving request from outside, connection refused

I have a running nginx container: # docker run --name mynginx1 -P -d nginx;

And got its PORT info by docker ps: 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp

Then I could get response from within the container(id: c30991a04b2f):

  • docker exec -i -t c3099 bash

  • curl http://localhost => which return the default index.html page content, it works

However, when I make the curl http://localhost:32769 outside of the container, I got this:

curl: (7) failed to connect to localhost port 32769: Connection refused


I am running on a mac with docker version 1.9.0; nginx latest

Does anyone know what cause this? Any help? thank you

like image 202
user3035623 Avatar asked Nov 14 '15 10:11

user3035623


1 Answers

If you are On OSX, you are probably using a VirtualBox VM for your docker environment.

Make sure you have forwarded your port 32769 to your actual host (the mac), in order for that port to be visible from localhost.
This is valid for the old boot2docker, or the new docker machine.

VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port32769  ,tcp,,32769,,32769"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port32769 ,udp,,32769,,$32769

(controlvm if the VM is running, modifyvm is the VM is stopped)
(replace "boot2docker-vm" b ythe name of your vm: see docker-machine ls)

I would recommend to not use -P, but a static port mapping -p xxx:80 -p yyy:443.
That way, you can do that port forwarding once, using fixed values.

Of course, you can access the VM directly through docker-machine ip vmname

curl http://$(docker-machine ip vmname):32769
like image 79
VonC Avatar answered Sep 20 '22 20:09

VonC