Skip to content

Instantly share code, notes, and snippets.

@brendangregg
Last active August 23, 2023 10:11
Show Gist options
  • Save brendangregg/1abcfeef9155ac526197f6f0abdd86bf to your computer and use it in GitHub Desktop.
Save brendangregg/1abcfeef9155ac526197f6f0abdd86bf to your computer and use it in GitHub Desktop.
docker ps --namespaces
#!/bin/bash
#
# dockerpsns - proof of concept for a "docker ps --namespaces".
#
# USAGE: ./dockerpsns.sh
#
# This lists containers, their init PIDs, and namespace IDs. If container
# namespaces equal the host namespace, they are colored red (this can be
# disabled by setting color=0 below).
#
# Copyright 2017 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 10-Apr-2017 Brendan Gregg Created this.
namespaces="cgroup ipc mnt net pid user uts"
color=1
declare -A hostns
printf "%-14s %-20s %6s %-16s" "CONTAINER" "NAME" "PID" "PATH"
for n in $namespaces; do
printf " %-10s" $(echo $n | tr a-z A-Z)
done
echo
# print host details
pid=1
read name < /proc/$pid/comm
printf "%-14s %-20.20s %6d %-16.16s" "host" $(hostname) $pid $name
for n in $namespaces; do
id=$(stat --format="%N" /proc/$pid/ns/$n)
id=${id#*[}
id=${id%]*}
hostns[$n]=$id
printf " %-10s" "$id"
done
echo
# print containers
for UUID in $(docker ps -q); do
# docker info:
pid=$(docker inspect -f '{{.State.Pid}}' $UUID)
name=$(docker inspect -f '{{.Name}}' $UUID)
path=$(docker inspect -f '{{.Path}}' $UUID)
name=${name#/}
printf "%-14s %-20.20s %6d %-16.16s" $UUID $name $pid $path
# namespace info:
for n in $namespaces; do
id=$(stat --format="%N" /proc/$pid/ns/$n)
id=${id#*[}
id=${id%]*}
docolor=0
if (( color )); then
[[ "${hostns[$n]}" == "$id" ]] && docolor=1
fi
(( docolor )) && echo -e "\e[31;1m\c"
printf " %-10s" "$id"
(( docolor )) && echo -e "\e[0m\c"
done
echo
done
@OlegKorchagin
Copy link

thanks, very useful script

by the way, if docker run with systemd cgroup driver ( https://kubernetes.io/docs/setup/production-environment/container-runtimes/ ), there is no "cgroup" namespace

[root@vm-docker-test-n2 ok]# ./dockerpsns.sh
CONTAINER      NAME                    PID PATH             CGROUP     IPC        MNT        NET        PID        USER       UTS
host           vm-docker-test-n2.ro      1 systemd         stat: cannot stat ‘/proc/1/ns/cgroup’: No such file or directory
            4026531839 4026531840 4026531956 4026531836 4026531837 4026531838
01386f5db3b5   k8s_nginx_nginx-86c5   5944 nginx           stat: cannot stat ‘/proc/5944/ns/cgroup’: No such file or directory
            4026532533 4026532636 4026532536 4026532638 4026531837 4026532637

[root@vm-docker-test-n2 ok]# ls /proc/5944/ns/
ipc  mnt  net  pid  user  uts

it might be worth adding " 2>/dev/null || echo 'N/A'" after stat:

--- dockerpsns.sh.old   2020-01-23 17:25:41.885868947 +0300
+++ dockerpsns.sh       2020-01-23 17:26:30.557443329 +0300
@@ -28,7 +28,7 @@
 read name < /proc/$pid/comm
 printf "%-14s %-20.20s %6d %-16.16s" "host" $(hostname) $pid $name
 for n in $namespaces; do
-    id=$(stat --format="%N" /proc/$pid/ns/$n)
+    id=$(stat --format="%N" /proc/$pid/ns/$n 2>/dev/null || echo 'N/A')
     id=${id#*[}
     id=${id%]*}
     hostns[$n]=$id
@@ -47,7 +47,7 @@

     # namespace info:
     for n in $namespaces; do
-        id=$(stat --format="%N" /proc/$pid/ns/$n)
+        id=$(stat --format="%N" /proc/$pid/ns/$n 2>/dev/null || echo 'N/A')
         id=${id#*[}
         id=${id%]*}
         docolor=0
@@ -60,4 +60,4 @@
     done
     echo

-done
\ No newline at end of file
+done
[root@vm-docker-test-n2 ok]# ./dockerpsns.sh
CONTAINER      NAME                    PID PATH             CGROUP     IPC        MNT        NET        PID        USER       UTS
host           vm-docker-test-n2.ro      1 systemd          N/A        4026531839 4026531840 4026531956 4026531836 4026531837 4026531838
01386f5db3b5   k8s_nginx_nginx-86c5   5944 nginx            N/A        4026532533 4026532636 4026532536 4026532638 4026531837 4026532637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment