Module: Kitchen::Docker::Helpers::ImageHelper
Instance Method Summary
collapse
#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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 61
def build_image(state, dockerfile)
cmd = "build"
cmd << " --no-cache" unless config[:use_cache]
cmd << " --platform=#{config[:docker_platform]}" if config[:docker_platform]
= config_to_options(config[:build_options])
cmd << " #{}" unless .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) ⇒ Boolean
85
86
87
|
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 85
def image_exists?(state)
state[:image_id] && !!docker_command("inspect --type=image #{state[:image_id]}") rescue false
end
|
#image_in_use?(state) ⇒ Boolean
57
58
59
|
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 57
def image_in_use?(state)
docker_command("ps -a", suppress_output: !logger.debug?).include?(state[:image_id])
end
|
#parse_image_id(output) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# 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
if line =~ /naming to moby-dangling@(sha256:[[:xdigit:]]{64})(?: \d*\.\ds)? done/i
img_id = line[/naming to moby-dangling@(sha256:[[:xdigit:]]{64})(?: \d*\.\ds)? done/i, 1]
return img_id
end
end
raise ActionFailed, "Could not parse Docker build output for image ID"
end
|
#remove_image(state) ⇒ Object
47
48
49
50
51
52
53
54
55
|
# File 'lib/kitchen/docker/helpers/image_helper.rb', line 47
def remove_image(state)
image_id = state[:image_id]
if image_in_use?(state)
info("[Docker] Image ID #{image_id} is in use. Skipping removal")
else
info("[Docker] Removing image with Image ID #{image_id}.")
docker_command("rmi #{image_id}")
end
end
|