The web container is running, so you can run commands in it and look at its processes from the inside.
docker exec <name> <command> starts one more command in a running container. The process list from the inside:
The first line is PID 1, httpd -f -p 8080: inside the container the main process comes first. sudo docker exec -it web sh gives you a shell inside: -i and -t connect your input and terminal to it, and exit leaves it.
From the server itself the same process shows up in the ordinary process list, under its own number:
A container is a set of the server's processes whose view the kernel has limited (namespaces and cgroups): they see only their own processes and the files of their image. docker stop stops the main process, and docker start starts the same container again.
The box container shows Exited (0) 5 minutes ago in docker ps -a. What will sudo docker exec box ls / print?
-
The command never reaches the container: Docker refuses with an
is not runningerror and a non-zero code. -
docker execadds a command to the processes of a running container. A stopped one has no processes, so Docker answersis not running. -
docker execdoes not start a container by itself. To start it again usesudo docker start box, and only thenexec.
docker exec runs a command next to the main process of a running container. A stopped container has no processes: docker start first, then exec.
Key points
-
docker execruns one more command in a running container;docker exec -it … shgives a shell inside. -
Inside a container the main process is PID 1, while on the server
psshows it under its own number. - A container is a set of the server's processes with a limited view: their own processes and their image's files.