docker nginx container not receiving request from

2020-02-01 02:39发布

问题:

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

回答1:

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


回答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



回答3:

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