Skip to content
프로젝트

DigitallyRefined/docker-wireguard-tunnel

GitHub에서 보기

소개

Connect two or more Docker servers together sharing container ports between them via a WireGuard tunnel

시스템 및 인프라

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

Docker WireGuard Tunnel

Build and push workflow Check for updates workflow Container registry Container registry

Connect two or more Docker servers together sharing container ports between them via a WireGuard tunnel.

For example a Docker server without a public IP address behind a NAT can expose container ports to another Docker server that has a public IP address to allow incoming connections.

Example Topology

Usage Example

This assumes that you have already setup a subdomain DNS entry for your domain, for example:
wireguard-tunnel.example.com

Server

Will accept connections on behalf of a peer and tunnel them to the designated peer.

docker-compose.yml

services:
  wireguard-tunnel-server:
    image: ghcr.io/digitallyrefined/docker-wireguard-tunnel:v4
    container_name: wireguard-tunnel-server
    environment:
      # Update to your domain
      - DOMAIN=wireguard-tunnel.example.com
      # Number of peers to auto generate config for
      - PEERS=1
      # Services to expose format (comma-separated)
      # SERVICES=peer-id:peer-container-name:peer-container-port:expose-port-as
      - SERVICES=peer1:nginx:80:8080,peer1:nginx-demo:80:8081
    cap_add:
      - NET_ADMIN
    volumes:
      - ./config:/etc/wireguard
    restart: unless-stopped
    ports:
      - '51820:51820/udp'
      - 8080:8080
      - 8081:8081
docker compose up -d
docker compose logs -f

Once started, a peer1.conf file will be automatically generated in the config directory.

Peer

Will connect to the server via WireGuard and setup a tunnel to expose the listed ports.

Move the config/peer1.conf file from the server that was automatically generated and rename it to config/wg0.conf on the peer.

docker-compose.yml

services:
  wireguard-tunnel-peer:
    image: ghcr.io/digitallyrefined/docker-wireguard-tunnel:v4
    container_name: wireguard-tunnel-peer
    environment:
      # Note that DOMAIN & PEERS are not required for the peer
      # Services to expose format (comma-separated)
      # SERVICES=peer-id:peer-container-name:peer-container-port:expose-port-as
      - SERVICES=peer1:nginx:80:8080,peer1:nginx-demo:80:8081
    cap_add:
      - NET_ADMIN
    volumes:
      - ./config:/etc/wireguard
    restart: unless-stopped
    init: true
    healthcheck:
      test: ping 10.0.0.254 -c 1 || bash -c 'kill -s 15 -1 && (sleep 10; kill -s 9 -1)'
      interval: 60s
      timeout: 30s
      retries: 3
      start_period: 20s
    links:
      - nginx:nginx
      - nginx-demo:nginx-demo

  nginx:
    image: nginx
    restart: unless-stopped

  nginx-demo:
    image: nginxdemos/hello
    restart: unless-stopped
docker compose up -d
docker compose logs -f

Note: if you have a firewall in front of your server you will need to allow connections on port 51820/udp for the WireGuard server, and TCP connections on ports 8080 and 8081 for the 2 demo nginx servers.

Once started you should be able to access both nginx servers via their exposed ports on the WireGuard server, for example:
wireguard-tunnel.example.com:8080 and wireguard-tunnel.example.com:8081

You may want to combine the WireGuard tunnel server with Traefik or Nginx Proxy Manager or use a 3rd party service such as Fly.io.

The generated peer config uses PersistentKeepalive = 10. Keep it at 10 seconds or lower: the server learns the peer's (possibly NATed) endpoint from incoming packets, so this is what allows the tunnel to come back promptly after either side restarts.

How forwarding works

Port forwarding is handled by HAProxy running in TCP mode, configured from the SERVICES environment variable. A few consequences worth knowing:

  • TCP only. HAProxy does not proxy UDP, so SERVICES entries only forward TCP. Earlier versions of this image forwarded UDP as well; that is no longer supported.
  • Health checks. Each backend is checked with a TCP connection every 10s. The server may need 20-40 seconds to re-establish the WireGuard handshake after it restarts, because it has no remembered endpoint for a NATed peer until that peer's keepalive arrives. The backend is not marked down during that window, so requests keep being forwarded and start succeeding as soon as the handshake completes — no restarts or manual action required.
  • Timeouts. Idle client/server connections are closed after 1h and established tunnels after 24h, so a stalled connection cannot wedge the proxy.
  • Logs. docker compose logs -f shows per-connection logs plus backend state changes (is UP / is DOWN).

Resource limits

Forwarding is handled by a single HAProxy process, which is event-driven and cheap even with many long-lived connections — typically a fraction of a percent of one core. On small instances it is still worth bounding the container so a runaway process or a traffic spike cannot starve the host:

    cpus: 0.5

There is no universal value: size it against the host. 0.25 is reasonable on a 1 vCPU VPS, while a host running several tunnels can afford more. Watch nr_throttled in the container's cgroup cpu.stat after setting it — if that counter climbs steadily, the limit is too low. The same applies to the peer container, which runs the same proxy.

Troubleshooting

  • is DOWN in the logs / 503 responses — check the tunnel from both ends with wg show inside each container. On the server a peer shows an endpoint only after the peer has contacted it; if the endpoint is missing, the peer is not sending keepalives (check PersistentKeepalive in config/wg0.conf on the peer).
  • Container exits immediately with Invalid HAProxy configuration — a SERVICES entry is malformed. Each entry must have four colon-separated fields: peer-id:peer-container-name:peer-container-port:expose-port-as.
  • Container crash-loops with /usr/bin/wg-quick: line 11: /usr/bin/readlink: Permission denied — rootless Docker on a host with Ubuntu's kernel.apparmor_restrict_unprivileged_userns=1. This image works around it by invoking wg-quick through bash rather than its shebang; on an older image either upgrade, or set that sysctl to 0 on the host.
  • A service is unreachable but the tunnel is up — forwarding is TCP only; UDP services are not proxied.

Examples using Docker WireGuard Tunnel with:

  • Traefik to automatically provision TLS/HTTPS certificates
  • Fly.io to provision a server, subdomain and TLS/HTTPS certificates

관련 프로젝트 살펴보기

불러오는 중…