If you're using the VirtualBox driver with docker-machine to run a Docker VM on your Mac, then you can forward the port from your Mac to the VM like this:
VBoxManage controlvm myvirtualmachine natpf1 'myapp,tcp,,4505,,4505'
Let's break this down:
- VBoxManage is the name of the executable that controls VirtualBox.
- controlvm tells VirtualBox that you're telling a VM (rather than the VM manager) to do something.
- myvirtualmachine is the name of your Docker VM (you can find this by running
docker-machine ls
).
- natpf1 tells the VM to create a NAT port forward. This is where the magic starts to happen.
- 'myapp,tcp,,4505,,4505' is a list of arguments to natpf1:
- myapp is a name for the port forwarding rule. It's optional.
- tcp is the type of traffic you want to forward. The other option is udp
- There is room for a host IP at this point, but you can leave it blank.
- 4505 is the port on your Mac where you want to receive the traffic. This is the port number you would give to the other hosts on your network.
- There is room for a guest IP at this point, but you can leave it blank.
- The second 4505 is the port on your Docker VM where you want to receive the traffic. You can change this to whatever you like without affecting the first port. You would just have to change the port you expose from your Docker container.
You can find the official documentation for this command at https://www.virtualbox.org/manual/ch08.html#vboxmanage-controlvm