Machine: Automatically configure Virtualbox port forwarding when using boot2docker

Created on 1 Jun 2017  路  1Comment  路  Source: docker/machine

When using the new Docker for Windows, vpnkit is used to automatically forward ports from localhost to the containers running in the Hyper-V VM:
https://github.com/moby/vpnkit/blob/master/docs/ports.md

This means it's possible to connect to services on eg: localhost:8000 (like is possible when the Docker host OS is Linux) rather than $(docker-machine ip):8000. This is which is more convenient, since:

  • It means a consistent experience across platforms (ie: avoids having to add platform-specific documentation for developer/contributor onboarding)
  • The IP can change (unlike localhost), so requires workarounds to either get the IP into the container (eg for Django ALLOWED_HOSTS) or else manual updates to C:\Windows\System32\Drivers\etc\hosts (until we have #1792).

However when using Docker Machine with boot2docker (that uses Virtualbox rather than Hyper-V), port forwarding isn't automatically set up - so users have to do so manually using:

  • VBoxManage modifyvm "VM name" --natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000" (if the machine is powered down)
  • VBoxManage controlvm "VM name" --natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000" (if the machine is running)

As such, it would be great if Docker Machine / the Docker daemon / vpnkit automatically ran the controlvm command when containers that declared ports were started.

(Ideally I would use the new Docker for Windows, however doing so isn't very practical yet, since it requires Hyper-V which prevents all other Virtualbox VMs from working, and so would require continual reboots when switching between projects that use Docker and eg Vagrant with Virtualbox.)

Most helpful comment

if a controlvm not a modifyvm, remove the hypen. otherwize it errors with invalid parameter.

- VBoxManage controlvm "VM name" --natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000"
+ VBoxManage controlvm "VM name" natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000"

reference: https://www.virtualbox.org/ticket/10122#comment:2

>All comments

if a controlvm not a modifyvm, remove the hypen. otherwize it errors with invalid parameter.

- VBoxManage controlvm "VM name" --natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000"
+ VBoxManage controlvm "VM name" natpf1 "tcp-port8000,tcp,127.0.0.1,8000,,8000"

reference: https://www.virtualbox.org/ticket/10122#comment:2

Was this page helpful?
0 / 5 - 0 ratings