From ca0fd64ded5ac879116ebb7dda7d76fafd052b68 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 29 Apr 2019 14:11:57 -0700 Subject: [PATCH] Fixes #10798: Enhance how docker compose driver path expands Prior to this commit, the docker compose driver would _always_ path expand a host volume no matter what. This is not always the correct option, for example if that host volume is actually a reference to a key inside a `volumes` hash instead of a path on disk. This commit changes that by looking to see if the requested host volume is actually a defined key inside the compose config, and if not, it will path expand it like before. Otherwise it will leave the key "as is". --- plugins/providers/docker/driver/compose.rb | 14 +++++++++++++- templates/locales/providers_docker.yml | 6 ++++++ .../providers/docker/driver_compose_test.rb | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/plugins/providers/docker/driver/compose.rb b/plugins/providers/docker/driver/compose.rb index 3313d28d2..a17124906 100644 --- a/plugins/providers/docker/driver/compose.rb +++ b/plugins/providers/docker/driver/compose.rb @@ -102,7 +102,19 @@ module VagrantPlugins # will be fixed someday and the gsub below can be removed. host.gsub!(/^[^A-Za-z]+/, "") end - host = @machine.env.cwd.join(host).to_s + # if host path is a volume key, don't expand it. + # if both exist (a path and a key) show warning and move on + # otherwise assume it's a realative path and expand the host path + compose_config = get_composition + if compose_config["volumes"] && compose_config["volumes"].keys.include?(host) + if File.directory?(@machine.env.cwd.join(host).to_s) + @machine.env.ui.warn(I18n.t("docker_provider.volume_path_not_expanded", + host: host)) + end + else + @logger.debug("Path expanding #{host} to current Vagrant working dir instead of docker-compose config file directory") + host = @machine.env.cwd.join(host).to_s + end "#{host}:#{guest}" end cmd = Array(params.fetch(:cmd)) diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index 5c6b00018..3c23b905c 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -104,6 +104,12 @@ en: destroy the container and recreate it. waiting_for_running: |- Waiting for container to enter "running" state... + volume_path_not_expanded: |- + Host path `%{host}` exists as a `volumes` key and is a folder on disk. Vagrant + will not expand this key like it used to and instead leave it as is defined. + If this folder is intended to be used, make sure its full path is defined + in your `volumes` config. More information can be found on volumes in the + docker compose documentation. messages: destroying: |- diff --git a/test/unit/plugins/providers/docker/driver_compose_test.rb b/test/unit/plugins/providers/docker/driver_compose_test.rb index 7f93560b1..d3a795590 100644 --- a/test/unit/plugins/providers/docker/driver_compose_test.rb +++ b/test/unit/plugins/providers/docker/driver_compose_test.rb @@ -106,6 +106,22 @@ describe VagrantPlugins::DockerProvider::Driver::Compose do end end + context 'with a volumes key in use for mounting' do + let(:compose_config) { {"volumes"=>{"my_volume_key"=>"data"}} } + + before do + params[:volumes] = 'my_volume_key:my/guest/path' + allow(Pathname).to receive(:new).with('./path').and_call_original + allow(Pathname).to receive(:new).with('my_volume_key').and_call_original + allow(Pathname).to receive(:new).with('/compose/cwd/my_volume_key').and_call_original + allow(subject).to receive(:get_composition).and_return(compose_config) + end + + it 'should not expand the relative host directory' do + expect(docker_yml).to receive(:write).with(%r{my_volume_key}) + end + end + it 'links containers' do params[:links].each do |link| expect(docker_yml).to receive(:write).with(/#{link}/)