RSS Amplifier

John's Blog · Nov 13, 2024

Listing Files in Docker Image

0
Sign in to vote or save

John · John's Blog

Introduction

A common problem I run into when containerizing apps is checking the files included in the image. More often than I’d like to admit, the first attempt I end up with files in the wrong location. For example, when I containerized “is it Easter”, I needed to verify all the files were getting added to the correct location.

Normally, I’d create an image, then start it as a container with Docker Desktop. At which point I can view the container contents. This works, but isn’t great. A lot of times I want to check the file contents because I know the container won’t start and I’m trying to figure out why. Like if my CMD referencing the wrong location.

Create and Export

Here is a clever solution but not necessarily the most efficient. That said, it’s the best I’ve found.

❯ docker create --name="tmp_container" isiteaster
❯ docker export tmp_container | tar -t
❯ docker container rm -v tmp_container

As far as I can tell there is no way to list the contents of an image directly. The best you can do is create a container and export its contents to a tar archive. Then you can inspect the archive. Which is exactly what’s happening in the above snippet.

  1. A temporary container is created from the image.
  2. The container is exported
  3. The temporary container is deleted

Step 2, docker export will export the contents of the container to a tar archive. The archive that’s being generated is streamed to stdout and piped into tar -t which lists the archive contents. The file listing can be redirected to a file with > after the tar -t.

The -v option needs to be used when deleting the container because it removes any volumes created by the container. Without this option you’ll have danging volumes that you don’t need to keep around.

Possible Issue

Image size could be a point of concern with this approach. When exporting a container it exports all data within the container. If you have a 10 GB container, then 10 GB will be streamed to tar from docker export.

Read the original on nachtimwald.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.