7

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

user3035623
  • 183
  • 2
  • 7

3 Answers3

5

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, it helps.! I also read that on another post just now. I just started learning docker, still got a lots of docs to go through. – user3035623 Nov 14 '15 at 11:40
2

Solved.. I misunderstood how docker port mapping works. Since I'm using mac, the host for nginx container is a VM, 0.0.0.0:32769->80/tcp maps the port 80 of the container to the port 32769 of the VM.


solution:

  1. docker-machine ip vm-name => 192.168.99.xx

  2. curl http://192.168.99.xx:32769

user3035623
  • 183
  • 2
  • 7
  • Also, you can run "docker-machine ls" to see the IP of your 'default'. For example, the VM IP in this case is 192.168.99.100: > docker-machine ls NAME ACTIVE URL STATE URL SWARM DOCKER ERRORS default * virtualbox Running tcp://192.168.99.100:2376 v1.9.1 – Lionel Jan 31 '16 at 12:41
2

Not exactly answers for your question but spend some time trying to figure out similar thing in context of "why is my docker container not connecting to elastic search localhost:9200" and this was the first S.O. question that pops up, so I hope it helps some other googling person

if you are linking containers together (e.g. docker run --rm --name web2 --link db:db training/webapp env)

... then Dockers adds enviroment variables:

DB_NAME=/web2/db DB_PORT=tcp://172.17.0.5:5432 DB_PORT_5432_TCP=tcp://172.17.0.5:5432 DB_PORT_5432_TCP_PROTO=tcp DB_PORT_5432_TCP_PORT=5432 DB_PORT_5432_TCP_ADDR=172.17.0.5

... and also updates your /etc/hosts

# /etc/hosts
#...
172.17.0.9  db

so you can technically connect to ping db

https://docs.docker.com/v1.8/userguide/dockerlinks/

so for elastic search is

 # /etc/hosts
 # ...
 172.17.0.28    elasticsearch f9db83d0dfb5 ecs-awseb-qa-3Pobblecom-env-f7yq6jhmpm-10-elasticsearch-fcbfe5e2b685d0984a00

so wget elasticseach:9200 will work

equivalent8
  • 13,754
  • 8
  • 81
  • 109