Sub topic 10
exec, run, and attach compared
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 know that a container has one main process, the one that was started when the container was created. Mixing up exec, run, and attach is a common source of confusion and accidental data loss.
Three commands, three jobs
The three commands differ in what they do to the container and to its main process.
docker runcreates a new container from an image and starts its main process.docker execstarts an additional process inside a container that is already running.docker attachconnects your terminal to the main process of a running container. It starts nothing.
docker run -d --name web -p 8080:80 nginx
docker exec web nginx -t
docker attach webThe first command creates the container web. The second starts a short-lived nginx -t process next to the main nginx process. The third connects your terminal to the main nginx process itself.
How they relate to the container
The extra process started by exec shares the container's filesystem, network, and process namespace. It is not the main process, so when it ends, the container keeps running.
Why attach is risky
docker attach shows the main process's stdin, stdout, and stderr. Because you are wired directly to that process, these things can surprise you:
- Pressing
Ctrl+CsendsSIGINTto the main process. For many programs this stops the container. - Typing
exitin an attached shell ends the main process, so the container stops. - If several terminals attach to the same container, they all see the same stream.
- The output you see starts from the moment you attach. Earlier output is available with
docker logs.
To leave an attached session without stopping the container, press the detach keys, Ctrl+P followed by Ctrl+Q. This works only when the container was started with -i and -t. You can also pass --sig-proxy=false to docker attach, so that Ctrl+C is not forwarded to the process.
Choosing the right command
| Goal | Command |
|---|---|
| Start a new container from an image | `docker run` |
| Run a one-off command in a running container | `docker exec` |
| Open a shell in a running container | `docker exec -it NAME sh` |
| Read what the main process has already printed | `docker logs NAME` |
| Watch the main process's output live | `docker logs --follow NAME` |
| Interact with the main process's stdin | `docker attach NAME` |
For most inspection work, docker logs --follow is a safer replacement for attach. It shows the same output, and Ctrl+C stops only the docker logs client, not the container.
A worked comparison
Run a container with an interactive shell as its main process:
docker run -dit --name box alpine shNow compare the two ways of reaching it.
docker exec box ps
docker exec -it box shThe ps output lists two processes: the main sh and the ps itself. In the second command you get a second shell. Typing exit there leaves box running. You can confirm with docker ps.
Now attach instead:
docker attach boxYou are in the main shell. If you type exit, the main process ends and docker ps no longer lists box. Use docker ps -a to see it with the status Exited.
Another difference is that exec works only while the container runs. docker run creates a fresh container every time, so changes you made through an earlier container are not there unless they came from the image or a volume.
Exercise
You started a container with docker run -dit --name box alpine sh. You want a shell to look around, and you want the container to keep running after you leave. Which command do you use, and why not docker attach box?
Show answer
Use docker exec -it box sh. It starts a new shell process beside the main process, so exiting it leaves the container running. docker attach box connects you to the main process, so typing exit there stops the container.