Skip to content

Instantly share code, notes, and snippets.

@bencord0
Created March 31, 2019 11:51
Show Gist options
  • Save bencord0/5535040ae834a4af95d3c11ebee0d065 to your computer and use it in GitHub Desktop.
Save bencord0/5535040ae834a4af95d3c11ebee0d065 to your computer and use it in GitHub Desktop.
NSEnter into a docker container
#!/bin/bash
CONTAINER_NAME="$1"
exec nsenter --target $(
docker inspect --format "{{.State.Pid}}" $(
docker ps -q --filter name="${CONTAINER_NAME}"
)
) --mount --uts --ipc --net --pid
@bencord0
Copy link
Author

Count file descriptors in all docker containers

for CID in $(docker ps -q); do
 echo -n "${CID}: "
 nsenter --target $(
    docker inspect --format {{.State.Pid}} $CID
  ) --uts --ipc --net --pid --mount sh -c 'ls /proc/*/fd \
| grep -v -e : -e ^\$ \
| wc -l';
done

@bencord0
Copy link
Author

note: doesn't work for single-binary containers, e.g. many standalone go binaries are distributed without /bin/sh.

@VSpike
Copy link

VSpike commented Mar 31, 2019

We should probably check fds against limit per process. Something like.

for p in /proc/[0-9]*; do
	fds="$(ls $p/fd | wc -l)"
	limit="$(sed -n 's/Max open files/max_open_files/p' /proc/1/limits | awk '{print $2}')"
	printf 'Pid: %s fds: %s limit: %s Used: %f\n' "$p" "$fds" "$limit" "$((fds/limit*100))"
done

Maybe track the maximum percentage in a container?

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