Skip to content

Instantly share code, notes, and snippets.

@kitt1987
Last active September 24, 2019 07:50
Show Gist options
  • Save kitt1987/65be0bc158921e76aaf7363a1e56fa20 to your computer and use it in GitHub Desktop.
Save kitt1987/65be0bc158921e76aaf7363a1e56fa20 to your computer and use it in GitHub Desktop.
Show docker container with respect to a host PID
function findc {
local pid=$1
local containerLabel=$(head -1 /proc/11212/cgroup | sed -E 's|.*pod([0-9a-z\-]*).*|\1|')
if [[ "${containerLabel}" == "" ]]; then
echo "no container found with regards to PID $pid"
exit 2
fi
local matchedID=$(docker ps -a | grep "${containerLabel}" | awk '{ print $1 }' 2>/dev/null)
while read -r id; do
pidInContainer=$(docker inspect -f '{{.State.Pid}}' $id)
if [[ "${pidInContainer}" == "${pid}" ]]; then
docker ps -f "id=${id}"
return
fi
done <<< "${matchedID}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment