LoginSignup
83
78

More than 5 years have passed since last update.

[Vagrant]2つのゲストOSを同時に立ち上げる(※別のVagrantfile)

Posted at

[Vagrant]2つのゲストOSを同時に立ち上げる(※別のVagrantfile)

別々のVagrantfileで管理しているゲストOSを同時にvagrant upしようとしたら、sshポート(hostにforwardする方)が重複して失敗した。
(デフォルトはhost: 2222

Vagrantはデフォルトだと、ホストの2222番にゲストの22番をフォワードするらしい。(1.4.3時点)
このホストのポート番号を2222番以外にする事で、2つのゲストOSを起動する事に成功した。

前提

  • Mac
  • Vagrant 1.4.3

現象

  • フォルダ構成は以下。名前は気にしないでください。
/path/to/vagrant-elasticsearch
/path/to/vagrant-elasticsearch/Vagrantfile
/path/to/vagrant-fluent
/path/to/vagrant-fluent/Vagrantfile
  • /path/to/vagrant-elasticsearchでvagrant upした状態で、/path/to/vagrant-fluentでvagrant upを実行した
  • 以下のエラーが発生し、起動に失敗した。
Bringing machine 'default' up with 'virtualbox' provider...


Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening


on these ports. The forwarded port to 2222 is already in use
on the host machine.

To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.
  • ポートの衝突を解消するために、一旦vagrant haltした後で、以下のように設定した。
...
config.vm.network :forwarded_port, guest: 22, host: 2223
...
  • vagrant upで以下の結果になった。
...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 22 => 2223 (adapter 1)
[default] -- 8888 => 8888 (adapter 1)
...
  • ちがう、そうじゃない。[default] -- 22 => 2222 (adapter 1)は要らない。

解決方法

Vagrantfileに以下のように設定する。
id: "ssh"を指定する事で、デフォルトの設定を上書きできるようです。
公式ドキュメントを見ても分かりませんでしたが。

  • /path/to/vagrant-elasticsearch/Vagrantfile
...
config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2223
...
  • /path/to/vagrant-fluent/Vagrantfile
...
config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2224
...

参考URL

以上

83
78
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
83
78