From 428eb3ed939cefbd94731b0e01ccf88fe906ce2e Mon Sep 17 00:00:00 2001 From: Matthew Olenik Date: Tue, 8 May 2018 21:10:54 -0700 Subject: [PATCH 1/2] Support Docker volume consistency for synced folders Adds the `docker_consistency` option, which sets the Docker volume consistency level. This can be used to greatly improved synced folder performance, especially on macOS. See for details: moby/moby#31047 --- plugins/providers/docker/synced_folder.rb | 5 ++++- website/source/docs/docker/basics.html.md | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/plugins/providers/docker/synced_folder.rb b/plugins/providers/docker/synced_folder.rb index e18682586..ca3829841 100644 --- a/plugins/providers/docker/synced_folder.rb +++ b/plugins/providers/docker/synced_folder.rb @@ -21,7 +21,10 @@ module VagrantPlugins host_path = data[:hostpath] guest_path = data[:guestpath] - machine.provider_config.volumes << "#{host_path}:#{guest_path}" + # Append consistency option if it exists, otherwise let it nil out + consistency = data[:docker_consistency] + consistency &&= ":" + consistency + machine.provider_config.volumes << "#{host_path}:#{guest_path}#{consistency}" end end end diff --git a/website/source/docs/docker/basics.html.md b/website/source/docs/docker/basics.html.md index e3846d71b..521a4d685 100644 --- a/website/source/docs/docker/basics.html.md +++ b/website/source/docs/docker/basics.html.md @@ -75,6 +75,16 @@ on folders synced with a docker container. Private and public networks are not currently supported. +### Volume Consistency + +Docker's [volume consistency](https://github.com/moby/moby/pull/31047) setting can be specified using the `docker_consistency` option when defining a synced folder. This can +[greatly improve performance on macOS](https://docs.docker.com/docker-for-mac/osxfs-caching). An example is shown using the `cached` and `delegated` settings: + +``` +config.vm.synced_folder "/host/dir1", "/guest/dir1", docker_consistency: "cached" +config.vm.synced_folder "/host/dir2", "/guest/dir2", docker_consistency: "delegated" +``` + ## Host VM If the system cannot run Linux containers natively, Vagrant automatically spins From a3ef471cb0f334753981669ebf56c7bf9be5173b Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 27 Jul 2018 15:11:45 -0700 Subject: [PATCH 2/2] (#9811) Add test for volume consistency and improve documentation --- .../providers/docker/synced_folder_test.rb | 56 +++++++++++++++++-- website/source/docs/docker/basics.html.md | 4 +- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/test/unit/plugins/providers/docker/synced_folder_test.rb b/test/unit/plugins/providers/docker/synced_folder_test.rb index 58584312f..4cad2e33e 100644 --- a/test/unit/plugins/providers/docker/synced_folder_test.rb +++ b/test/unit/plugins/providers/docker/synced_folder_test.rb @@ -5,13 +5,15 @@ require Vagrant.source_root.join("plugins/providers/docker/synced_folder") describe VagrantPlugins::DockerProvider::SyncedFolder do subject { described_class.new } + let(:provider_config) { double("provider_config", volumes: []) } + let(:machine) { double("machine") } + + before do + allow(machine).to receive(:provider_name).and_return(:docker) + allow(machine).to receive(:provider_config).and_return(provider_config) + end + describe "#usable?" do - let(:machine) { double("machine") } - - before do - allow(machine).to receive(:provider_name).and_return(:docker) - end - it "is usable" do expect(subject).to be_usable(machine) end @@ -27,4 +29,46 @@ describe VagrantPlugins::DockerProvider::SyncedFolder do to raise_error(VagrantPlugins::DockerProvider::Errors::SyncedFolderNonDocker) end end + + describe "#prepare" do + let(:folders) {{"/guest/dir1"=> + {:guestpath=>"/guest/dir1", + :hostpath=>"/Users/brian/code/vagrant-sandbox", + :disabled=>false, + :__vagrantfile=>true}, + "/dev/vagrant"=> + {:guestpath=>"/dev/vagrant", + :hostpath=>"/Users/brian/code/vagrant", + :disabled=>false, + :__vagrantfile=>true}}} + + let(:consistency_folders) {{"/guest/dir1"=> + {:docker_consistency=>"cached", + :guestpath=>"/guest/dir1", + :hostpath=>"/Users/brian/code/vagrant-sandbox", + :disabled=>false, + :__vagrantfile=>true}, + "/dev/vagrant"=> + {:docker_consistency=>"delegated", + :guestpath=>"/dev/vagrant", + :hostpath=>"/Users/brian/code/vagrant", + :disabled=>false, + :__vagrantfile=>true}}} + let(:options) { {} } + + let(:volumes) { ["/Users/brian/code/vagrant-sandbox:/guest/dir1", + "/Users/brian/code/vagrant:/dev/vagrant"] } + let(:consistency_volumes) { ["/Users/brian/code/vagrant-sandbox:/guest/dir1:cached", + "/Users/brian/code/vagrant:/dev/vagrant:delegated"] } + + it "prepares folders to mount" do + subject.prepare(machine, folders, options) + expect(machine.provider_config.volumes).to eq(volumes) + end + + it "sets volume consistency if specified" do + subject.prepare(machine, consistency_folders, options) + expect(machine.provider_config.volumes).to eq(consistency_volumes) + end + end end diff --git a/website/source/docs/docker/basics.html.md b/website/source/docs/docker/basics.html.md index 521a4d685..e0930b1c2 100644 --- a/website/source/docs/docker/basics.html.md +++ b/website/source/docs/docker/basics.html.md @@ -77,10 +77,10 @@ Private and public networks are not currently supported. ### Volume Consistency -Docker's [volume consistency](https://github.com/moby/moby/pull/31047) setting can be specified using the `docker_consistency` option when defining a synced folder. This can +Docker's [volume consistency](https://docs.docker.com/v17.09/engine/admin/volumes/bind-mounts/) setting can be specified using the `docker_consistency` option when defining a synced folder. This can [greatly improve performance on macOS](https://docs.docker.com/docker-for-mac/osxfs-caching). An example is shown using the `cached` and `delegated` settings: -``` +```ruby config.vm.synced_folder "/host/dir1", "/guest/dir1", docker_consistency: "cached" config.vm.synced_folder "/host/dir2", "/guest/dir2", docker_consistency: "delegated" ```