Sub topic 7
Running a command in a running container
This chapter assumes you can start containers with docker run, list them with docker ps, and stop them with docker stop. It also assumes you are comfortable with a Unix shell, pipes, and redirection. Docker must be installed and working.
What docker exec does
docker exec starts a new process inside a container that is already running. The new process shares the container's filesystem, network, and process namespace. It is not the container's main process. The container keeps running whether or not the exec process finishes.
The basic form is:
docker exec CONTAINER COMMAND [ARGS...]CONTAINER is a name or ID. COMMAND must exist as an executable inside the container's image. Your host's tools are not available there.
A first example
Start an nginx container, then run commands inside it.
docker run -d --name web -p 8080:80 nginx
docker exec web nginx -t
docker exec web ls -l /var/log/nginxThe first docker exec asks nginx to check its own configuration and prints the result. The second lists the log directory. In the official nginx image, the files there are links to stdout and stderr. This is why docker logs can show nginx output.
Each call starts a process, prints its output to your terminal, and exits. The web container is unaffected.
Output and standard streams
The command's stdout and stderr are connected to your terminal, so you can use pipes and redirection on the host side:
docker exec web ls /etc/nginx | grep confHere ls runs in the container, and grep runs on your host. If you need the pipe to run inside the container, wrap the command in a shell:
docker exec web sh -c 'ls /etc/nginx | grep conf'Quote the command so your host shell does not interpret the pipe first.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `No such container: web` | The name is wrong or the container was removed. | Check `docker ps -a`. |
| `container ... is not running` | The container has stopped or exited. | Start it with `docker start`, or read `docker logs` to learn why it exited. |
| `executable file not found in $PATH` | The command is not installed in the image. | Use a tool the image has, or a different image. |
| `sh: 1: ...: not found` inside `sh -c` | The program named inside the quoted string is missing. | Check the spelling and the image contents. |
Slim and minimal images often lack ps, curl, and even bash. Do not assume a tool exists.
Exit codes
docker exec returns the exit code of the command it ran. Scripts can use this directly:
docker exec web sh -c 'exit 3'
echo $?The second line prints 3.
Exercise
You run docker exec web ls /var/log/nginx | wc -l. Where do ls and wc each run, and how would you run both inside the container?
Show answer
ls runs inside the container, and wc runs on your host, because the pipe is handled by your host shell. To run both inside the container, wrap them in a shell: docker exec web sh -c 'ls /var/log/nginx | wc -l'.
Exercise
A script runs docker exec web nginx -t and must stop if the configuration is invalid. How does it detect that?
Show answer
It checks the exit code with $?. docker exec returns the exit code of nginx -t, which is 0 on success and non-zero when the configuration is invalid, so the script can test $? or use if ! docker exec web nginx -t; then ....