LoginSignup
6
14

More than 5 years have passed since last update.

vagrantの共有フォルダ synced_folder について

Last updated at Posted at 2016-11-11

vagrantの共有フォルダ synced_folder について

vagrantでは、デフォルトでカレントディレクトリが共有される
https://www.vagrantup.com/docs/synced-folders/

By default, Vagrant will share your project directory (the directory with the Vagrantfile) to /vagrant.

デフォルトで、Vagrantはあなたのプロジェクトディレクトリ(Vagrantfileのディレクトリ)を/vagrantに共有します。

つまり、以下のディレクトリ構成で実行すると「/project」が「/vagrant」として共有されてしまうのです。

ホストの
/project/Vagrantfile
    /mysql
    /web_app1
    /web_app2

ゲストの
/vagrant/Vagrantfile
    /mysql
    /web_app1
    /web_app2

って見えちゃう

デフォルトの共有を止めるには、config.vm.synced_folderで、disabled: trueすると良い

Vagrantfile.sample
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "hoge/package"
  config.vm.box_url = "https://s3-ap-northeast-1.amazonaws.com/hoge-private-repo/hoge/private_image.json"

  #config.vmに書くと、ゲスト全部に適用されます。
  config.vm.synced_folder '.', '/vagrant', disabled: true # デフォルトの共有を行わない

end

共有したいディレクトリは、個別に設定を追加する

Vagrantfile.sample
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "hoge/package"
  config.vm.box_url = "https://s3-ap-northeast-1.amazonaws.com/hoge-private-repo/hoge/private_image.json"

  #config.vmに書くと、ゲスト全部に適用されます。
  config.vm.synced_folder '.', '/vagrant', disabled: true # デフォルトの共有を行わない

  # DBサーバー
  config.vm.define "dbserver" do |atomic|
    atomic.vm.hostname = "dbserver.local"
    atomic.vm.synced_folder "./mysql", "/var/lib/mysql", owner: "mysql", group: "mysql"
  end

  # APサーバー
  config.vm.define "apserver" do |atomic|
    atomic.vm.hostname = "apserver.local"

    #複数のディレクトリを共有したいときは、別々に書く
    atomic.vm.synced_folder "./web_app1", "/var/www/html/web_app1", owner: "apache", group: "apache"
    atomic.vm.synced_folder "./web_app2", "/var/www/html/web_app2", owner: "apache", group: "apache"
  end
end

最後に

一度、共有されるとトップディレクトリ「/vagrant」は残っていましたので、消す必要がありました。

config.vm.synced_folderの所は、vagrant reloadしてあげないと、Vagrantfileへの変更が適用されない

atomic.vm.synced_folderの所は、vagrant provisionでも適用される

6
14
0

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
6
14