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 filesC
for changed filesD
for deleted files
🧭 Directory Structure Inside a Container
Docker containers are based on Linux and follow a standard Linux file system structure:
Directory | Description |
---|---|
/ | Root directory |
/app | Often contains your application code |
/etc | System configuration files |
/var/log | Log files |
/usr , /bin | Binaries and system packages |
/tmp | Temporary files |
✅ Summary of Useful Commands
Task | Command |
---|---|
Open interactive shell in container | docker exec -it <container> bash |
List container files | docker exec -it <container> ls /path |
Copy file from container to host | docker cp <container>:/file/path ./local-dir |
View changes to container filesystem | docker diff <container> |
Inspect container metadata | docker 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!