找回密码
 加入我们
搜索
      
查看: 15358|回复: 41

[网络] 【已解决,附方法】求教大佬们一个走不同网关的问题。urnaid,不同网关

[复制链接]
发表于 2023-6-27 20:46 | 显示全部楼层
25楼应该是正解,根据他的方法,我用linuxserver/qbittorrent举例,我在unraid下用的是docker-compose.yml运行容器。

我的docker-compose.yml如下:
version: '3.9'
services:
  qbittorrent:
    container_name: qbittorrent
    image: linuxserver/qbittorrent:14.3.9v2
    restart: unless-stopped
    networks:
      br0:
        ipv4_address: 192.168.2.99
    ports:
      - 9999:9999
      - 55555:55555
      - 55555:55555/udp
    volumes:
      - /mnt/user/appdata/qbittorrent/config:/config
      - /mnt/ssd:/downloads
    entrypoint: /config/entrypoint.sh
    environment:
      - PUID=1000
      - PGID=100
      - TZ=Asia/Shanghai
      - WEBUI_PORT=9999
    cap_add:
      - NET_ADMIN # 允许修改网络设置
networks:
  br0: # 定义 macvlan 网络
    external: true # 使用外部网络

先写一个shell脚本,我的文件名是route.sh,内容如下:
#!/bin/bash
#如果容器里没有route命令,去掉下面这行的#先安装route命令(安装这个貌似要选择yes/no之类的,最好先拉取镜像安装好容器之后,再用命令docker commit -m="这里随便填" -a="这里随便填" 38fbe36addd5 linuxserver/qbittorrent:14.3.9v2创建一个有route工具的镜像,38fbe36addd5是原来容器的ID,linuxserver/qbittorrent:14.3.9v2我在原来容器的基础上加了个v2表示区别)
#apt-get update && apt install net-tools
# 删除原来的路由规则
route del -net 0.0.0.0/0 gw 192.168.2.3
# 添加新的路由规则
route add -net 0.0.0.0/0 gw 192.168.2.1

再生成一个entrypoint.sh,内容如下:
#!/bin/bash
# 执行你的 route.sh 脚本
bash /config/route.sh
# 调用 linuxserver/qbittorrent 的默认 entrypoint 脚本
exec /init

这两个文件一起放进config文件夹,先用#注释掉entrypoint: /config/entrypoint.sh这行,这样才能进入容器,不然会一直重启。在容器里打开控制台,分别运行chmod +x /config/route.sh和chmod +x /config/entrypoint.sh,使这两个文件拥有执行权限,不然进入容器会报错。权限修改好后,停止容器,去掉docker-compose.yml里entrypoint: /config/entrypoint.sh前的#号,保存运行容器即可。进入容器控制台,使用route -n命令可以看到路由表已更改为192.168.2.1,这样即便重启容器也会自动执行这个脚本,从主路由192.168.2.1出口。
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-6-2 22:14 , Processed in 0.008311 second(s), 6 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

快速回复 返回顶部 返回列表