Skip to content

Instantly share code, notes, and snippets.

@olegfedoseev
Created January 12, 2016 07:26
Show Gist options
  • Save olegfedoseev/f1eab2a0ebb4421a521e to your computer and use it in GitHub Desktop.
Save olegfedoseev/f1eab2a0ebb4421a521e to your computer and use it in GitHub Desktop.
How to create docker container with static ip
# create container, important bit is "--net=none"
CID=$(docker run -d --net=none ubuntu-upstart /sbin/init)
# get pid of main proccess
PID=$(docker inspect -f '{{.State.Pid}}' $CID)
# what ip we want for container
IP="192.168.20.167/24 broadcast 192.168.20.255"
# what gateway we shoud use
GATEWAY="192.168.20.1"
# main magic
sudo ln -s /proc/$PID/ns/net /var/run/netns/$PID
sudo ip link add A$PID type veth peer name B$PID
sudo brctl addif br0 A$PID
sudo ip link set A$PID up
sudo ip link set B$PID netns $PID
sudo ip netns exec $PID ip link set dev B$PID name eth0
sudo ip netns exec $PID ip link set eth0 up
sudo ip netns exec $PID ip addr add $IP dev eth0
sudo ip netns exec $PID ip route add default via $GATEWAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment