Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
List files in each layer of an image tarball created through `docker save`.
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# Copyright © 2022 Pi-Yueh Chuang <>
# Distributed under terms of the BSD 3-Clause license.
"""List files in each layer of an image tarball created through `docker save`.
import argparse
import logging
import pathlib
import tarfile
import tempfile
def main(image, logger):
"""Main function.
with, "r") as fobj:
member =
while member is not None:
if "layer.tar" not in
member =
with tempfile.TemporaryDirectory() as tempdir:
fobj.extract(member, tempdir)
with"{tempdir}/{}", "r") as subfobj:
submember =
while submember is not None:"\t{}")
submember ="")
member =
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="List files in each layer of a Docker image (in *.tar.gz format)")
parser.add_argument("image", metavar="IMAGE", action="store", type=pathlib.Path)
parser.add_argument("logfile", metavar="LOGFILE", action="store", type=pathlib.Path)
args = parser.parse_args()
logger = logging.getLogger(__name__)
logging.basicConfig(handlers=[logging.NullHandler()], format="%(message)s", level=logging.DEBUG)
logger.addHandler(logging.FileHandler(args.logfile, "w", "utf-8"))
main(args.image, logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment