Last active
August 2, 2020 15:36
-
-
Save jakejarvis/b9a1d406b3f6f8ed20b61724e085c7b6 to your computer and use it in GitHub Desktop.
extract files from a docker image's filesystem (NOT a running container)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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