6

I'm creating a VirtualBox image using Packer.

Afterwards I start the VM and I'd like to use ssh to connect to it. I know how to enable port forwarding using the GUI but I prefer to automate things, so I'm looking for a way to let Packer do that for me.

I'm using NAT as the way of connecting my VM to the network.

How do I tell Packer to forward some ports to the VM?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171

2 Answers2

8

After having a look at how to enable port forwarding using VirtualBox' commandline tool VBoxManage, I came up with this configuration in my packerConfig.json:

"type": "virtualbox-iso",
"vboxmanage": [
   [ "modifyvm", "{{.Name}}", "--memory", "1024" ],
   [ "modifyvm", "{{.Name}}", "--cpus", "1" ],
   [ "modifyvm", "{{.Name}}", "--natpf1", "guest_ssh,tcp,,3022,,22" ]
 ]
...

The last part makes VirtualBox forward traffic from the host's 3022 to the guest's 22.

This means I can do ssh -p 3022 me@127.0.0.1 to connect to the VM.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
1

To create a Port forward in VirtualBox with Packer, used this:

     [
         "modifyvm", "{{.Name}}", "--natpf1", "name,tcp,ipHost,portHost,IpGuest,PortGuest"
     ]