module Kitchen::Docker::Helpers::ImageHelper

Public Instance Methods

build_image(state, dockerfile) click to toggle source
# File lib/kitchen/docker/helpers/image_helper.rb, line 47
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', Pathname.pwd + config[:build_tempdir])
  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) click to toggle source
# File lib/kitchen/docker/helpers/image_helper.rb, line 71
def image_exists?(state)
  state[:image_id] && !!docker_command("inspect --type=image #{state[:image_id]}") rescue false
end
parse_image_id(output) click to toggle source
# File lib/kitchen/docker/helpers/image_helper.rb, line 28
def parse_image_id(output)
  output.split("\n").reverse_each do |line|
    if line =~ /writing image (sha256:[[:xdigit:]]{64})(?: \d*\.\ds)? done/i
      img_id = line[/writing image (sha256:[[:xdigit:]]{64})(?: \d*\.\ds)? 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) click to toggle source
# File lib/kitchen/docker/helpers/image_helper.rb, line 42
def remove_image(state)
  image_id = state[:image_id]
  docker_command("rmi #{image_id}")
end