Last active
September 24, 2019 07:50
-
-
Save kitt1987/65be0bc158921e76aaf7363a1e56fa20 to your computer and use it in GitHub Desktop.
Show docker container with respect to a host PID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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