Skip to content

fix: show Pid as 0 for stopped containers in `nerdctl container ins… - #4965

Merged
AkihiroSuda merged 1 commit into
containerd:mainfrom
haytok:stopped-container-pid
Jun 15, 2026
Merged

fix: show Pid as 0 for stopped containers in `nerdctl container ins…#4965
AkihiroSuda merged 1 commit into
containerd:mainfrom
haytok:stopped-container-pid

Conversation

@haytok

@haytok haytok commented Jun 14, 2026

Copy link
Copy Markdown
Member

…pect`

In Docker, running docker container inspect on a stopped container shows the Pid as 0.

> 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.

> 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.

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.

…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
haytok force-pushed the stopped-container-pid branch from 758c309 to 0925492 Compare June 14, 2026 13:18
@AkihiroSuda AkihiroSuda added this to the v2.3.2 milestone Jun 15, 2026

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda
AkihiroSuda merged commit c2f0192 into containerd:main Jun 15, 2026
87 of 96 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants