Explore Container Images Without A Shell
A best practice in building container images is to add as little dependencies as possible into the image.
Most application don’t need a shell to run, hence dropping the shell from the container image reduces the size
of the image and reduces the attack surface. Popular base images for that use case is either not using a base image at all
and build the image FROM SCRATCH
or use distroless. The downside is that we can’t open a shell inside the running container
for debugging.
Kubernetes introduces ephemeral containers
to spawn a debugging container into a running pod.
To debug build issues or examine a container image before bringing it in production we have to have a look at the content
of the filesystem in the image. In a container image with a shell installed we can create a container instance and launch a shell as entrypoint.
This is not possible in images based on SCRATCH
or distroless.
The container engine podman allows us to mount a container image into the filesystem of the developer machine. This allows us to examine the filesystem content of the container image with the tools installed on the developers machine.
Mounting the image
To demonstrate this feature of podman we want to examine the current distroless base image . Pull that image:
|
|
The next step is to create a new user namespace. This allows us to freely explore the container image as root user.
|
|
After this we can mount a container image into the user namespace.
|
|
The command returns the mount point where the image is mounted. Let examine the glibc version installed in the distroless image:
|
|
Based on the filenames we assume a glibc 2.28 installed in the image. Since we know that the image is based on debian we can have a look at the dpkg database.
|
|
So we have the concrete package version 2.28-10. We can do the same to find the installed openssl version:
|
|
Cleanup
To clean up, exit the user namespace:
|
|
Last but not least delete the image:
|
|
Alternatives
An alternative to podman is dive. This tool let you explore the image layers in a ncurses UI. It focuses on the filesystem attributes of files and the changes between the image layers. Hence you can see the names and the attributes of a file but not the content.