0x00

  1. 从rootfs创建系统镜像
docker import \
    http://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz \
    openvz/ubuntu:16.04
  1. 构建openwrt的docker镜像
  2. 安装openwrt docker镜像

0x00:
先决条件
1、确保宿主机 Linux Kernel 内核版本 ≥ 4.20 (此版本是 Docker Network 对 IPVLAN 模式稳定支持的最低限度版本)
2、一定的 Linux Shell 命令行及 IP Router Config 配置基础
3、宿主需要安装iproute2依赖库
4、能看得懂本文

0x01:
创建一个 IPVLAN/MACVLAM 类型的 Docker Network (根据需要任选其一即可)

# MACVLAN
docker network create \
    --driver=macvlan            `# 指定网络驱动类型为:macvlan` \
    --subnet=10.1.0.0/23        `# 接口所对应的物理网络网域(网段)` \
    --ip-range=10.1.0.0/24      `# 划分一段网段用于给容器分配 IP 地址使用(可选)` \
    --gateway=10.1.1.254        `# 物理网络的网关` \
    --opt parent=eth0           `# 绑定子接口到物理网卡(父接口)` \
    pub                         `# 给此 Docker Network 指派一个名称`


# IPVLAN
docker network create \
    --driver=ipvlan             `# 指定网络驱动类型为:ipvlan ` \
    --subnet=10.1.0.0/23        `# 接口所对应的物理网络网域(网段)` \
    --ip-range=10.1.0.0/24      `# 划分一段网段用于给容器分配 IP 地址使用(可选)` \
    --gateway=10.1.1.254        `# 物理网络的网关` \
    --opt parent=eth0           `# 绑定子接口到物理网卡(父接口)` \
    pub                         `# 给此 Docker Network 指派一个名称`
# 新建关于 Docker 容器的专用路由表
echo "100     docker" >> /etc/iproute2/rt_tables
ip route flush table docker

创建docker容器


version: '2'

services:
  openwrt:
    image:  raymondwong/openwrt_r9:21.2.1-arm64  # 这里是使用的 arm 架构镜像,如是 x86架构这里需要做一下更改
    container_name: openwrt_r9
    privileged: true
    restart: always
    networks:
      openwrt_macnet:
        ipv4_address: 192.168.8.111

networks:
  openwrt_macnet:
    driver: macvlan
    driver_opts:
      parent: eth0 # 对应桥接的网卡
    ipam:
      config:
        - subnet: 192.168.8.0/24
          ip_range: 192.168.8.220/25
          gateway: 192.168.8.1

0x02:
配置 IPVLAN/MACVLAN 静态路由表(根据需求选择对应的模式)
(步骤 0x02/0x03 二选一即可,使用 0x02 的方法则不需要再继续配置 0x03 的方法,反之亦然)

创建用于配置宿主机关于物理网络及 Docker 网络的静态路由表的脚本(用于开机自启时执行)
docker-network-boot-config.sh

# MACVLAN
ip link add mac0 link eth0 type macvlan mode bridge         # 根据需求创建一个新的 MACVLAN 子接口
ip addr add 10.1.0.1 dev mac0                               # 为子接口手动分配一个静态 IP 地址
ip link set mac0 up                                         # 打开子接口
ip link set eth0 promisc on                                 # 启用父接口网络混杂支持
ip link set mac0 promisc on                                 # 启用子接口网络混杂支持
ip route add default via 10.1.0.1 table docker              # 配置到容器的默认路由表
ip route add 10.1.0.0/23 dev mac0 src 10.1.0.1 metric 10    # 为子接口配置对应的静态路由表
ip rule add from all to 10.1.0.0/24 table docker            # 配置本地回环到容器的路由规则


# IPVLAN
ip link add ipvl link eth0 type ipvlan                      # 根据需求创建一个新的 IPVLAN 子接口
ip addr add 10.1.0.1 dev ipvl                               # 为子接口手动分配一个静态 IP 地址
ip link set ipvl up                                         # 打开子接口
ip link set eth0 promisc on                                 # 启用父接口网络混杂支持
ip link set ipvl promisc on                                 # 启用子接口网络混杂支持
ip route add default via 10.1.0.1 table docker              # 配置到容器的默认路由表
ip route add 10.1.0.0/23 dev ipvl src 10.1.0.1 metric 10    # 为子接口配置对应的静态路由表
ip rule add from all to 10.1.0.0/24 table docker            # 配置本地回环到容器的路由规则

将创建的 docker-network-boot-config.sh 脚本添加到 Linux 开机自启里(以便每次开机能自动配置)
然后重启 Linux 或执行下docker-network-boot-config.sh(记得 chmod +x)

0x03:
另一种更优雅的子接口配置方式,直接在网卡配置文件里设置(推荐)

nano /etc/network/interfaces


auto lo
iface lo inet loopback
iface lo inet6 loopback


auto eth0
iface eth0 inet static
        address 10.1.1.0
        gateway 10.1.1.254
        netmask 255.255.254.0
        dns-nameservers 10.1.0.53
        up ip link set $IFACE promisc on


auto mac0
iface mac0 inet static
        hwaddress ea:8c:f8:af:89:c2
        address 10.1.0.1
        gayway 10.1.1.254
        netmask 255.255.254.0
        dns-nameservers 10.1.0.53
        pre-up ip link add mac0 link eth0 type macvlan mode bridge
        up ip link set $IFACE promisc on
        up ip route add default via 10.1.0.1 table docker
        up ip rule add from all to 10.1.0.0/24 table docker
        post-down ip link del mac0 link eth0 type macvlan mode bridge


auto ipvl
iface ipvl inet static
        hwaddress ea:8c:f8:af:89:c2
        address 10.1.0.1
        gayway 10.1.1.254
        netmask 255.255.254.0
        dns-nameservers 10.1.0.53
        pre-up ip link add ipvl link eth0 type ipvl
        up ip link set $IFACE promisc on
        up ip route add default via 10.1.0.1 table docker
        up ip rule add from all to 10.1.0.0/24 table docker
        post-down ip link del ipvl link eth0 type ipvl