Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read aux ID field to get image name if not in stream
Browse files Browse the repository at this point in the history
I'm not sure if this is a good thing to add it's undocumented and potentially fragile.
I was trying it when looking at #309 for a root cause, but determined that the error was actually the build failing. So the image id detection wasn't necessary.

The canonicalization though may be valuable to merge w/o the aux reading logic.
tfoote committed Jan 25, 2025
1 parent 0648d39 commit 1fa4dd4
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/rocker/core.py
Original file line number Diff line number Diff line change
@@ -227,28 +227,55 @@ def get_user_name():
userinfo = pwd.getpwuid(os.getuid())
return getattr(userinfo, 'pw_' + 'name')

def canonicalize_image_id(image_id, docker_client=None):
if not docker_client:
docker_client = get_docker_client()
inspect_info = docker_client.inspect_image(image_id)
# print("image info ", inspect_info)
#TODO(tfoote) Consider canonicalizing this long instead of truncated especially with podman support
return inspect_info['Id'].split(':')[1][0:12]

def docker_build(docker_client = None, output_callback = None, **kwargs):
image_id = None
aux_image_id = None

if not docker_client:
docker_client = get_docker_client()
kwargs['decode'] = True
for line in docker_client.build(**kwargs):
# Undocumented way to get image_id from alternative element from stream
# example seen {'aux': {'ID': 'sha256:e328328fb3297a7c0f6cc5c2bf460d976f8e69e87a0706e6b8a211f42afa025c'}}
found_aux_image_id = None
found_aux_image_id = line.get('aux', {}).get('ID')
if found_aux_image_id:
aux_image_id = found_aux_image_id

output = line.get('stream', '').rstrip()

#Reset aux_image_id if a new layer is detected
if aux_image_id and output.startswith('Step '):
aux_image_id = None
print("resetting aux id for new step: ", output[0:15], "...")

if not output:
# print("non stream data", line)
continue
if output_callback is not None:
output_callback(output)
# Fallback detect image_id via string matching
if not image_id:
match = re.match(r'Successfully built ([a-z0-9]{12})', output)
if match:
image_id = match.group(1)

match = re.match(r'Successfully built ([a-z0-9]{12})', output)
if match:
image_id = match.group(1)
# Fallback to aux based image id if found
if not image_id and aux_image_id:
image_id = aux_image_id

if image_id:
return image_id
return canonicalize_image_id(image_id, docker_client=docker_client)
else:
print("no more output and success not detected")
print('Docker Build Failed: Output stream from docker_build finished but no image_id detected.')
return None

def docker_remove_image(

0 comments on commit 1fa4dd4

Please sign in to comment.