Last active
August 29, 2015 14:22
-
-
Save ziozzang/80b7a7bffc185b4d507b to your computer and use it in GitHub Desktop.
sync docker container network namespace / bash version
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
#!/bin/bash | |
# Code by Jioh L. Jung ([email protected]) | |
NETNS=/var/run/netns | |
mkdir -p ${NETNS} | |
function remove_missed() { | |
# Remove purged record. | |
for f in ${NETNS}/* | |
do | |
# Symbolic Link && not exist target | |
if [ -L "$f" ] && [ ! \( -e "$f" \) ] ; then | |
rm -f $f | |
fi | |
done | |
return | |
} | |
remove_missed | |
while true; do | |
# Acquire docker ids. (don't need -a option. fixed) | |
DIDS=`docker ps | tail -n +2 | awk '{print $1}'` | |
for p in ${DIDS} | |
do | |
pid=`docker inspect --format {{.State.Pid}} ${p}` | |
# pid must not 0 | |
if [ "$pid" -ne "0" ]; then | |
# Create Link if not exist | |
if [ ! -e "${NETNS}/${p}" ]; then | |
ln -s /proc/${pid}/ns/net ${NETNS}/${p} | |
fi | |
fi | |
done | |
remove_missed | |
sleep 0.5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment