Sub topic 12
What exec cannot do
This chapter assumes you can run docker run, docker ps, and docker stop, and that you have opened a shell with docker exec -it. It covers three limits of docker exec that cause most confusion: it needs a running container, it needs the program you ask for inside the image, and changes you make with it do not last.
It needs a running container
docker exec starts a new process next to the container's main process. If the main process has exited, the container is stopped and there is nothing to attach to. Docker refuses the request.
docker run -d --name web -p 8080:80 nginx
docker stop web
docker exec web nginx -tThe last command fails with an error saying the container is not running. Use docker logs web instead. Logs stay readable after a container stops, as long as the container has not been removed and the logging driver supports reading.
A container that restarts in a loop is a harder case. It is running only for a moment, so exec may work once and fail the next time. Read the logs first to find out why it exits.
It needs the program inside the image
exec runs a program from the container's own filesystem. It does not use tools from your host. Slim and distroless images often have no bash, and some have no shell at all.
docker exec -it web bash
docker exec -it web shIf the first command fails with an error that bash is not found in $PATH, try sh. If sh is missing too, the image has no shell, and you cannot open one with exec. Tools such as ps, curl, and ping are also often missing from small images.
Changes do not last
A container has a thin writable layer on top of its image. Files you edit through exec land in that layer. When the container is removed and recreated, for example after docker rm or a new docker run, the layer is gone and the container starts again from the original image.
docker exec web sh -c 'echo hello > /tmp/note.txt'
docker exec web cat /tmp/note.txt
docker rm -f web
docker run -d --name web -p 8080:80 nginx
docker exec web cat /tmp/note.txtThe second command prints hello. The last one fails, because the new container never had the file. A stop and start of the same container keeps the file, but removal does not.
To make a change permanent, change the Dockerfile or the configuration, then rebuild or recreate the container. Use a volume for data that must outlive the container.
Other limits
execdoes not change the main process. It cannot restart it, and killing your exec process does not stop the container.- Environment variables set with
-eexist only for that one process. - A process started with
execis not part of the container's logs. Its output goes to your terminal, not todocker logs. - You cannot change ports or mounts with
exec. Those are fixed when the container is created.
Common errors
| Message or symptom | Likely cause | What to do |
|---|---|---|
| Container is not running | Main process exited | Run `docker logs`, fix the cause, restart |
| Executable file not found in `$PATH` | Program missing from image | Try `sh`, or rebuild with the tool |
| File missing after recreate | Edit lived in the writable layer | Put the change in the image or a volume |
Exercise
You edited /etc/nginx/nginx.conf with docker exec, and it worked. After the container was removed and recreated, the old configuration is back. Why, and what is the correct fix?
Show answer
The edit was made in the container's writable layer, which is deleted when the container is removed. The new container starts from the original image. Put the configuration in the image (through the Dockerfile) or mount it from the host or a volume, then recreate the container.
Exercise
docker exec -it app bash prints an error that bash is not found. What do you try next?
Show answer
Try docker exec -it app sh. If that fails too, the image has no shell, so you cannot open one with exec. Use docker logs and rebuild the image with a shell for debugging.