Sub topic 5
Looking inside a running container with docker exec
This chapter assumes you can run containers, read docker ps output, and use basic Linux commands such as ls, cat, and ps. You should also know the difference between an image's ENTRYPOINT and CMD.
What docker exec does
docker exec starts a new process inside a container that is already running. The new process joins the container's namespaces and filesystem view. It does not replace the main process (PID 1), and it does not restart anything.
This makes it the main tool for checking what the application sees: its files, environment variables, users, and processes.
Opening a shell
Use -i to keep standard input open and -t to allocate a terminal. Together they give you an interactive shell.
docker exec -it web sh
docker exec web env
docker exec web ps auxThe first command opens a shell in the container named web. Prefer sh because it exists in nearly every image. Use bash only after you have confirmed it is installed. The next two commands run one command each and print the result. They need no -it because they are not interactive.
When you leave the shell with exit, only your debugging process ends. The container keeps running.
What to check first
Once inside, look at the things that most often explain a failure:
- Environment:
envshows which variables the application actually received. A missing or misspelled variable is a common cause of bad behavior. - Processes:
ps auxshows whether the main process is running and what command line it was given. - Files:
ls -landcatshow whether configuration files exist, and whether the application user can read them. - Identity:
idshows the user and groups the shell runs as. - Connectivity:
curlagainstlocalhosttests whether the service is listening inside the container.
Useful options
| Option | Purpose | Example |
|---|---|---|
| `-u` | Run as a given user | `docker exec -u root web id` |
| `-w` | Set the working directory | `docker exec -w /app web ls` |
| `-e` | Add an environment variable for this command only | `docker exec -e DEBUG=1 web env` |
Use -u root when the container runs as an unprivileged user and you need to inspect files or install a diagnostic tool. Be aware that the shell's user may differ from the user your application runs as, so a file you can read as root may still be unreadable to the application.
How the pieces fit
Common errors
- "container is not running": The container has exited or is restarting. Check its state with
docker ps -a. - "executable file not found in $PATH": The image has no
shor the command you named. Minimal images often have no shell. Another chapter covers attaching a tooling container for this case. - Changes vanish: Files you edit inside a container are lost when the container is removed. Treat
execas a way to look and test, not a place to apply fixes. Apply the fix in the image or the configuration. - Misleading results: A shell started with
execdoes not inherit variables you set in an earlierexecsession. It sees the container's configured environment, not your previous shell's.
Exercise
A container named api is running, but requests to it fail. You open a shell with docker exec -it api sh and find that ls /app/config.yml succeeds as root. The application still reports it cannot read the file. Which command checks the likely cause, and what are you looking for?
Show answer
Run docker exec -u root api ls -l /app/config.yml and compare it with docker exec api id. You are checking whether the file's owner, group, and permission bits allow the user the application runs as to read it. Root can read nearly any file, so a successful check as root does not prove the application user has access.