Quick hack: extracting the contents of a Docker image to disk

Hello! Please note I’ve written a little python tool called Occy Strap which makes this a bit easier, and can do some fancy things around importing and exporting multiple images. You might want to read about it?

For various reasons, I wanted to inspect the contents of a Docker image without starting a container. Docker makes it easy to get an image as a tar file, like this:

docker save -o foo.tar image

But if you extract that tar file you’ll find a configuration file and manifest as JSON files, and then a series of tar files, one per image layer. You use the manifest to determine in what order you extract the tar files to build the container filesystem.

That’s fiddly and annoying. So I wrote this quick python hack to extract an image tarball into a directory on disk that I could inspect:


# Call me like this:
#  docker-image-extract tarfile.tar extracted

import tarfile
import json
import os
import sys

image_path = sys.argv[1]
extracted_path = sys.argv[2]

image = tarfile.open(image_path)
manifest = json.loads(image.extractfile('manifest.json').read())

for layer in manifest[0]['Layers']:
    print('Found layer: %s' % layer)
    layer_tar = tarfile.open(fileobj=image.extractfile(layer))

    for tarinfo in layer_tar:
        print('  ... %s' % tarinfo.name)
        if tarinfo.isdev():
            print('  --> skip device files')

        dest = os.path.join(extracted_path, tarinfo.name)
        if not tarinfo.isdir() and os.path.exists(dest):
            print('  --> remove old version of file')

        layer_tar.extract(tarinfo, path=extracted_path)

Hopefully that’s useful to someone else (or future me).

Building a Thien Baffle

For a while now I’ve been meaning to play with making a Thien Baffle for my garage workshop. The motivation is that whilst I have a quite nice 3HP dust extractor (for those in the market for such a thing — don’t spend $1,400 with some retailers — I am super happy with my $500 unit from Leda Machinery), I want to keep the number of times I change the bags to a bare minimum. I see the dust extractor as a way of controlling potentially dangerous very fine dust, whereas some of the machines in my workshop create a lot of very large shavings — the thicknesser seems like the most obvious culprit here. It would be cool to divert these larger shavings into a bin where I can just use them as garden mulch, and then save the dust extractor bags for the fine and more dangerous dust.

Continue reading “Building a Thien Baffle”