Linux Labs

Containers: Docker and compose / Stage 1 of 11

Image, container and process

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 6 · on a machine · 5 min

Inside the container

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:

bash
sudo docker exec web ps

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:

bash
pgrep -a httpd

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.

Check yourself 1 question

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 running error and a non-zero code.

  • docker exec adds a command to the processes of a running container. A stopped one has no processes, so Docker answers is not running.

  • docker exec does not start a container by itself. To start it again use sudo docker start box, and only then exec.

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 exec runs one more command in a running container; docker exec -it … sh gives a shell inside.
  • Inside a container the main process is PID 1, while on the server ps shows 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.
Ubuntu 24.04
Loading…