在接下来的文章中,我们将在
Docker
容器中运行
OpenWrt
,并通过设置,让
Docker
容器中的
OpenWrt
网关接管路由器自身的网关,减轻路由器的负担,同时,由于网关被
OpenWrt
接管,所以
OpenWrt
中的大部分应用都是可用的,比如酸酸乳,V2ray,去广告等。做个不恰当的比喻,
OpenWrt
网关接管路由器自身的网关后,你可以理解为路由器是
OpenWrt
的一块外置网卡。
一、概念介绍
1. OpenWrt
2. 旁路网关
所谓的旁路网关 :就是在路由器之外的其他硬件设备上搭建一个网关环境,把路由器肩负的网关重任交给其他更适合的设备来做,同时,旁路网关处理完的结果会返回给主路由器,由主路由器继续进行转发处理。
这样,每个角色各司其职,路由器肩上的任务轻了,即使是油管 4K 也能轻松跑满网速了,而
旁路网关
也能提供更多的功能。由此资源的充分利用,一举两得。
3. Docker
二、Docker 安装
1. 获取自动安装脚本
# 安装命令
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun
# Executing docker install script, commit: 1b02882d63b9cfc484ad6b0180171c679cfe0f3a
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/debian/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sh -c echo "deb [arch=arm64] https://download.docker.com/linux/debian stretch stable" > /etc/apt/sources.list.d/docker.list
+ sh -c apt-get update -qq >/dev/null
+ [ -n ]
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sh -c docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:27:00 2020
OS/Arch: linux/arm64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:25:31 2020
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
2. 开启网卡的混杂模式
ip link set eth0 promisc on
3. 下载 OpenWrt 镜像
# 镜像下载命令
docker pull buddyfly/openwrt-aarch64
Using default tag: latest
latest: Pulling from buddyfly/openwrt-aarch64
aef47d0acf8a: Pull complete
Digest: sha256:d3a3f4f9aaac4169d3b16c1f796b5d379c6234b997cb440daee80ef3ad1aba05
Status: Downloaded newer image for buddyfly/openwrt-aarch64:latest
docker.io/buddyfly/openwrt-aarch64:latest
4. 创建 Docker 虚拟网络
虚拟网络名称为
macnet
,驱动为macvlan
模式,将subnet 10.10.10.0
修改为你自己主路由的网段,geteway 10.10.10.1
修改为你自己的主路由网关】
# 创建虚拟网络
docker network create -d macvlan --subnet=10.10.10.0/24 --gateway=10.10.10.1 -o parent=eth0 macnet
5. 启动 OpenWrt 容器
# 开启容器
docker run --restart always -d --network macnet --privileged buddyfly/openwrt-aarch64:latest
6. 查看运行中的 OpenWrt 容器
# 查看运行中的容器
docker ps -a
root@aml4:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f76303c1b9f0 buddyfly/openwrt-aarch64:latest "/sbin/init" 43 seconds ago Up 40 seconds affectionate_liskov
root@aml4:~#
7. 修改OpenWrt容器的IP地址
# 进入容器命令行
docker exec -it f76303c1b9f0 /bin/bash
root@aml4:~# docker exec -it f763 /bin/bash
bash-4.4#
# 修改网络配置
vi /etc/config/network
修改文件如下 将
option ipaddr 192.168.1.1
改为你想作为旁路网关的空闲地址。我的网段为
10.10.10.X
,共享地址为
10.10.10.254
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 'fd00:6e95:e381::/48'
config interface 'lan'