Insights

Deploy WireGuard with Docker Compose on Linux

A LinuxServer.io Compose file runs WireGuard on Linux, saves one peer profile, and shows how to check the handshake and private service access.

Illustration of vented mini-server with one projecting conduit.
On this page

On a Linux host with WireGuard support, use the LinuxServer.io WireGuard image in Docker Compose. Set PEERS for server mode, mount a persistent host directory at /config, grant the NET_ADMIN network permission and publish port 51820 over User Datagram Protocol (UDP). Import the generated profile on a second device, then test from another network for a recent handshake, the exchange that establishes the tunnel, and a response from your private service.

In short:

  • Use server mode. Set PEERS=phone to generate one remote device profile.
  • Keep the configuration. Mount a persistent host directory at /config.
  • Test from outside. Check both the expected peer's handshake and the private service response.

A Compose file for one remote device

Docker Compose defines how Docker runs the container. WireGuard is the tunnel technology, while the LinuxServer.io image generates the server settings and peer profiles. In this example, phone names the remote device that will connect.

1. Save this as compose.yaml on your Linux host.

This example assumes Docker Compose is installed and WireGuard is already available to the Linux kernel. Replace wireguard.domain.com with the hostname or address your remote device can reach. Replace /path/to/wireguard/config with the host directory you want to keep, and check PUID and PGID against your intended user and group IDs.

services:
  wireguard:
    image: lscr.io/linuxserver/wireguard:latest
    container_name: wireguard
    cap_add:
      - NET_ADMIN
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Etc/UTC
      - SERVERURL=wireguard.domain.com
      - SERVERPORT=51820
      - PEERS=phone
      - ALLOWEDIPS=0.0.0.0/0
      - LOG_CONFS=false
    volumes:
      - /path/to/wireguard/config:/config
    ports:
      - '51820:51820/udp'
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
    restart: unless-stopped

What access the container needs

NET_ADMIN lets the container create the WireGuard network interface. The file also keeps net.ipv4.conf.all.src_valid_mark=1, the kernel setting in the maintainer's Compose example.

Leave out SYS_MODULE and the /lib/modules mount when WireGuard is already built in or loaded. Those options are for a container that needs to load an available host module. They don't add WireGuard support to a host that lacks it.

The /config mount puts the settings and generated profiles in your host directory. If you replace the container and keep the same mount, the files stay in place.

Start the service and import the peer profile

2. Start WireGuard from the Compose project directory on your Linux host.

docker compose up -d wireguard

3. Import the profile on the remote device.

Look for peer_phone inside the host directory you mounted at /config. That folder should contain the generated profile and its scannable QR code. Import the profile file in your WireGuard client or scan the code privately.

LOG_CONFS=false keeps the generated QR code out of Docker's startup logs. Both forms contain connection credentials, so keep them out of screenshots, support posts and shared logs.

Check the connection from outside your network

4. Check that the endpoint can reach the published port.

Docker maps UDP port 51820 on the Linux host to the same port in the container. That mapping doesn't get packets through a router in front of the host. Before changing a shared router or firewall, make sure you're authorized and know how to undo the change.

If the host sits behind the router that receives the incoming traffic, forward UDP port 51820 on that router to UDP port 51820 on the Linux host. If the router itself isn't reachable from outside, that forward won't fix the upstream path.

5. Try a known private service from another network.

Pick a private service in the peer's allowed routes and note the response you expect. On your phone, turn off Wi-Fi, switch to cellular data and activate the WireGuard profile. Request the service at its private address.

6. Check the expected peer's handshake on the Linux host with wg show.

docker exec -it wireguard wg show

Look for a latest handshake from your connection attempt under the phone's peer entry. In this setup, the phone should be the only peer listed. Use the handshake and the service response to decide what to check next:

  • No recent handshake: Check the endpoint address and port, the inbound UDP path and whether you imported the right profile.
  • Recent handshake, failed request: Check the destination address, route and firewall, then check whether the service is listening.
  • Recent handshake, expected response: The test confirms that this peer can reach that private destination.

What traffic goes through the tunnel

ALLOWEDIPS=0.0.0.0/0 sends all of the peer's Internet Protocol version 4 (IPv4) traffic into the tunnel, but it doesn't cover IPv6. For split tunneling, which sends only selected traffic through WireGuard, set ALLOWEDIPS to the private addresses or ranges you need plus the server's WireGuard address.

This file runs WireGuard as a server for incoming peers. It doesn't route other containers' outbound traffic through a WireGuard client. That needs a separate client-mode setup and routing for those containers.

Where doxx.net fits. If you want a private network for people and agents instead of maintaining your own endpoint, doxx.net runs one on its own hardware. A WireGuard tunnel alone doesn't include chat or calling, so if you want those features, you can download the doxx.net app.

Ask an AI assistant

Share this article

Private Everywhere

Stop giving the internet everything

Keep your browsing, messages, files, and agents private.