树莓派上使用 Docker 运行 OpenWrt 搭建旁路由

下载Ubuntu镜像

使用官方的imager或者Etcher烧录系统到sd卡

Ubuntu系统的ssh会默认打开,因此不需要像其他系统在boot中新建ssh文件

直接插上卡启动

第一次登陆要求更改密码

更改镜像为清华大学源/etc/apt/sources.list

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse

根据Docker官网指引安装Docker:

https://docs.docker.com/engine/install/ubuntu/

OpenWrt镜像:

https://hub.docker.com/r/scenerycmopen/openwrt-bcm27xx-bcm2711-rpi-4-lean-openwrt/tags?page=1&ordering=last_updated

创建一块网卡:

该模式的官网介绍:https://docs.docker.com/network/macvlan/

简单来说,docker虚拟机连接到这个网卡,可以连接到和docker所在的主机连接到的网络,也就是相当于docker虚拟机和其宿主平级,一同连接到上层网络。

1
2
3
4
docker network create -d macvlan \
--subnet=192.168.2.0/24 \
--gateway=192.168.2.1 \
-o parent=eth0 mac_net

根据刚才拉取的image创建container:

1
2
3
4
5
6
7
8
9
docker create \
--restart unless-stopped \
--network mac_net \
--privileged \
--name openwrt \
-v /etc/resolv.conf:/etc/resolv.conf \
-v /mnt/sda1:/home/harddisk/ \
scenerycmopen/openwrt-bcm27xx-bcm2711-rpi-4-lean-openwrt \
/sbin/init
1
docker exec -it openwrt bash
1
vi /etc/config/network

主要配置lan,这里配置了dhcp,和docker宿主机一样的上网方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'
option ula_prefix 'fdcd:8f77:ae01::/48'

config interface 'lan'
option type 'bridge'
option ifname 'eth0'
option proto 'dhcp'
option gateway '192.168.31.1'
option hostname docker

config interface 'vpn0'
option ifname 'tun0'
option proto 'none'

重启网络:

1
/etc/init.d/network restart

此时,可以前往宿主机所在的网络后台查看Docker中的openwrt的IP,直接访问IP进入后台。默认密码password

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×