lib/rbbt/util/docker.rb in rbbt-util-5.26.17 vs lib/rbbt/util/docker.rb in rbbt-util-5.26.18
- old
+ new
@@ -1,17 +1,20 @@
module Docker
def self.run(image, cmd, options = {})
- mounts, job_inputs, directory, pipe = Misc.process_options options, :mounts, :job_inputs, :directory, :pipe
+ mounts, job_inputs, directory, pipe, host_user = Misc.process_options options, :mounts, :job_inputs, :directory, :pipe, :host_user
+ pipe = false if pipe.nil?
if mounts
mounts.each{|t,s| FileUtils.mkdir_p s unless File.exist? s}
mount_cmd = mounts.sort.collect{|t,s| "-v " + ["'" + s + "'", "'" + t + "'"] * ":" } * " "
else
mount_cmd = ""
end
image_cmd = "-t #{image}"
+
+ user_cmd = host_user ? "-u $(id -u ${USER}):$(id -g ${USER})" : ""
if directory
Path.setup(directory) unless Path === directory
FileUtils.mkdir_p directory unless File.directory? directory
mount_cmd += " -v '#{directory}':/job"
@@ -31,16 +34,13 @@
else
Open.write(directory[name], obj)
end
end
end if job_inputs
- cmd = "docker run #{mount_cmd} #{image_cmd} #{cmd}"
- if pipe
- CMD.cmd(cmd, :log => true, :pipe => true)
- else
- CMD.cmd_log(cmd, :log => true)
- end
+
+ cmd = "docker run #{mount_cmd} #{user_cmd} #{image_cmd} #{cmd}"
+ CMD.cmd_log(cmd, :log => true, :pipe => pipe)
else
TmpFile.with_file do |tmpfile|
Path.setup(tmpfile)
Open.mkdir tmpfile
mount_cmd += " -v '#{tmpfile}':/job"
@@ -60,10 +60,11 @@
else
Open.write(tmpfile[name], obj)
end
end
end if job_inputs
- cmd = "docker run #{mount_cmd} #{image_cmd} #{cmd}"
+
+ cmd = "docker run #{mount_cmd} #{user_cmd} #{image_cmd} #{cmd}"
CMD.cmd_log(cmd, :log => true)
end
end
end
end