Cannot browse dockerized web app from other comput

2019-09-18 21:42发布

问题:

I would like any computer on the same network as my Mac to be able to access the dockerized Rails web app running on my Mac.

On my Mac (10.9.5) my Rails 4.2.4 web app is running fine in Docker when I access it via the docker ip and the port I assigned, 192.168.99.100:3000

Docker 1.8.1 is running under Virtualbox 5.0.2.

I'm using Docker-compose and the relevant part of the docker-compose.yml file is:

web:
  build: .
  command: 'bash -c ''bundle exec unicorn -p $PORT -c ./config/unicorn-local.rb'''
  working_dir: /app/user
  env_file:
    - .docker_dev_env_config
    - .docker_dev_env_personal
  environment:
    PORT: 3000
    DATABASE_URL: 'postgres://postgres:@herokuPostgresql:5432/postgres'
  ports:
    - '3000:3000'
  links:
    - herokuPostgresql

and my Dockerfile is

FROM heroku-ruby2.0.0 # a local image based on heroku/ruby with ruby 2.0.0
EXPOSE 3000
ENV widget foo

My Mac's ip address on the local network is always set to 192.168.0.33.

How do I permit testers on the local network to access the running dockerized app via 192.168.0.33:3000 ?

(FWIW if I run my web app under Vagrant - instead of Docker - other testers on the network can access the web app by browsing 192.168.0.33:3000. My Vagrant is also running under Virtualbox. My Vagrantfile contains config.vm.network :forwarded_port, guest: 3000, host: 3000 but I do not see any equivalent for docker-compose.yml)

回答1:

You need to port forward the port 3000 from the VM to your localhost. For this you can run the following script

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$3000,tcp,,$3000,,$3000";
  VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$3000,udp,,$3000,,$3000";

This will port forward to your localhost. In case you want to port forward to some other interface, you have to provide that interface's IP address in the command. This is the general syntax for the --natpf command

[--natpf<1-N> [<rulename>],tcp|udp,[<hostip>],<hostport>,[<guestip>],<guestport>]

So you can modify it accordingly. Refer to this for further reading. Docker on Mac. The Missing Guide



回答2:

The 'default' Virtualbox created when using docker-toolkit's Docker Quickstart Terminal app does not have any port other than an ssh defined.

The solution on the Mac, when using VirtualBox with Docker is:

  • Run VirtualBox
  • click the 'default' box (or whichever box your Docker setup uses)
  • go to 'Settings > Network > Adapter 1
  • click 'Port Forwarding'
  • create a new rule the example below fwds 3000 to 3000 (be sure to leave the host ip column blank)