22

I’m delivering a private docker container in my company and want my colleagues to be able to access in our internal network, the problem is that my guest OS is OSX and as so I can only access my application using the 192.168.99.100:3000 default ip from docker machine.

How can I forward the docker-machine 3000 port to my host 80 port?

Felipe Plets
  • 7,230
  • 3
  • 36
  • 60
Juliano Pacheco
  • 521
  • 1
  • 5
  • 13

4 Answers4

32

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.

Felipe Plets
  • 7,230
  • 3
  • 36
  • 60
  • 1
    Out of all the answers I explored, this is the one that helped me. +1 – Tagc Dec 08 '16 at 15:55
  • 3
    It is not necessary to run `docker-machine stop` first. This may have changed since the answer was posted, but it has been true for at least the last few months. – toddkaufmann Mar 27 '17 at 01:17
  • Is there not a way to get any/all ports mapped to the Docker Host in the VirtualBox? Seems a lot of hassle to setup a new port binding each time! – Ian Vaughan Jul 03 '17 at 11:22
  • @IanVaughan my understanding is that with NAT you can't forward all ports while you can do that using a BRIDGE interface. You can find more information on VirtualBox manual https://www.virtualbox.org/manual/ch06.html#network_bridged – Felipe Plets Jul 03 '17 at 11:36
  • 1
    Be aware to map to host port < 1024 you need to run Virtual Box as root – Alex Dvoretsky Jan 30 '18 at 14:48
  • 1
    What are `dev` and `myapp` herre? How do they relate to the `default` VM ? – user2490003 Oct 11 '19 at 03:32
  • 2
    Figured it out - `"dev"` should be `"default"` to match the name of the VM and `myapp` is any name you want to assign to this new port forwarding rule. Generally a good name is to name it the name of your application. – user2490003 Oct 11 '19 at 03:36
  • Also keep in mind that you MUST write the rule parameter with this format: [],tcp|udp,[],,[],] – Jackie Degl'Innocenti Mar 14 '21 at 11:10
2

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>)
mziwisky
  • 335
  • 1
  • 12
1

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

enter image description here

Then you should be able to access the application using

> http://localhost:8100/
codejunkie
  • 908
  • 2
  • 21
  • 34
0

docker-machine uses VM underneath, usually VirtualBox.

You can find the IP address of that machine by:

docker-machine ip

You can access that IP directly:

docker run -p 8080:8080 apache --name hello
curl $(docker-machine ip):8080/index.html

Unfortunately that IP address is not permanent (could change after VBox restarts): port forwarding to localhost could make it permanent. You have stop VM and configure VM:

VBoxManage modifyvm "default" --natpf1 "myapp,tcp,,80,,3000"
gavenkoa
  • 45,285
  • 19
  • 251
  • 303