Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Expose the netns of a docker container to the host.
#!/usr/bin/env bash
if [ "$1" == "" ]; then
echo "usage: $0 <docker_id>"
echo "Exposes the netns of a docker container to the host"
exit 1
fi
ppid=`docker inspect $1 | grep Pid | awk '{print $2 + 0}'`
if [ "$ppid" == "" ]; then
echo "lxc parent pid not found"
exit 1
fi
pid=`ps --ppid $ppid -o pid | tail -n1`
if [ "$pid" == "" ]; then
echo "lxc child pid not found"
exit 1
fi
sudo mkdir -p /var/run/netns
sudo rm -f /var/run/netns/$1
sudo ln -s /proc/$pid/ns/net /var/run/netns/$1
echo "Container netns exposed as $1"
echo "For example try: sudo ip netns exec $1 ip addr"
@genzj

This comment has been minimized.

Copy link

genzj commented Jul 10, 2015

great! that's what I have been searching around for days. they really should consider add this as a official docker command!
how did you figure out this way? any reference or documents? Thanks a lot!

@kaharlichenko

This comment has been minimized.

Copy link

kaharlichenko commented Jan 29, 2016

You could extract the pid of the container with docker inspect directly without any grep/awk magic:

ppid=$(docker inspect --format '{{.State.Pid}}' $1)

Also, why do you query for any child processes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.