Skip to content

Instantly share code, notes, and snippets.

@ziozzang
Last active August 29, 2015 14:22
Show Gist options
  • Save ziozzang/80b7a7bffc185b4d507b to your computer and use it in GitHub Desktop.
Save ziozzang/80b7a7bffc185b4d507b to your computer and use it in GitHub Desktop.
sync docker container network namespace / bash version
#!/bin/bash
# Code by Jioh L. Jung (ziozzang@gmail.com)
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