Port forwarding in docker machine?

Since boot2docker deprecated, I switched to docker-machine , but I don't know how to open the port from docker-machine . In boot2docker I could do this:

 boot2docker ssh -L 27017:localhost:27017 

This redirects port 27017 from VirtualBox to localhost 27017 while the SSH connection is open. Please note that I am not looking for a way to constantly open a port in VirtualBox. How can I achieve this with docker-machine ?

+44
docker boot2docker docker-machine
Aug 24 '15 at 4:41
source share
6 answers

You can still access the VBoxmanage.exe command from VirtualBox used by the docker machine:

 VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port27017,tcp,,27017,,27017"; 
  • Use docker-machine info to get the name of your vm.
  • use modifyvm if vm is not already running.

See a practical example in this answer .




This is the current workaround pending the ability to pass the docker-machine ssh argument: see issue 691 .

Another workaround is not port forwarding and using the virtual machineโ€™s IP address directly:

  $(docker-machine ip default) 



As sdc commented :

You can confirm that port forwarding is configured correctly using

  VBoxManage showvminfo boot2docker-vm | grep "NIC.* Rule" 
+42
Aug 24 '15 at 5:46
source share

In the latest versions of the machine, you can simply do (where the machine name is specified by default):

 docker-machine ssh default -L 27017:localhost:27017 

This is a more temporary solution than changing the configuration of a virtual machine.

Use the following option to forward ports in the background:

 docker-machine ssh default -f -N -L 27017:localhost:27017 
  • -f Requests ssh to switch to the background just before the command is executed.
  • -N Allow an empty command (useful here only for port forwarding)
+20
Jan 02 '15 at 21:43
source share

You can send ssh to the machine and pass the normal port forwarding arguments:

 ssh docker@$(docker-machine ip default) -L 27017:localhost:27017 

The password for the docker user is tcuser. (see https://github.com/boot2docker/boot2docker )

+17
Oct 03 '15 at 19:29
source share

Since I find it difficult to remember how to do this, I created a small bash script called pf (which means "port forward"), which allows:

 $ pf 8080 

This will cause port 8080 of the docker to switch to the host port 8080 in the background (add -f so that it starts in the foreground). Use a different host port:

 $ pf 8090:8080 

which maps the host port 8090 to 8080.

To stop port forwarding, add -s :

 $ pf 8090:8080 -s 

(actually enough host port: pf 8090 -s ). There are other options available, so check out github .

+7
Apr 08 '16 at 8:20
source share

If you do not want to use passwords, I would add that you should just point to the private key.

 ssh -L 8080:localhost:8080 -i ~/.docker/machine/machines/default/id_rsa docker@$(docker-machine ip default) 
+3
Oct 27 '15 at 17:00
source share

Just to increase @VonC's answer in the script - currently, if you use the Docker Toolbox on MacOS X, the default VM machine is "default." Thus, the script to display all the exhibits from the container should look like this:

 for port in `docker port cassandra | cut -d'-' -f1`; do port_num=`echo ${port} | cut -d'/' -f1` port_type=`echo ${port} | cut -d'/' -f2` echo "Create rule natpf1 for ${port_type} port ${port_num}" VBoxManage controlvm "default" natpf1 "${port_type}-port${port_num},${port_type},,${port_num},,${port_num}" done 

If you try to execute several times, to delete an existing rule, add an instruction before creating it:

 VBoxManage controlvm "default" natpf1 delete "${port_type}-port${port_num}" 

The script assumes that you are already migrating ports from the container to the virtual machine.

 docker port cassandra 

gives a conclusion, for example:

 7000/tcp -> 0.0.0.0:7000 
+1
Jan 12 '16 at 19:50
source share



All Articles