fix: show Pid as 0 for stopped containers in `nerdctl container ins… - #4965
Merged
Conversation
…pect`
In Docker, running `docker container inspect` on a stopped container shows
the Pid as `0`.
```bash
> docker ps -a --filter=name=nginx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8dcc84e427f nginx "/docker-entrypoint.…" 14 seconds ago Exited (0) 8 seconds ago nginx
> docker container inspect nginx | grep 'Pid"'
"Pid": 0,
```
However, running `nerdctl container inspect` on a stopped container still
shows a stale Pid from the already-exited process.
```bash
> sudo nerdctl ps -a --filter=name=nginx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ce4cb9b1ed1 docker.io/library/nginx:latest "/docker-entrypoint.…" 16 seconds ago Exited (0) 4 seconds ago nginx
> sn container inspect nginx | grep 'Pid"'
"Pid": 1531853,
```
The docker daemon subscribes to containerd's events and when it detects a
task exit event, it deletes the task and updates the state of the
container it manages, setting Pid to `0`. Therefore, in Docker the Pid
becomes `0` after the container stops.
- https://github.com/moby/moby/blob/docker-v29.5.3/daemon/monitor.go#L34
On the other hand, nerdctl has no long-running daemon like the docker
daemon, so it does not subscribe to containerd's events. As a result, the
task is not deleted even after the container stops, and
`nerdctl container inspect` still shows the stale Pid of the
already-exited process returned by `task.Pid()`.
Therefore, this commit changes the behavior so that
`nerdctl container inspect` on a stopped container shows the Pid as `0`,
for compatibility with Docker.
Signed-off-by: Hayato Kiwata <dev@haytok.jp>
haytok
force-pushed
the
stopped-container-pid
branch
from
June 14, 2026 13:18
758c309 to
0925492
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…pect`
In Docker, running
docker container inspecton a stopped container shows the Pid as0.However, running
nerdctl container inspecton a stopped container still shows a stale Pid from the already-exited process.The docker daemon subscribes to containerd's events and when it detects a task exit event, it deletes the task and updates the state of the container it manages, setting Pid to
0. Therefore, in Docker the Pid becomes0after the container stops.On the other hand, nerdctl has no long-running daemon like the docker daemon, so it does not subscribe to containerd's events. As a result, the task is not deleted even after the container stops, and
nerdctl container inspectstill shows the stale Pid of the already-exited process returned bytask.Pid().Therefore, this commit changes the behavior so that
nerdctl container inspecton a stopped container shows the Pid as0, for compatibility with Docker.