lib/minke/docker/docker_runner.rb in minke-1.10.0 vs lib/minke/docker/docker_runner.rb in minke-1.11.0
- old
+ new
@@ -1,8 +1,12 @@
module Minke
module Docker
class DockerRunner
+ def initialize network = nil
+ @network = network ||= 'bridge'
+ end
+
##
# returns the ip address that docker is running on
def get_docker_ip_address
# first try to get the ip from docker-ip env
if !ENV['DOCKER_IP'].to_s.empty?
@@ -37,10 +41,11 @@
##
# pull_image pulls a new copy of the given image from the registry
def pull_image image_name
puts "Pulling Image: #{image_name}"
+ ::Docker.options = {:chunk_size => 1, :read_timeout => 3600}
::Docker::Image.create('fromImage' => image_name)
end
##
# running_images returns a list of running containers
@@ -56,68 +61,78 @@
# create_and_run_container starts a conatainer of the given image name and executes a command
#
# Returns:
# - Docker::Container
# - sucess (true if command succeded without error)
- def create_and_run_container image, volumes, environment, working_directory, cmd
+ def create_and_run_container args
# update the timeout for the Excon Http Client
# set the chunk size to enable streaming of log files
- #puts working_directory
- puts volumes
- #puts environment
-
::Docker.options = {:chunk_size => 1, :read_timeout => 3600}
container = ::Docker::Container.create(
- 'Image' => image,
- 'Cmd' => cmd,
- "Binds" => volumes,
- "Env" => environment,
- 'WorkingDir' => working_directory)
+ 'Image' => args[:image],
+ 'Cmd' => args[:command],
+ "Binds" => args[:volumes],
+ "Env" => args[:environment],
+ 'WorkingDir' => args[:working_directory],
+ 'NetworkMode' => @network,
+ 'name' => args[:name],
+ 'PublishAllPorts' => true
+ )
success = true
- thread = Thread.new do
- container.attach(:stream => true, :stdin => nil, :stdout => true, :stderr => true, :logs => false, :tty => false) do
- |stream, chunk|
- stream.to_s == 'stdout' ? color = :green : color = :red
- puts "#{chunk.strip}".colorize(color)
+ unless args[:deamon] == true
+ thread = Thread.new do
+ container.attach(:stream => true, :stdin => nil, :stdout => true, :stderr => true, :logs => false, :tty => false) do
+ |stream, chunk|
+ stream.to_s == 'stdout' ? color = :green : color = :red
+ puts "#{chunk.strip}".colorize(color)
- if stream.to_s == "stderr"
- success = false
- else
- success = true
- end
+ if stream.to_s == "stderr"
+ success = false
+ else
+ success = true
+ end
+ end
end
end
container.start
- thread.join
+
+ thread.join unless args[:deamon] == true
return container, success
end
##
# build_image creates a new image from the given Dockerfile and name
def build_image dockerfile_dir, name
+ puts dockerfile_dir
+ puts name
::Docker.options = {:read_timeout => 6200}
begin
::Docker::Image.build_from_dir(dockerfile_dir, {:t => name}) do |v|
data = /{"stream.*:"(.*)".*/.match(v)
data = data[1].encode(Encoding.find('UTF-8'), {invalid: :replace, undef: :replace, replace: ''}) unless data == nil || data.length < 1
$stdout.puts data unless data == nil
end
rescue => e
+ puts e
message = /.*{"message":"(.*?)"}/.match(e.to_s)
puts "Error: #{message[1]}" unless message == nil || message.length < 1
end
end
+ def stop_container container
+ container.stop()
+ end
+
def delete_container container
if container != nil
begin
container.delete()
rescue => e
- puts "Error: Unable to delete container"
+ puts "Error: Unable to delete container: #{e}"
end
end
end
def login_registry url, user, password, email