Tomas Tomecek: 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?


Source From: fedoraplanet.org.
Original article title: Tomas Tomecek: Unpack container image using docker-py.
This full article can be read at: Tomas Tomecek: Unpack container image using docker-py.

Advertisement
Boost your Website Traffic with iNeedHits Today! Shop Now!


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*