Checks / Docker and containers
A container shares the host PID namespace
- Check
docker.host_pid- Severity
- medium
- Runs
- on the server, in the daily run of the agent
- Looks at
- PidMode is host
What the check looks at
HostConfig.PidMode of every running container (docker inspect). The check fails for each container where it
is host. The setting comes from --pid=host on docker run or from pid: host in a Compose file.
Risk
With --pid=host the container shares the process list of the host. In a test on Docker 29.9:
- the container saw all processes of the host, and
/proc/1/commwassystemd; - it could read their command lines, as
ps -efshows them, with any tokens or passwords given as arguments; kill -0 1succeeded from a root process in the container, on a host without an AppArmor profile for containers, so the container could send signals to host processes and stop them;- reading
/proc/1/environwas denied, because the container lacksCAP_SYS_PTRACE.
SYS_PTRACE or --privileged removes that limit: capabilities(7) describes CAP_SYS_PTRACE as the right to
"trace arbitrary processes using ptrace(2)".
How to fix it
- Check why the container has it. Monitoring agents that report on host processes and some debugging tools need it; applications and databases do not.
- Remove
pid: hostfrom the Compose file, or--pid=hostfrom thedocker runcommand. - Recreate the container:
docker compose up -d --force-recreate <service>.
If two containers need to see the processes of one another (a sidecar that debugs an application), share the
namespace of that one container with --pid=container:<name>, and not the one of the host.
How to check the fix
docker ps -q | xargs -r docker inspect --format '{{.Name}} pid={{.HostConfig.PidMode}}' | grep 'pid=host'
The output must be empty.
When a warning is fine
A monitoring agent that reports per-process figures of the host needs it, and so does a container you start for
a debugging session. If the image comes from a source you trust and runs without extra capabilities, accept the
result for that container on the dashboard. Add --privileged or SYS_PTRACE only if the tool needs that too.
An accepted result stays out of the digest and the counts until it changes.
Sources
- Docker docs: docker container run, --pid
- Docker docs: Isolate containers with a user namespace. The host PID namespace does not work together with user namespace remapping.
- proc(5) (
/proc/[pid]/environneeds a ptrace access mode check)