How do I forward a docker-machine port to my host port on OSX?
At this time Docker Machine is a virtual machine running under VirtualBox in your machine, so to expose your application port you need to map your virtual machine port to your host port.
To achieve this there are two options, but before make sure your Docker Machine is stopped running:
docker-machine stop default # see PS below if docker machine isn't default
Option 1 - Use the VirtualBox interface
- Open VirtualBox Manager
- Select your Docker Machine VirtualBox image (e.g.: default)
- Open Settings -> Network -> Advanced -> Port Forward
- Add your app name, the desired host port (e.g.: 80) and your Guest port (e.g.: 3000)
Option 2 - Use the VirtualBox command line
Just run the following command with your own parameters:
VBoxManage modifyvm "dev" --natpf1 "myapp,tcp,,80,,3000"
Final considerations
Now you can start your Docker Machine running:
docker-machine start default
eval $(docker-machine env default)
Then just start your application Docker container and test it running http://localhost/.
P.S.: Your Docker Machine name may not be default, in this case change the name accordingly.
If you are trying to run the bulletinboard example using the following ports
docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0
On macOs you can open VirtualBox and then right-click on the machine --> Settings --> Network --> Advanced --> Port Forwarding
If you add the following rule
Then you should be able to access the application using
> http://localhost:8100/
This can be achieved with ssh port forwarding:
ssh -L 0.0.0.0:80:localhost:3000 docker@$(docker-machine ip)
It will ask you for the docker
user's password, which should be tcuser
.
If your docker-machine instance is not named "default" then you'll have to specify its name in there like
ssh -L 0.0.0.0:80:localhost:3000 docker@$(docker-machine ip <name>)