Unpack container image using docker-py

This is just a quick blog post. I’ve seen a bunch of questions on docker-py’s issue tracker about how to extract a container image using docker-py and get_archive (a.k.a. docker export).

Here’s what I’m doing:

import subprocess
import docker

container_image = "fedora:27"
path = "/var/tmp/container-image"
docker_client = docker.APIClient(timeout=600)
container = docker_client.create_container(container_image)
try:
    stream, _ = docker_client.get_archive(container, "/")

    # tarfile is hard to use
    os.mkdir(path, 0o0700)
    logger.debug("about to untar the image")
    p = subprocess.Popen(
        ["tar", "--no-same-owner", "-C", path, "-x"],
        stdin=subprocess.PIPE,
    )
    for x in stream:
        p.stdin.write(x)
    p.stdin.close()
    p.wait()
    if p.returncode:
        raise RuntimeError("Failed to unpack the archive.")
finally:
    docker_client.remove_container(container, force=True)

Pretty easy, right?

comments powered by Disqus