Skip to content

Instantly share code, notes, and snippets.

@jsidhu
Created October 6, 2017 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsidhu/d6924f549df7f2f440a6e8af109f9d08 to your computer and use it in GitHub Desktop.
Save jsidhu/d6924f549df7f2f440a6e8af109f9d08 to your computer and use it in GitHub Desktop.
shell script to find docker container from process id
#!/bin/bash
cpid=$1
while true; do
ppid=$(ps -o ppid= -p $cpid)
pname=$(ps -o comm= -p $ppid)
if [ "$pname" == "docker" ]; then
echo "$cpid parent $ppid ($pname)"
break
else
echo "$cpid parent $ppid ($pname)"
cpid=$ppid
fi
done
docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep $cpid
@mimmus
Copy link

mimmus commented Jul 28, 2022

Same using containerd instead of docker:

#!/bin/bash

cpid=$1
while true; do
        ppid=$(ps -o ppid= -p $cpid)
        pname=$(ps -o comm= -p $ppid)
        if [ "$pname" == "containerd-shim" ]; then
                echo "$cpid parent $ppid ($pname)"
                break
        else
                echo "$cpid parent $ppid ($pname)"
                cpid=$ppid
        fi
done

crictl ps -q | xargs crictl inspect --output go-template --template '{{.info.pid}}, {{.status.metadata.name}}' | grep $cpid

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