lib/vocker/docker_client.rb in vocker-0.3.3 vs lib/vocker/docker_client.rb in vocker-0.4.0

- old
+ new

@@ -29,11 +29,11 @@ containers.each do |name, config| cids_dir = "/var/lib/vocker/cids" config[:cidfile] ||= "#{cids_dir}/#{Digest::SHA1.hexdigest name}" @machine.communicate.sudo("mkdir -p #{cids_dir}") - run_container(config) + run_container({name: name}.merge config) end end def run_container(config) raise "Container's cidfile was not provided!" unless config[:cidfile] @@ -60,16 +60,43 @@ def container_running?(id) @machine.communicate.test("sudo docker ps -q | grep #{id}") end def create_container(config) - args = "-cidfile=#{config[:cidfile]} -d" - args << " -dns=#{config[:dns]}" if config[:dns] - args << " #{config[:additional_run_args]}" if config[:additional_run_args] + # DISCUSS: Does this really belong here? + ensure_bind_mounts_exist(config) + + args = "-cidfile=#{config[:cidfile]} -d " + args << prepare_run_arguments(config) @machine.communicate.sudo %[ rm -f #{config[:cidfile]} docker run #{args} #{config[:image]} #{config[:cmd]} ] + end + + private + + def ensure_bind_mounts_exist(config) + Array(config[:volumes]).each do |volume| + if volume =~ /(.+):.+/ + guest_vm_path = $1 + @machine.communicate.sudo "mkdir -p #{guest_vm_path}" + end + end + end + + def prepare_run_arguments(config) + args = [] + + args << "-dns=#{config[:dns]}" if config[:dns] + args << "-name=#{config[:name]}" if config[:name] + args << "#{config[:additional_run_args]}" if config[:additional_run_args] + + args += Array(config[:volumes]).map { |volume| "-v #{volume}" } + args += Array(config[:ports]).map { |port| "-p #{port}" } + args += Array(config[:links]).map { |link| "-link #{link}" } + + args.compact.flatten.join ' ' end end end end