Sub topic 5
Looking inside a running container with docker exec
docker exec runs a new process inside a container that is already running. This chapter covers how to use it to inspect a live container, what it can and cannot tell you, and the errors you will hit most often.
This chapter assumes you can read docker ps and docker inspect output, know the difference between ENTRYPOINT and CMD, and are comfortable with basic Linux commands such as ls, cat, ps, and curl. It also assumes you know that a container is a set of isolated processes, not a virtual machine.
What docker exec does
When you run docker exec, the daemon starts a second process in the container's existing namespaces and cgroup. The new process sees the same filesystem, network, and process table as the main process (PID 1). It does not restart anything and does not change the container's main command.
Three consequences follow from this:
- The container must be in the
runningstate. You cannot exec into a stopped, created, or restarting container. - The exec process is not the container's main process. When it exits, the container keeps running.
- The exec process starts with the image's environment and the container's configured user and working directory, unless you override them. It does not inherit variables that the main process set for itself at runtime.
Opening a shell
The most common use is an interactive shell:
docker exec -it web sh
docker exec web env
docker exec web ps auxThe flags matter:
-ikeeps standard input open, so you can type.-tallocates a pseudo-terminal, which gives you a prompt and line editing.
Use -it together for an interactive shell. Leave both off for a single command whose output you want to pipe or capture in a script. Using -t in a script or pipeline can add carriage returns to the output.
Try sh first. Many images, such as Alpine, include sh but not bash. If sh works and you want more comfort, try bash.
Choosing the user, directory, and environment
By default the exec process runs as the container's configured user. If the image sets USER app, you get app, which may lack permission to read what you want to check. Override it for the one command:
docker exec --user root web id
docker exec --user 0 web ls -l /var/log/app
docker exec --workdir /app web ls -la
docker exec --env DEBUG=1 web printenv DEBUGSwitching to root helps you tell a permission problem from a missing file. It also changes what you are testing. If a file is readable as root but not as app, the application's own user is the one that matters, so repeat the check as that user:
docker exec --user app web cat /app/config.ymlWhat to check first
Once you are inside, work from the cheapest, most informative checks. These answer most questions.
| Question | Command |
|---|---|
| What is the main process doing? | `ps aux` |
| What environment does the app see? | `env` |
| Does the config file exist and have the right content? | `ls -l /app` and `cat /app/config.yml` |
| Is the app listening on the expected port? | `ss -ltnp` or `netstat -ltn` |
| Does the app respond locally? | `curl -sv http://localhost:8080/health` |
| Can it resolve and reach other services? | `getent hosts db` and `curl -sv http://db:5432` |
| Is the disk full? | `df -h` |
| Who am I? | `id` |
Many small images lack some of these tools. If ss is missing, read /proc/net/tcp directly. If ps is missing, list /proc:
docker exec web cat /proc/net/tcp
docker exec web sh -c 'for p in /proc/[0-9]*; do echo "$p $(tr "\0" " " < $p/cmdline)"; done'Ports in /proc/net/tcp are in hexadecimal. Port 8080 appears as 1F90, and a state of 0A means LISTEN.
Reading the main process's view
PID 1 is special. Its environment and command line are visible under /proc/1:
docker exec web cat /proc/1/cmdline
docker exec web sh -c 'tr "\0" "\n" < /proc/1/environ'This shows the arguments and environment the main process was actually started with. It is useful when a shell wrapper, an entrypoint script, or a docker run -e flag changed what you expected. Reading /proc/1/environ may need root, because the file is owned by the process's user.
The difference matters. docker exec web env shows the container's configured environment. An entrypoint script may have exported more variables or unset others before starting the app. /proc/1/environ reflects the state at the moment PID 1 was executed, so an export done later in the app does not appear there. Check both when a setting seems ignored.
Running commands without a shell
You do not need a shell to run a command. Pass the program directly:
docker exec web ls -l /app
docker exec web cat /etc/resolv.confThere is no shell expansion in this form. Pipes, globs, and redirections are handled by your host shell, not the container's. For example, docker exec web cat /app/*.log expands * on the host. To expand inside the container, wrap the command:
docker exec web sh -c 'cat /app/*.log | tail -n 20'Quote the whole command string so your host shell passes it intact. Also quote any untrusted value you interpolate into it.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `container ... is not running` | The container exited, was never started, or is paused or restarting. | Check `docker ps -a` and use the failed-start techniques from the next chapter. |
| `OCI runtime exec failed: exec: "bash": executable file not found in $PATH` | The image has no `bash`. | Try `sh`. If no shell exists, share namespaces with a tooling container. |
| `the input device is not a TTY` | You used `-t` where no terminal exists, for example in CI. | Drop `-t`, or use `-i` alone. |
| `permission denied` on a file | The exec user cannot read it. | Check with `--user root`, then fix ownership or mode. |
| The container restarts while you are inside | A restart policy or crash loop is replacing it. | Your session ends. See the chapter on restart policies. |
Limits and gotchas
- Changes are not durable. Files you create or edit in the container's writable layer vanish when the container is removed. Never treat a hand edit as a fix. Make the change in the image, the Compose file, or a mounted file.
- A shell changes what you observe. Installing a tool such as
curlwithapt-getorapk addchanges the filesystem and may use memory. Use this to investigate, then discard the container. - You are not the app. The exec process has its own PID and may have a different user, working directory, or environment. A command that works for you can still fail for the app.
- Exec does not reproduce a start-up failure. If the container never gets to
running, there is nothing to exec into. - Exec processes die with the container. If the container stops, any exec session ends with it.
- Resource use counts. Exec processes share the container's memory limit. A heavy debugging command in a container near its limit can trigger an out-of-memory kill.
Using exec in a debugging session
A short, repeatable sequence works well:
- Confirm the container is running with
docker ps. - Run
docker exec web ps auxto see whether the main process is the one you expect. - Check
envand the config files the app reads. - Test the app from the inside with
curl http://localhost:PORT. - Test its dependencies by name, for example
getent hosts db. - If something fails, repeat the same step as the app's user and as root to separate permission problems from missing files.
Keep notes of each command and result. When a later step contradicts an earlier one, the notes show which assumption failed.
Exercise
A container named api runs as USER app. docker exec api cat /app/secret.txt fails with "permission denied", but docker exec --user root api cat /app/secret.txt works. What does this tell you, and what should you check next?
Show answer
The file exists and is readable by root but not by the app user, so the cause is ownership or mode, not a missing file. Run docker exec api ls -l /app/secret.txt and docker exec api id to compare the file's owner, group, and mode with the app user's UID and groups. Then fix the ownership or permissions in the image or the mount, since a change made inside the running container is lost when it is removed.
Exercise
You run docker exec web cat /app/*.log and get "No such file or directory", although ls /app inside an interactive shell shows server.log. Why, and how do you fix it?
Show answer
Your host shell expanded * before Docker ran the command, and the host has no matching files, so cat received the literal pattern or nothing useful. Wrap the command so the container's shell expands it: docker exec web sh -c 'cat /app/*.log'.