Exploring a Docker Container’s File System

Docker containers are lightweight, isolated environments that include everything your application needs to run—code, runtime, libraries, and even the file system. But sometimes you need to look inside a container to debug, inspect logs, or check configuration files.

In this article, we’ll explore several ways to access and explore the file system of a Docker container—whether it’s running or stopped.


✅ Why Explore a Container’s File System?

You might want to:

  • Debug issues inside a running container
  • View application logs
  • Inspect mounted volumes
  • Check environment configuration or installed packages
  • Validate file permissions

🧰 1. Use docker exec to Access the File System

If the container is running, use docker exec to start a shell session inside it.

🔹 Bash (preferred if available):

docker exec -it <container_name_or_id> bash

🔹 If bash is not available, use sh:

docker exec -it <container_name_or_id> sh

✅ Once inside, you can run commands like:

ls -la
cd /var/www
cat /etc/config.yaml

📝 Tip: You can find the container name using docker ps.


🔍 2. Use docker container inspect for Metadata (Not File System)

To get details like mount paths, environment variables, and network info:

docker inspect <container_name_or_id>

This won’t give you file access, but it’s useful to locate volume mounts or check the image source.


📦 3. Explore the Container’s File System from the Host

You can copy files from a running or stopped container to the host using:

docker cp <container_name_or_id>:<container_path> <host_path>

Example:

docker cp my-container:/app/logs ./logs

This is helpful for reading logs or configuration files without running an interactive shell.


🛑 4. Explore a Stopped Container’s File System

A stopped container cannot be accessed with exec, but you can create a new container from the same image and inspect it:

docker run -it --rm <image_name> bash

Or to mount the stopped container’s filesystem temporarily:

🔹 Find the container ID:

docker ps -a

🔹 Start a temporary container with volume mount:

docker run --rm -it --volumes-from <stopped_container_id> ubuntu bash

Now you can explore the file system that was used in the stopped container.


🧪 5. Use docker diff to See File Changes

To list changes made inside a container compared to its original image:

docker diff <container_id_or_name>

This shows:

  • A for added files
  • C for changed files
  • D for deleted files

🧭 Directory Structure Inside a Container

Docker containers are based on Linux and follow a standard Linux file system structure:

DirectoryDescription
/Root directory
/appOften contains your application code
/etcSystem configuration files
/var/logLog files
/usr, /binBinaries and system packages
/tmpTemporary files

✅ Summary of Useful Commands

TaskCommand
Open interactive shell in containerdocker exec -it <container> bash
List container filesdocker exec -it <container> ls /path
Copy file from container to hostdocker cp <container>:/file/path ./local-dir
View changes to container filesystemdocker diff <container>
Inspect container metadatadocker inspect <container>

🚀 Final Thoughts

Exploring a Docker container’s file system is essential for debugging, auditing, and understanding how your app runs in isolation. Whether you’re using docker exec for live interaction or docker cp to extract files, Docker gives you the tools to inspect containers like a pro.

💡 Pro tip: Always start with docker exec -it <container> bash for live troubleshooting. It’s like SSH, but faster and lighter!

Sharing Is Caring:

Leave a Comment