Skip to content

Instantly share code, notes, and snippets.

@jakejarvis
Last active August 2, 2020 15:36
Show Gist options
  • Save jakejarvis/b9a1d406b3f6f8ed20b61724e085c7b6 to your computer and use it in GitHub Desktop.
Save jakejarvis/b9a1d406b3f6f8ed20b61724e085c7b6 to your computer and use it in GitHub Desktop.
extract files from a docker image's filesystem (NOT a running container)
#!/usr/bin/python3
# Pulled from: https://www.madebymikal.com/quick-hack-extracting-the-contents-of-a-docker-image-to-disk/
# Usage:
# docker save -o image_files.tar image_tag:latest
# ./docker-image-extract.py image_files.tar /tmp/files
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')
continue
dest = os.path.join(extracted_path, tarinfo.name)
if not tarinfo.isdir() and os.path.exists(dest):
print(' --> remove old version of file')
os.unlink(dest)
layer_tar.extract(tarinfo, path=extracted_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment