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 42
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,
                            environment: { DOCKER_BUILDKIT: '0' })
           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 64
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 27
def parse_image_id(output)
  output.each_line do |line|
    if line =~ /image id|build successful|successfully built|writing image/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 37
def remove_image(state)
  image_id = state[:image_id]
  docker_command("rmi #{image_id}")
end