How to read files and stdout from a running Docker container
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reading output from a running Docker container is a normal debugging task, but Docker exposes logs and files through different mechanisms. Standard output and standard error come through docker logs, while regular files inside the container are read with docker exec or copied out with docker cp.
Read stdout and stderr with docker logs
If the application writes to standard output or standard error, use:
To follow logs in real time:
Useful options include:
This is the fastest way to inspect runtime behavior when the containerized process follows the usual container logging pattern.
docker logs is not file access
A common source of confusion is expecting docker logs to read arbitrary files from the container filesystem. It cannot. docker logs only shows what the main process wrote to stdout and stderr.
If the application writes to a file such as /app/logs/app.log, Docker will not show that file automatically unless the application also forwards it to stdout. That distinction matters because many legacy apps still write only to files.
Read files with docker exec
To inspect a file in the running container, execute a command inside it:
For large files, use tail:
To follow a file continuously:
This is usually the right approach when the app logs to files instead of stdout.
Open an interactive shell when needed
If the image contains a shell, you can inspect the container more broadly:
Or:
Once inside, you can list directories, inspect permissions, and open files with the normal shell tools available in that image.
Copy files out of the container
If you want to examine a file locally, copy it out:
You can also copy a directory:
This is often easier than staying inside the container when you want to search, archive, or compare files on the host.
Know the container state
If you are not sure which container to inspect, start with:
If the container already exited, docker logs may still work, but docker exec will not because there is no running process namespace to enter. That difference explains why log access sometimes still succeeds after a crash while file inspection does not.
Prefer stdout logging when you control the app
For most containerized applications, writing logs to stdout and stderr is the better operational model. It makes docker logs useful immediately and helps orchestration platforms collect logs without extra file management.
File-based logging still has valid use cases, but if the application is under your control, stdout is usually the simplest default.
Common Pitfalls
- Expecting
docker logsto show arbitrary files in the container filesystem. - Trying
docker execagainst a stopped container. - Assuming
bashexists in minimal images when onlyshor no shell is available. - Keeping logs only in files when stdout logging would make container operations simpler.
Summary
- Use
docker logsfor stdout and stderr from the container’s main process. - Use
docker execto read files inside a running container. - Use
docker cpto copy files or directories out of the container. - Prefer stdout logging for simpler container debugging and log collection.

