#! /bin/bash
set -x
set -e
# it is unlikely we'd use the notary
unset DOCKER_CONTENT_TRUST
# contaienrID of course.
CID=`docker run -d ubuntu:14.04 sleep 10000`
PID=`docker inspect -f "{{.State.Pid}}" $CID`
DEVICE_NAME="eth1"
# Use the bridge Ip, "docker0" for example, as the default gateway for container
Bridge=docker0
#BridgeIP=`ifconfig $Bridge | grep inet | grep -v inet6 | awk '{print $2}' |tr -d "addr:"`
BridgeIP="172.17.0.1"
mkdir -p /var/run/netns
# /var/run/netns/xxx is what `ip` wanted.
ln -s /proc/$PID/ns/net /var/run/netns/$PID
# set up veth pair A and B, attach A to bridge-docker0.
ip link add A type veth peer name B
brctl addif $Bridge A
ip link set A up
# add B into container's network namespace and hack it.
ip link set B netns $PID
ip netns exec $PID ip link set dev B name $DEVICE_NAME
ip netns exec $PID ip link set $DEVICE_NAME address 12:34:56:78:9a:bb
ip netns exec $PID ip addr add 172.17.42.101/16 dev $DEVICE_NAME
ip netns exec $PID ip link set $DEVICE_NAME up
#ip netns exec $PID ip route add default via $BridgeIP
# just in case we want do some thing
sleep 30
ip netns exec $PID ip link delete $DEVICE_NAME
sleep 30
# teardown
docker stop $CID
docker rm $CID
find -L /var/run/netns -type l -delete
ip link delete A
The test script would be: