Class: Minke::Docker::DockerRunner
- Inherits:
-
Object
- Object
- Minke::Docker::DockerRunner
- Defined in:
- lib/minke/docker/docker_runner.rb
Instance Method Summary collapse
-
#build_image(dockerfile_dir, name) ⇒ Object
build_image creates a new image from the given Dockerfile and name.
-
#create_and_run_blocking_container(args) ⇒ Object
create_and_run_blocking_container starts a conatainer of the given image name and executes a command, this method blocks until the container exits.
-
#create_and_run_container(args) ⇒ Object
create_and_run_container starts a conatainer of the given image name and executes a command.
- #delete_container(container) ⇒ Object
- #docker_version ⇒ Object
-
#find_image(image_name) ⇒ Object
find_image finds a docker image in the local registry Returns.
-
#get_docker_ip_address ⇒ Object
returns the ip address that docker is running on.
- #get_exposed_ports(args) ⇒ Object
- #get_port_bindings(args) ⇒ Object
-
#initialize(logger, network = nil, project = nil) ⇒ DockerRunner
constructor
A new instance of DockerRunner.
- #login_registry(url, user, password, email) ⇒ Object
-
#pull_image(image_name) ⇒ Object
pull_image pulls a new copy of the given image from the registry.
- #push_image(image_name) ⇒ Object
-
#running_containers ⇒ Object
running_images returns a list of running containers Returns.
- #stop_container(container) ⇒ Object
- #tag_image(image_name, tag) ⇒ Object
Constructor Details
#initialize(logger, network = nil, project = nil) ⇒ DockerRunner
Returns a new instance of DockerRunner.
4 5 6 7 8 |
# File 'lib/minke/docker/docker_runner.rb', line 4 def initialize logger, network = nil, project = nil @network = network ||= 'bridge' @logger = logger @project = project end |
Instance Method Details
#build_image(dockerfile_dir, name) ⇒ Object
build_image creates a new image from the given Dockerfile and name
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/minke/docker/docker_runner.rb', line 164 def build_image dockerfile_dir, name ::Docker. = {: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: ''}).chomp('/n') unless data == nil || data.length < 1 @logger.info data unless data == nil end rescue => e = e. @logger.error "Error: #{}" unless == nil || .length < 1 end end |
#create_and_run_blocking_container(args) ⇒ Object
create_and_run_blocking_container starts a conatainer of the given image name and executes a command, this method blocks until the container exits
Returns:
-
Docker::Container
-
sucess (true if command succeded without error)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/minke/docker/docker_runner.rb', line 116 def create_and_run_blocking_container args host_config = get_port_bindings args host_config['NetworkMode'] = @network host_config['Binds'] = args[:volumes] if args[:links] != nil network = {'EndpointsConfig' => {@network => {'Links' => args[:links].map {|l| "#{@project}_#{l}_1:#{l}"}} }} end exposed_ports = get_exposed_ports args # update the timeout for the Excon Http Client # set the chunk size to enable streaming of log files #::Docker.options = {:chunk_size => 1, :read_timeout => 3600} container = ::Docker::Container.create( 'Image' => args[:image], 'Cmd' => args[:command], "Binds" => args[:volumes], "Env" => args[:environment], 'WorkingDir' => args[:working_directory], 'name' => args[:name], 'NetworkMode' => @network, "OpenStdin" => true, "Tty" => true, 'PublishAllPorts' => true, 'ExposedPorts' => exposed_ports, 'HostConfig' => host_config, 'NetworkingConfig' => network ) container.start success = (container.json['State']['ExitCode'] == 0) ? true: false @logger.error("Unable to start docker container") unless success STDIN.raw do |stdin| container.attach(stdin: stdin, tty: true) do |chunk| print chunk end end return container, success end |
#create_and_run_container(args) ⇒ Object
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)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/minke/docker/docker_runner.rb', line 68 def create_and_run_container args # update the timeout for the Excon Http Client # set the chunk size to enable streaming of log files ::Docker. = {:chunk_size => 1, :read_timeout => 3600} container = ::Docker::Container.create( 'Image' => args[:image], 'Cmd' => args[:command], "Binds" => args[:volumes], "Env" => args[:environment], 'NetworkMode' => @network, 'WorkingDir' => args[:working_directory], 'name' => args[:name], 'PublishAllPorts' => true ) output = '' 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| if chunk.index('[ERROR]') != nil # deal with hidden characters @logger.error chunk.gsub(/\[.*\]/,'') else output += chunk.gsub(/\[.*\]/,'') if output == '' output += chunk.gsub(/\[.*\]/,'').prepend(" ") unless output == '' @logger.debug chunk.gsub(/\[.*\]/,'') end end end end container.start thread.join unless args[:deamon] == true success = (container.json['State']['ExitCode'] == 0) ? true: false @logger.error(output) unless success return container, success end |
#delete_container(container) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/minke/docker/docker_runner.rb', line 182 def delete_container container if container != nil begin container.delete() rescue => e @logger.error "Error: Unable to delete container: #{e}" end end end |
#docker_version ⇒ Object
212 213 214 |
# File 'lib/minke/docker/docker_runner.rb', line 212 def docker_version ::Docker.version['Version'] end |
#find_image(image_name) ⇒ Object
find_image finds a docker image in the local registry Returns
Docker::Image
35 36 37 38 39 40 41 42 43 |
# File 'lib/minke/docker/docker_runner.rb', line 35 def find_image image_name found = nil ::Docker::Image.all.each do | image | found = image if image.info["RepoTags"] != nil && image.info["RepoTags"].include?(image_name) end return found end |
#get_docker_ip_address ⇒ Object
returns the ip address that docker is running on
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/minke/docker/docker_runner.rb', line 12 def get_docker_ip_address # first try to get the ip from docker-ip env if !ENV['DOCKER_IP'].to_s.empty? return ENV['DOCKER_IP'] end if !ENV['DOCKER_HOST'].to_s.empty? # dockerhost set host = ENV['DOCKER_HOST'].dup host.gsub!(/tcp:\/\//, '') host.gsub!(/:\d+/,'') return host else return '127.0.0.1' end end |
#get_exposed_ports(args) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/minke/docker/docker_runner.rb', line 234 def get_exposed_ports args port_bindings = {} if args[:ports] != nil args[:ports].each do |p| hostDest = p.split(":") port_bindings[hostDest[1] + "/tcp"] = {} end end return port_bindings end |
#get_port_bindings(args) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/minke/docker/docker_runner.rb', line 216 def get_port_bindings args host_config = {} if args[:ports] != nil port_bindings = {} args[:ports].each do |p| hostDest = p.split(":") if hostDest[0] == "" port_bindings[hostDest[1] + "/tcp"] = [{'HostPort' => "#{rand(40000..50000)}", 'HostIp' => "0.0.0.0"}] else port_bindings[hostDest[1] + "/tcp"] = [{'HostPort' => hostDest[0], 'HostIp' => "0.0.0.0"}] end end host_config = {'PortBindings' => port_bindings } end return host_config end |
#login_registry(url, user, password, email) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/minke/docker/docker_runner.rb', line 192 def login_registry url, user, password, email if docker_version.start_with? '1.11' # email is removed for login in docker 1.11 system("docker login -u #{user} -p #{password} #{url}") else system("docker login -u #{user} -p #{password} -e #{email} #{url}") end $?.exitstatus end |
#pull_image(image_name) ⇒ Object
pull_image pulls a new copy of the given image from the registry
47 48 49 50 |
# File 'lib/minke/docker/docker_runner.rb', line 47 def pull_image image_name ::Docker. = {:chunk_size => 1, :read_timeout => 3600} ::Docker::Image.create('fromImage' => image_name) end |
#push_image(image_name) ⇒ Object
207 208 209 210 |
# File 'lib/minke/docker/docker_runner.rb', line 207 def push_image image_name system("docker push #{image_name}:latest") $?.exitstatus == 0 end |
#running_containers ⇒ Object
running_images returns a list of running containers Returns
Array of Docker::Image
57 58 59 60 |
# File 'lib/minke/docker/docker_runner.rb', line 57 def running_containers containers = ::Docker::Container.all(all: true, filters: { status: ["running"] }.to_json) return containers end |
#stop_container(container) ⇒ Object
178 179 180 |
# File 'lib/minke/docker/docker_runner.rb', line 178 def stop_container container container.stop() end |
#tag_image(image_name, tag) ⇒ Object
202 203 204 205 |
# File 'lib/minke/docker/docker_runner.rb', line 202 def tag_image image_name, tag image = self.find_image "#{image_name}:latest" image.tag('repo' => tag, 'force' => true) unless image.info["RepoTags"].include? "#{tag}:latest" end |