Can I run multiple instances of the same box?

6,503 views
Skip to first unread message

mikehale

unread,
Mar 12, 2010, 10:24:01 AM3/12/10
to Vagrant
I'd like to be able to start up multiple images of the same box each
with a different Vagrantfile. Is that possible? How would one do this?

Mitchell Hashimoto

unread,
Mar 12, 2010, 12:46:08 PM3/12/10
to vagra...@googlegroups.com
Mike,

This is certainly possible. Each time you run "vagrant up," it creates a copy of the box specific to that project (that Vagrantfile). By creating multiple Vagrantfiles in multiple directories and pointing them to the same base, you'll create multiple instances of the same box.

The only thing to watch out for, and its something that will be addressed in the next version (0.2) of Vagrant, is to make sure that the ports don't collide with each other. By default SSH is set to forward to port 2222 on the host machine. This can be changed in the Vagrantfile like so, where 1234 in the following example is what port to forward to on your host machine:

config.vm.forward_port("ssh", 22, 1234)

If you're forwarding any other ports (http, ftp, etc.) you'll want to be careful of those as well. In the next release of  Vagrant, we'll actually build in port collision detection, but for now this must be watched manually.

Mitchell

mikehale

unread,
Mar 12, 2010, 1:53:57 PM3/12/10
to Vagrant
Thanks for the reply. Initially I had copied my project directory
"vagrant" to "vagrant2" and then when I ran `vagrant up` from vagrant2
I was seeing "Vagrant: VM already created. Starting VM if its not
already running...". I realize now that this was because I was using
the same .vagrant file. Here are the steps I took to resolve my issue:

# First instance:
mkdir vagrant
cd vagrant
vagrant init
vagrant up

# Second instance:
cd ..
cp -r vagrant vagrant2
rm .vagrant
vagrant up

On Mar 12, 12:46 pm, Mitchell Hashimoto <mitchell.hashim...@gmail.com>
wrote:


> Mike,
>
> This is certainly possible. Each time you run "vagrant up," it creates a
> copy of the box specific to that project (that Vagrantfile). By creating
> multiple Vagrantfiles in multiple directories and pointing them to the same
> base, you'll create multiple instances of the same box.
>
> The only thing to watch out for, and its something that will be addressed in
> the next version (0.2) of Vagrant, is to make sure that the ports don't
> collide with each other. By default SSH is set to forward to port 2222 on
> the host machine. This can be changed in the Vagrantfile like so, where 1234
> in the following example is what port to forward to on your host machine:
>
> config.vm.forward_port("ssh", 22, 1234)
>
> If you're forwarding any other ports (http, ftp, etc.) you'll want to be
> careful of those as well. In the next release of  Vagrant, we'll actually
> build in port collision detection, but for now this must be watched
> manually.
>
> Mitchell
>

Mitchell Hashimoto

unread,
Mar 12, 2010, 1:55:54 PM3/12/10
to vagra...@googlegroups.com
Mike,

Ah, yes. Vagrant uses that ".vagrant" file purposefully so that project directories can be moved around with little consequence, since it would be quite annoying to move a project directory and somehow "lose" your virtual environment! Of course, a `vagrant reload` would have to be done to update the shared folder but thats the gist of it.

I'm glad you got around it.

Mitchell

Александр Губернаторов

unread,
Nov 18, 2015, 3:25:40 PM11/18/15
to Vagrant, mi...@hales.ws
The best way is to use single Vagrantfile with Ruby array of hashes like here - http://sysadm.pp.ua/linux/sistemy-virtualizacii/vagrantfile.html . You can define array like:

   
 servers=[
     
{
       
:hostname => "web",
       
:ip => "192.168.100.10",
       
:box => "saucy",
       
:ram => 1024,
       
:cpu => 2
     
},
     
{
       
:hostname => "db",
       
:ip => "192.168.100.11",
       
:box => "saucy",
       
:ram => 2048,
       
:cpu => 4
     
}
   
]

Then you just iterate each item in server array and define configs:

Vagrant.configure(2) do |config|
    servers
.each do |machine|
        config
.vm.define machine[:hostname] do |node|
            node
.vm.box = machine[:box]
            node
.vm.hostname = machine[:hostname]
            node
.vm.network "private_network", ip: machine[:ip]
            node
.vm.provider "virtualbox" do |vb|
                vb
.customize ["modifyvm", :id, "--memory", machine[:ram]]
Reply all
Reply to author
Forward
0 new messages