Module: Kitchen::Docker::Helpers::ImageHelper

Includes:
Configurable, CliHelper, ContainerHelper
Included in:
Container
Defined in:
lib/kitchen/docker/helpers/image_helper.rb

Instance Method Summary collapse

Methods included from ContainerHelper

#container_env_variables, #container_exec, #container_exists?, #container_ip_address, #copy_file_to_container, #create_dir_on_container, #dockerfile_path, #dockerfile_proxy_config, #dockerfile_template, #parse_container_id, #remote_socket?, #remove_container, #replace_env_variables, #run_container, #socket_uri

Methods included from CliHelper

#build_copy_command, #build_env_variable_args, #build_exec_command, #build_powershell_command, #build_run_command, #config_to_options, #dev_null, #docker_command, #docker_shell_opts

Instance Method Details

#build_image(state, dockerfile) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 41

def build_image(state, dockerfile)
  cmd = 'build'
  cmd << ' --no-cache' unless config[:use_cache]
  extra_build_options = config_to_options(config[:build_options])
  cmd << " #{extra_build_options}" unless extra_build_options.empty?
  dockerfile_contents = dockerfile
  build_context = config[:build_context] ? '.' : '-'
  file = Tempfile.new('Dockerfile-kitchen', Dir.pwd)
  output = begin
             file.write(dockerfile)
             file.close
             docker_command("#{cmd} -f #{Shellwords.escape(dockerfile_path(file))} #{build_context}",
                            input: dockerfile_contents)
           ensure
             file.close unless file.closed?
             file.unlink
           end

  parse_image_id(output)
end

#image_exists?(state) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 62

def image_exists?(state)
  state[:image_id] && !!docker_command("inspect --type=image #{state[:image_id]}") rescue false
end

#parse_image_id(output) ⇒ Object

Raises:

  • (ActionFailed)


27
28
29
30
31
32
33
34
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 27

def parse_image_id(output)
  output.each_line do |line|
    if line =~ /image id|build successful|successfully built/i
      return line.split(/\s+/).last
    end
  end
  raise ActionFailed, 'Could not parse Docker build output for image ID'
end

#remove_image(state) ⇒ Object



36
37
38
39
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 36

def remove_image(state)
  image_id = state[:image_id]
  docker_command("rmi #{image_id}")
end