Search code examples
linuxdockercontainerspid

Why do pids jump in a container?


Entering a container (e.g. docker run or docker exec) makes the PID of next created process jump ahead, why is that?

For example, in the image below the second process in the container (ps) is assigned PID 10, not PID 2:

example

According to this answer, Linux appears to be allocating PIDs in a sequence, is that not always the case?


Solution

  • Talked with once of runc's maintainers, Aleksa Sarai, and he explained why this is happening.

    By design, the golang runtime spawns several threads to manage a process. runc is written in golang, and when building/execing into the container, there is a short time where the runc process is running inside the container (before execing the user requested executable, e.g. bash in docker exec bash). In Linux, threads and processes are both identified with ids from the same pool, so the go runtime threads are counted in the container pid namespace, leading to the pid jump I described.