# Docker 安装 WireGuard

### docker-compose.yaml

```yaml
version: "3.8"

services:
  wireguard:
    image: linuxserver/wireguard:latest
    container_name: wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Shanghai
      - SERVERURL=wills-home.yunwei361.com  # 服务器的公网 IP 地址或域名
      - SERVERPORT=51820                    # WireGuard 使用的端口
      - PEERS=3                             # 生成的客户端数量
      - PEERDNS=auto                        # 使用自动的 DNS 配置
      - INTERNAL_SUBNET=192.168.2.0/24         # WireGuard 的内部子网
      - INTERNAL_SUBNET_V6=fd86:ea04:1115::/64 # WireGuard 的内部 IPv6 子网
    volumes:
      - /root/docker-wireguard/config:/config
      - /lib/modules:/lib/modules
    ports:
      - "51820:51820/udp"
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
      - net.ipv6.conf.all.forwarding=1
    restart: unless-stopped
```

### 启停命令

```bash
# 启停
docker compose up -d
docker compose down

# 查看客户端配置文件，可直接导出使用
docker exec -it wireguard cat /config/peer1/peer1.conf
docker exec -it wireguard cat /config/peer2/peer2.conf
docker exec -it wireguard cat /config/peer3/peer3.conf

# 查看状态
docker exec -it wireguard wg
```
