From 74fc04769f062cc9818512d5e33755a8c42e2616 Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Mon, 18 Oct 2021 12:59:50 +0200 Subject: [PATCH 1/2] fix[docker] getting Container ID in case the image does not exists locally docker will pull it from any remote registry, but will include the progress information in the output: > Unable to find image 'docker-registry.XXX/YYY:ZZZ' locally > ZZZ: Pulling from YYY ... > Digest: sha256:4b9bfe7b0a6c970e3613c04f267ba6319cfceb8cc120b0435d9ee7b8037a1f06 > Status: Downloaded newer image for docker-registry.XXX/YYY:ZZZ > 8be38c89d12937b98c8be5ab7466dd45b0e4a306862f282b58077ac7193251eb The old code expected the output to be the container ID, which fails in that case. --- piuparts.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/piuparts.py b/piuparts.py index f05db2ff..be99abf7 100644 --- a/piuparts.py +++ b/piuparts.py @@ -922,11 +922,16 @@ class Chroot: def setup_from_docker(self, docker_image): self.check_if_docker_storage_driver_is_supported() - ret_code, output = run(['docker', 'run', '-d', '-it', docker_image, 'bash']) - if ret_code != 0: - logging.error("Couldn't start the container from '%s'" % docker_image) - panic() - self.docker_container = output.strip() + with tempfile.TemporaryDirectory() as tmpdir: + tmp = os.path.join(tmpdir, "cid") + ret_code, output = run(['docker', 'run', '-d', '-it', '--cidfile', tmp, docker_image, 'bash']) + if ret_code != 0: + logging.error("Couldn't start the container from '%s'" % docker_image) + panic() + + with open(tmp, "r") as fd: + self.docker_container = fd.read().strip() + ret_code, output = run(['docker', 'inspect', self.docker_container]) container_data = json.loads(output)[0] self.name = container_data['GraphDriver']['Data']['MergedDir'] -- GitLab From 72c50f0753638d7ca6dd2fb5d1083b747c64be6d Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Mon, 18 Oct 2021 14:18:25 +0200 Subject: [PATCH 2/2] refactor[docker] Let `docker inspect` return path Directly use golang templating to only get the MergedDir path instead of using JSON in Python. Include path in debug output. Let logging.debug() handle the variable substitution. --- piuparts.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/piuparts.py b/piuparts.py index be99abf7..e9c9934d 100644 --- a/piuparts.py +++ b/piuparts.py @@ -46,7 +46,6 @@ import os import tarfile import stat import re -import json import pickle import subprocess import traceback @@ -932,10 +931,9 @@ class Chroot: with open(tmp, "r") as fd: self.docker_container = fd.read().strip() - ret_code, output = run(['docker', 'inspect', self.docker_container]) - container_data = json.loads(output)[0] - self.name = container_data['GraphDriver']['Data']['MergedDir'] - logging.info("New container created '%s'" % self.docker_container) + ret_code, output = run(['docker', 'inspect', '-f', '{{ .GraphDriver.Data.MergedDir }}', self.docker_container]) + self.name = output.strip() + logging.info("New container created %r at %r", self.docker_container, self.name) def setup_from_lvm(self, lvm_volume): """Create a chroot by creating an LVM snapshot.""" -- GitLab