Dockernet enables you to build a flexible Docker network.
With Dockernet, you can add custom interfaces, configure static IP addresses, and adding static routes to a Linux container in a simple way.
Developer: Rayson Zhu vfreex@gmail.com
This project is inspired from pipework.
- Use the
--net=none
option when start a container to tell Docker not to configure its network automatically, leaving you free to build your custom network. For example, start a CentOS container without configuring the network:
$ sudo docker run -ti --rm --name=test_container --net=none centos:latest /bin/bash
- If you want to start a process after extra network interface is up in a container, you can add
dockernet
script to your Docker image and calldockernet --wait <interface>
, likedockernet --wait eth0
. It will wait until that interface is present and in UP operational state.
Use dockernet <container> addif <interface> [<bridge>=docker0]
to add an extra interface to a container.
Following command will add interface eth1
to container test_container
and bridge this interface with docker0
:
sudo dockernet test_container addif eth1
If you want to bridge your newly created interface to another interface br0
, just issue:
sudo dockernet test_container addif eth1 br0
Then, set operational state to UP:
sudo dockernet test_container up eth1
Usually, you may want to clear all IP addresses associated with that interface:
dockernet <container> clearip <interface>
Then, add an IP address. You should specify an IP address in CIDR notation. <default gateway>
is optional.
If not specified, no default route will be configured.
dockernet <container> addip <interface> <CIDR> [<default gateway>]
For instance, we add IPv4 address 192.0.2.100/24
and IPv6 address 2001:db8:1234:5678::100/64
with default gateways 192.0.2.1
and 2001:db8:1234:5678::1
to interface eth1
for container test_container
:
sudo dockernet test_container clearip eth1
sudo dockernet test_container addip eth1 192.0.2.100/24 192.0.2.1
sudo dockernet test_container addip eth1 2001:db8:1234:5678::100/64 2001:db8:1234:5678::1
You can use following notation to add or delete a route. The <ROUTE>
part given in these commands are directly passed to the ip route
command:
dockernet <CONTAINER> addroute <CONTAINER_IFNAME> <ROUTE>
dockernet <CONTAINER> delroute <CONTAINER_IFNAME> <ROUTE>
For instence, let's add 2 static routes to container test_container
:
sudo dockernet test_container addroute eth1 203.0.113.0/24 via 192.0.2.254 metric 1000
sudo dockernet test_container addroute eth1 2001:db8:abcd::/48 via 2001:db8:1234:5678::ffff metric 1000
These two rules will tell your container that route packets targeting network 203.0.113.0/24
to router 192.0.2.254
and 2001:db8:abcd::/48
to router 2001:db8:1234:5678::ffff
through interface eth1
.