简体   繁体   English

使用 Docker Compose 在 WireGuard 专用网络中阻止 Internet 访问

[英]Block Internet Access in WireGuard Private Network Using Docker Compose

I'd like to set up a private cloud.network using docker-compose that is only accessible via WireGuard.我想使用docker-compose设置一个只能通过 WireGuard 访问的私有云网络。 The private.network contains multiple services but no service should be accessible from the inte.net and containers internal to the.network should not have inte.net access. private.network 包含多个服务,但不应从 inte.net 访问任何服务,并且 .network 内部的容器不应具有 inte.net 访问权限。 The only port exposed for this.network is 51820 for WireGuard.为 this.network 公开的唯一端口是 WireGuard 的 51820。

I have everything working exactly how I want except for one problem: the containers in the private.network have inte.net access.除了一个问题外,我的一切都按照我想要的方式工作:private.network 中的容器具有 inte.net 访问权限。 If I block container inte.net access by specifying internal: true on the custom private .network, it breaks access to the wireguard.network for all peers/clients.如果我通过在自定义private .network 上指定internal: true来阻止容器 inte.net 访问,它会中断所有对等方/客户端对 wireguard.network 的访问。

Here is my docker-compose file:这是我的 docker-compose 文件:

version: "2.1"
services:
  wireguard:
    image: lscr.io/linuxserver/wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    environment:
      - PUID=1000
      - PGID=1000
      - SERVERPORT=51820
      - PEERS=1
      - PEERDNS=192.168.100.254
      - INTERNAL_SUBNET=10.10.0.0
      - ALLOWEDIPS=10.10.0.0/16,192.168.100.0/24
    volumes:
      - /root/wireguard/config:/config
      - /lib/modules:/lib/modules
    ports:
      - 51820:51820/udp
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
    networks:
      private:
        ipv4_address: 192.168.100.10
    restart: unless-stopped
  unbound:
    image: "mvance/unbound:latest"
    volumes:
      - ./unbound:/opt/unbound/etc/unbound/
    networks:
      private:
        ipv4_address: 192.168.100.254
  nginx:
    image: nginx
    networks:
      private:
        ipv4_address: 192.168.100.20


networks:
  private:
#    internal: true # Breaks WireGuard for all clients if uncommented
    ipam:
      driver: default
      config:
        - subnet: 192.168.100.0/24

I'd like to avoid custom iptables rules but I'm open to any suggestions.我想避免自定义 iptables 规则,但我愿意接受任何建议。

I think you want two .networks: an internal one to isolate the service containers, and an external one to provide the wireguard container with external access.我认为您需要两个.networks:一个用于隔离服务容器的内部网络,以及一个用于为 wireguard 容器提供外部访问的外部网络。 Somethinglike:就像是:

version: "2.1"
services:
  wireguard:
    image: lscr.io/linuxserver/wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    environment:
      - PUID=1000
      - PGID=1000
      - SERVERPORT=51820
      - PEERS=1
      - PEERDNS=192.168.100.254
      - INTERNAL_SUBNET=10.10.0.0
      - ALLOWEDIPS=10.10.0.0/16,192.168.100.0/24
    volumes:
      - /root/wireguard/config:/config
      - /lib/modules:/lib/modules
    ports:
      - 51820:51820/udp
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
    networks:
      outside:
      private:
        ipv4_address: 192.168.100.10
    restart: unless-stopped
  unbound:
    image: "mvance/unbound:latest"
    volumes:
      - ./unbound:/opt/unbound/etc/unbound/
    networks:
      private:
        ipv4_address: 192.168.100.254
  nginx:
    image: nginx
    networks:
      private:
        ipv4_address: 192.168.100.10


networks:
  outside:
  private:
    internal: true
    ipam:
      driver: default
      config:
        - subnet: 192.168.100.0/24

This is only a partial solution;这只是部分解决方案; you'll need to arrange for your service containers to route through the wireguard container;您需要安排您的服务容器通过 wireguard 容器; as currently configured, your containers have a default route of 192.168.100.1, which probably isn't useful.按照当前配置,您的容器有一个默认路由 192.168.100.1,这可能没有用。

Also note that you've assigned the same ip address to your wireguard container and to the nginx container (192.168.100.10), which is probably a typo.另请注意,您已将相同的 ip 地址分配给wireguard容器和nginx容器 (192.168.100.10),这可能是一个拼写错误。

Because I think I'm doing something similar to you (for now, I heard of other softwares I could use), I am going to presume I am either close to your intent, or in the same camp.因为我认为我正在做与您类似的事情(目前,我听说过我可以使用的其他软件),所以我假设我要么接近您的意图,要么在同一个阵营中。 You want to use your Wireguard VPN service to provide a DHCP.network across your VPN users.您希望使用 Wireguard VPN 服务为您的 VPN 用户提供 DHCP.network。 This is, in many ways, similar to having one computer out on the inte.net try to join your docker swarm.在很多方面,这类似于在 inte.net 上让一台计算机尝试加入您的 docker 群。 You want to deny access to them, you'd at least want them within your local.net.你想拒绝访问它们,你至少希望它们在你的 local.net 中。

In your case, now that your first correct answer tells you that you need TWO docker.networks to make an internal docker.network, the SECOND correct answer needs to tell you that your existing internal.network configurations are incorrect.在你的情况下,既然你的第一个正确答案告诉你你需要两个 docker.networks 来制作一个内部 docker.network,第二个正确答案需要告诉你你现有的 internal.network 配置不正确。 Since this answer's almost a year outdated, there's no telling if an upgraded version value would change your YML to my answer.由于这个答案已经过时了将近一年,所以无法确定升级后的version值是否会将您的 YML 更改为我的答案。 So here goes.所以就这样吧。

For clients attached via your VPN, you do have to route them internally using a docker overlay .network.对于通过 VPN 连接的客户端,您必须使用 docker overlay .network 在内部路由它们。 You need gateway for the.network these ipam IPs are on to be set to the Wireguard host.您需要将这些 ipam IP 所在的gateway设置为 Wireguard 主机。 In other words, your config would look like this, including @larsks answer: (As a note, I reduced your allowed hosts on the su.net VPN but not the docker.network. The Docker Network is still large spanning.10, .20, .254, and.100 therefore weighing a constraint on the overlay, see the docker.network documentation later on overlay.network sizes. )换句话说,您的配置看起来像这样,包括@larsks 回答:(请注意,我减少了 su.net VPN 上允许的主机,但没有减少 docker.network。Docker 网络仍然很大 spanning.10,。 20、.254 和 .100 因此权衡了对覆盖的约束,请参阅稍后有关覆盖网络大小的 docker.network 文档。)

version: "2.1"
services:
  wireguard:
    image: lscr.io/linuxserver/wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    environment:
      - PUID=1000
      - PGID=1000
      - SERVERPORT=51820
      - PEERS=1
      - PEERDNS=192.168.100.254
      - INTERNAL_SUBNET=10.10.0.0
      - ALLOWEDIPS=10.10.0.0/25,192.168.100.0/24
    volumes:
      - /root/wireguard/config:/config
      - /lib/modules:/lib/modules
    ports:
      - 51820:51820/udp
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
    networks:
      outside:
      private:
        ipv4_address: 192.168.100.10
    restart: unless-stopped
  unbound:
    image: "mvance/unbound:latest"
    volumes:
      - ./unbound:/opt/unbound/etc/unbound/
    networks:
      private:
        ipv4_address: 192.168.100.254
  nginx:
    image: nginx
    networks:
      private:
        ipv4_address: 192.168.100.20


networks:
  outside:
  private:
    internal: true
    ipam:
      driver: overlay
      config:
        - subnet: 192.168.100.0/24
        - subnet: 10.10.0.0/25
        - allocate-ip: 192.168.100.0/24
        - gateway: 192.168.100.10
        - gateway: 10.10.0.1

I intend that the above configuration makes...我打算让上面的配置......

  1. WireGuard start up on 10.10.0.1 as the VPN Su.net gateway, with the Docker.network granting WireGuard the access to both the wild inte.net as a default bridge, and access to the local docker intr.net/overlay.network. WireGuard 在 10.10.0.1 上作为 VPN Su.net 网关启动,Docker.network 授予 WireGuard 作为默认网桥访问 wild inte.net 和访问本地 docker intr.net/overlay.network 的权限。
  2. It makes this docker container an overlay.network with a gateway for the local docker.network and a gateway for the VPN.network, assigning IPs to the local docker.network它使这个 docker 容器成为一个 overlay.network,带有本地 docker.network 的网关和 VPN.network 的网关,将 IP 分配给本地 docker.network
  3. And sets the Docker.network reservation for its 192.160.100.10 IP to be the VPN container on the 192.168.100.0/24.network, equal to the VPN's gateway on this su.net并将其 192.160.100.10 IP 的 Docker.network 保留设置为 192.168.100.0/24.network 上的 VPN 容器,等于 VPN 在这个 su.net 上的网关

This is my first question on Stack Overflow under my new account, and I've yet to test this in my own learning.这是我在新帐户下关于 Stack Overflow 的第一个问题,我还没有在自己的学习中对此进行测试。 Take a look at the Docker Networking Tutorials for both the Commandline Create: https://docs.docker.com/engine/reference/commandline.network_create/ And joining a docker swarm with an Overlay docker.network: https://docs.docker.com.network.network-tutorial-overlay/ and good luck to anybody else looking to answer this question.查看命令行创建的 Docker 网络教程: https://docs.docker.com/engine/reference/commandline.network_create/并使用 Overlay docker.network 加入 docker 群: 881056383://docs. docker.com.network.network-tutorial-overlay/祝其他想回答这个问题的人好运。

Also, I didn't move driver: overlay out above the ipam block.另外,我没有移动driver: overlayipam块上方。 These VML versions can be a bit confusing, see here: What is the purpose of the "ipam" key in a Docker Compose config?这些 VML 版本可能有点令人困惑,请参阅此处: Docker Compose 配置中“ipam”键的用途是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM