Using localhost for to access running container

This command below:

docker run --rm -it -p 127.0.0.1:3000:3000 -v $(pwd):/usr/src/app sanchezjjose/my-node-app

Which I am running should apparently allow me to run my application against localhost on port 3000 locally, but instead in Kitematic I see:

'Access URL' : 192.168.99.100:3000

In the docker terminal when running ‘docker ps’ I see:

127.0.0.1:3000->3000/tcp

When I go to the browser, I am not able to access my application either with 192.168.99.100:3000 OR with localhost:3000.

Is there something maybe I am missing in the documentation on why this will not work? This is my current stack:

 - Mac OSX 
 - Docker 1.8.0 
 - Kitematic 0.8.1 
 - VirtualBox 5.0.0

I also don’t have any port forwaring for port 3000 either in VirtualBox because I figured that adding the host on the run command should allow me to skip that, but I have tried with port forwarding as well and it doesn’t work either.

Thank you very much.

1 Like

Hello,

When you specify 127.0.0.1:3000:3000, you are binding to the docker host’s 127.0.0.1. Since your docker host is in a VM, that’s different from your 127.0.0.1 on your mac.

If you did docker run --rm -it -p 3000:3000 -v $(pwd):/usr/src/app sanchezjjose/my-node-app instead, it would bind to 0.0.0.0 (all interfaces). That would include the 192.168.99.100 IP, and then you’d be able to access it from your host.

/Jeff

1 Like

Thanks for your response Jeff.

So because I am using OSX, and the docker host is actually on a VM, there is no way for me to access my application via localhost:3000, unless I am actually port forwarding 3000 from my Mac OSX => VirtualBox VM running docker?

That is correct-- you could use virtualbox’s port forwarding feature. You would probably have to bind to the NAT interface’s address instead of 127.0.0.1 though: docker run -p 10.0.2.15:3000:3000 (assuming 10.0.2.15 is the IP of your VM’s interface attached to the virtualbox NAT network). The port forwarding feature is tied to that-- virtualbox won’t reach in to the 127.0.0.1 interface in the VM and redirect ports like that.

Thank you Jeff, appreciate your response. I have one other general question I posted here, just in case you are able to take a look, no worries if you can’t but your response was very helpful here.

Thanks to this post: http://stackoverflow.com/questions/25327012/access-docker-from-external-machine-in-network

I was able to use [homebrew installed] nginx to forward the ports from my containers within vbox (on os x) to my own ip address so that other machines on my network could reach my containers

I replaced the default port 8080 forward in the default nginx.conf with this

server {
listen 8080;

server_name localhost; 

proxy_redirect off;                                                       
proxy_buffering off;                                                      
proxy_set_header Host $host;                                              
proxy_set_header X-Real-IP $remote_addr;                                  
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              

location / {                                                              
    proxy_pass http://192.168.99.100:8080;                                            
}                                                                         

}

to be clear: this is a workaround, what I’d like to be able to do is tell Docker Toolbox [vbox] to get an ip address via dhcp on my network – then I’d not have to forward ports around…

Cool workaround, thanks for sharing!

Hello, I’ve exactly the same question. I’m running docker on my Mac OS and executing my container with without specifying the IP of my host

docker run -i -p 3000:3000
-e “GF_SERVER_ROOT_URL=http://0.0.0.0”
-e "GF_SECURITY_ADMIN_PASSWORD=secret
grafana/grafana

ad10bbb0d7ef grafana/grafana “/run.sh” 7 minutes ago Up 7 minutes 0.0.0.0:3000->3000/tcp grafana

It’s up and running and accessible by VM’s network IP, not my Mac IP:
http://192.168.99.100:3000/login

Please advise, I just need it to be accessible by using my host’s ip.

1 Like

Hi,

In case someone is having the same issue, I was able to resolve by putting:

-p 3000:3000

in the docker run command, and putting:

-b 0.0.0.0

in my rails server command. Thanks.

I’ve found a solution. In my case am using docker-machine which in turns manages docker-machine running in Virtual Box. Simply select your Docker VM in Virtual Box, open settings > Networking > Advanced > Port forwarding.

Create a new entry and enter the details as follows:

  • Name: web
  • Protocol: TCP
  • Host IP: 127.0.0.1
  • Host Port: 80
  • Guest IP:
  • Guest Port: 80

Click OK. No need to restart. You can access your docker app on localhost:80 immediately

1 Like

Article explains it very well

Troubleshooting Port Forwarding in VirtualBox