使用vpn时,docker容器网络访问

时间:2018-05-26 16:26:09

标签: docker docker-compose docker-network

预期结果:

Container可以通过hostname或hostcomputer ip互相访问。

实际结果:

使用 network_mode 时,我无法将静态IP或链接更改为其他容器。

说明

我有几个容器都使用--net(network_mode)到openvpn。作为单个实例,他们可以使用nginx代理,我可以从任何计算机访问每个实例。

然而,除了使用本地IP(172.19.0.x)之外,容器无法找到彼此。我可以使用它,但主机重启会发生什么。 IP地址会改变吗?

搬运工-compose.yml

version: '3.4'
services:

    vpn:
        image: dperson/openvpn-client
        container_name: vpn
        cap_add:
            - net_admin
        networks:
            - default
        tmpfs:
            - /tmp
        restart: unless-stopped
        security_opt:
            - label:disable
        stdin_open: true
        tty: true
        volumes:
            - ../openvpn:/vpn
            - /dev/net:/dev/net:z
        environment:
            - DNS='8.8.4.4 8.8.8.8'
            - FIREWALL="1"
            - TZ='Europe/Stockholm'
        command: -f ""
        networks:
            - default

    proxy:
        image: nginx
        container_name: proxy
        environment:
            TZ: 'Europe/Stockholm'
        ports:
            - "6003:8989" # sonarr
            - "6004:7878" # radarr
            - "6001:8112" # deluge
            - "6002:9117" # jackett
        depends_on:
            - sonarr
            - radarr
            - deluge
            - jackett
        links:
            - vpn:sonarr
            - vpn:radarr
            - vpn:deluge
            - vpn:jackett
        networks:
            - default
        volumes:
            - ../nginx/default.conf:/etc/nginx/conf.d/default.conf
        restart: always
        command: "nginx -g 'daemon off;'"

    sonarr:
        image: linuxserver/sonarr
        container_name: sonarr
        volumes:
            - ../sonarr:/config
            - /etc/localtime:/etc/localtime:ro
            - /media/megadrive/Media/Series:/tv
            - /media/megadrive/Media/tmp/completed:/downloads
        env_file: ../uidgid.env
        network_mode: "service:vpn"
        environment:
            - TZ='Europe/Stockholm'
        cap_add:
            - net_admin
        depends_on:
            - vpn
        restart: always


    radarr:
        image: linuxserver/radarr
        container_name: radarr
        volumes:
            - ../radarr:/config
            - /media/megadrive/Media/Movies:/movies
            - /media/megadrive/Media/tmp/completed:/downloads
            - /etc/localtime:/etc/localtime:ro
        env_file: ../uidgid.env
        network_mode: "service:vpn"
        environment:
            - TZ='Europe/Stockholm'
        cap_add:
            - net_admin
        depends_on:
            - vpn
        restart: always

    deluge:
        image: linuxserver/deluge
        container_name: deluge
        depends_on:
            - vpn
        network_mode: "service:vpn"
        volumes:
            - ../deluge:/config
            - /media/megadrive/Media/tmp/:/downloads
            - /etc/localtime:/etc/localtime:ro
        restart: always
        env_file: ../uidgid.env
        environment:
            - TZ='Europe/Stockholm'

    jackett:
        container_name: jackett
        image: linuxserver/jackett
        restart: unless-stopped
        network_mode: "service:vpn"
        env_file: ../uidgid.env
        environment:
            - TZ='Europe/Stockholm'
        volumes:
            - ../jackett:/config
            - /media/megadrive/Media/tmp/blackhole:/downloads

networks:
    default:

2 个答案:

答案 0 :(得分:0)

似乎让vpn服务使用host而不是bridge(默认)。将解决一些问题。

  • 允许所有内容在主机ip上运行。只要每个服务都在自己的端口上,这是可以的。
  • 服务似乎仍遵循openvpn规则
  • 不再需要nginx代理webgui

    vpn:
        image: dperson/openvpn-client
        container_name: vpn
        cap_add:
            - net_admin
        tmpfs:
            - /tmp
        restart: unless-stopped
        security_opt:
            - label:disable
        stdin_open: true
        tty: true
        volumes:
            - ../openvpn:/vpn
            - /dev/net:/dev/net:z
        environment:
            - DNS='8.8.4.4 8.8.8.8'
            - FIREWALL="1"
            - TZ='Europe/Stockholm'
        command: -f ""
        network_mode: "host"
    

答案 1 :(得分:0)

另一种选择是vpn中的服务使用localhost相互访问。由于它们共享 vpn 容器的网络堆栈,因此可以像访问同一台主机一样访问它们。这周让我难住了一段时间。

一条评论,你的所有容器都有 net_admin,你只需要在 vpn 上使用