From 01a9046f2823beac8fe3b6313059b9c452445421 Mon Sep 17 00:00:00 2001 From: Clayton O'Neill Date: Tue, 3 Apr 2018 12:48:10 -0400 Subject: [PATCH 1/2] Add spec test for volume mounts This adds a new spec test that will mount a local directory and have the docker container launched create a file in that directory. This is to ensure that the volume mounts are operating as expected. --- spec/hoosegow_docker_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/hoosegow_docker_spec.rb b/spec/hoosegow_docker_spec.rb index 320a5e7..98946ba 100644 --- a/spec/hoosegow_docker_spec.rb +++ b/spec/hoosegow_docker_spec.rb @@ -1,3 +1,4 @@ +require 'fileutils' require_relative '../lib/hoosegow' unless defined?(CONFIG) @@ -44,6 +45,32 @@ end end + context "volume is writable" do + it "calls after_create" do + test_dir = File.join(Dir.pwd, 'volume-test') + FileUtils.remove_dir(test_dir, true) + FileUtils.mkpath(test_dir) + + config = CONFIG.merge( + :Entrypoint => ['/usr/bin/touch', '/volume-test/test'], + :volumes => { '/volume-test' => test_dir + ":rw"} + ) + docker = Hoosegow::Docker.new(config) + begin + docker.create_container CONFIG[:image_name] + docker.start_container + ensure + docker.stop_container + docker.delete_container + end + + exists = File.exists?(File.join(test_dir, 'test')) + expect(exists).to be_truthy + FileUtils.remove_dir(test_dir, true) + end + end + + context 'docker_url' do it "correctly generates TCP urls" do hoosegow = Hoosegow::Docker.new CONFIG.merge(:host => "1.1.1.1", :port => 1234) From 0d95b153b3438ab5ccdc09ea3a8d5a8793300fd6 Mon Sep 17 00:00:00 2001 From: Clayton O'Neill Date: Tue, 3 Apr 2018 12:56:19 -0400 Subject: [PATCH 2/2] Update volume mounts for new docker api Docker Engine API has deprecated passing in options when starting the container and now expects all volumes/binds to be passed in when the container is created --- lib/hoosegow/docker.rb | 24 +++++++----------------- spec/hoosegow_docker_spec.rb | 6 ------ 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/lib/hoosegow/docker.rb b/lib/hoosegow/docker.rb index 7bef80e..fc435ae 100644 --- a/lib/hoosegow/docker.rb +++ b/lib/hoosegow/docker.rb @@ -80,9 +80,11 @@ def run_container(image, data, &block) # Returns nothing. def create_container(image) @container = ::Docker::Container.create @container_options.merge( - :StdinOnce => true, - :OpenStdin => true, - :Volumes => volumes_for_create, + :StdinOnce => true, + :OpenStdin => true, + :HostConfig => { + :Binds => volumes_for_bind + }, :Image => image ) callback @after_create @@ -92,7 +94,7 @@ def create_container(image) # # Returns nothing. def start_container - @container.start :Binds => volumes_for_bind + @container.start callback @after_start end @@ -200,19 +202,7 @@ def docker_url(options) end end - # Private: Generate the `Volumes` argument for creating a container. - # - # Given a hash of container_path => local_path in @volumes, generate a - # hash of container_path => {}. - def volumes_for_create - result = {} - each_volume do |container_path, local_path, permissions| - result[container_path] = {} - end - result - end - - # Private: Generate the `Binds` argument for starting a container. + # Private: Generate the `Binds` argument for creating a container. # # Given a hash of container_path => local_path in @volumes, generate an # array of "local_path:container_path:rw". diff --git a/spec/hoosegow_docker_spec.rb b/spec/hoosegow_docker_spec.rb index 98946ba..692ad31 100644 --- a/spec/hoosegow_docker_spec.rb +++ b/spec/hoosegow_docker_spec.rb @@ -19,13 +19,11 @@ context 'unspecified' do subject { described_class.new } - its(:volumes_for_create) { should be_empty } its(:volumes_for_bind) { should be_empty } end context 'empty' do let(:volumes) { {} } - its(:volumes_for_create) { should be_empty } its(:volumes_for_bind) { should be_empty } end @@ -34,10 +32,6 @@ "/inside/path" => "/home/burke/data-for-container:rw", "/other/path" => "/etc/shared-config", } } - its(:volumes_for_create) { should == { - "/inside/path" => {}, - "/other/path" => {}, - } } its(:volumes_for_bind) { should == [ "/home/burke/data-for-container:/inside/path:rw", "/etc/shared-config:/other/path:ro",