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, #run_command

Instance Method Details

#build_image(state, dockerfile) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 46

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

  parse_image_id(output)
end

#image_exists?(state) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 70

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
35
36
37
38
39
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 27

def parse_image_id(output)
  output.each_line do |line|
    if line =~ /writing image sha256:[[:xdigit:]]{64} done/i
      img_id = line[/writing image (sha256:[[:xdigit:]]{64}) done/i,1]
      return img_id
    end
    if line =~ /image id|build successful|successfully built/i
      img_id = line.split(/\s+/).last
      return img_id
    end
  end
  raise ActionFailed, 'Could not parse Docker build output for image ID'
end

#remove_image(state) ⇒ Object


41
42
43
44
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 41

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