Skip to content

Instantly share code, notes, and snippets.

@jarek-przygodzki
Forked from yangyuqian/linkns.sh
Last active March 22, 2018 21:07
Show Gist options
  • Save jarek-przygodzki/e306dd48a00d42e244f1f1f8f4281153 to your computer and use it in GitHub Desktop.
Save jarek-przygodzki/e306dd48a00d42e244f1f1f8f4281153 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
#!/bin/bash
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
rm /var/run/netns/$cid
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment