Skip to content

Instantly share code, notes, and snippets.

@yangyuqian
Last active July 9, 2018 16:48
Show Gist options
  • Save yangyuqian/e25d90adb41a974f3a2e2015e197eba8 to your computer and use it in GitHub Desktop.
Save yangyuqian/e25d90adb41a974f3a2e2015e197eba8 to your computer and use it in GitHub Desktop.
Create symlinks for network namespaces inside docker containers
#!/bin/bash
# This simple script creates symlinks for network namespaces of docker containers,
# then you can interact to that network namespace with built-in iproute2 on linux.
# See iproute2 convention in: http://man7.org/linux/man-pages/man8/ip-netns.8.html
container_ids=`docker ps -aq`
for cid in $container_ids
do
pid=`docker inspect -f '{{.State.Pid}}' $cid`
if [ ! -f "/var/run/netns/$cid" ]; then
ln -s /proc/$pid/ns/net /var/run/netns/$cid
fi
echo "=================== Check network namespaces ==============="
ip netns exec $cid ip addr show eth0
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment