Last active
May 2, 2022 15:09
-
-
Save smijar/0f4a5a8a8ce69ed54baa6e82063ff074 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # | |
| # 1. $ docker save -o foo.tar image | |
| # | |
| # 2. Call me like this: | |
| # docker-image-extract tarfile.tar extracted | |
| # to install $ sudo curl -Lo /usr/local/bin/docker-image-extract https://gist.githubusercontent.com/smijar/0f4a5a8a8ce69ed54baa6e82063ff074/raw/e4f50bc636a27d88aa1a3fbf3073ceff271bbf4b/docker-image-extract.py | |
| 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