Self-host OpenVPN in Docker on an LXC: scripts, AdGuard DNS and a monitor
OpenVPN is an older, very widely supported VPN. Almost every operating system and router has a client for it, it works over UDP or TCP, and it is easy to get through restrictive networks. WireGuard is simpler and faster, but OpenVPN is still a sensible choice when you need broad client support or a certificate-based setup you control.
This guide builds a complete OpenVPN server in Docker inside an LXC container, based on the widely used kylemanna/openvpn image and its helper commands. On top of the basic install it adds scripts to make the setup repeatable: an unattended first-run script, a script that creates clients with their own passphrase, a script that sends a finished client file to another machine, plus backup, restore and reset.
Names and addresses are placeholders. Replace ovpn.example.com and the 192.168.1.x addresses with your own.
What you will end up with
- An OpenVPN server in Docker, in its own LXC, reachable on UDP 1194 through a public hostname
- A home network reachable through the tunnel, with the clients' DNS served by your own AdGuard Home
- One
.ovpnfile per device, each protected by its own passphrase - A small web page showing who is connected, published privately behind your reverse proxy
- Scripts for setup, new clients, sending files, backup, restore and reset
Requirements
- A Proxmox host (or similar) where you can create an LXC container, with Docker installed in it
- A domain, and a way to keep the public IP current if it changes (see Keep a dynamic IP updated in Cloudflare DNS with Docker)
- Port forwarding of UDP 1194 on your router to the LXC
- An existing DNS server for the clients, such as the one from Run AdGuard Home in Docker
- A reverse proxy for the monitor page. See Run Caddy as a standalone reverse proxy in Docker.
1. Prepare the LXC
OpenVPN needs the tun device and permission to change the network configuration, and an LXC container does not have either by default. On the Proxmox host, edit the container's file in /etc/pve/lxc/ (the file is named after the container ID) and add:
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
Also enable nesting in the container's options so Docker can run. Restart the container, and check that /dev/net/tun exists inside it.
Give the container a fixed IP address on your LAN. In this guide it is 192.168.1.20.
2. Folder layout
sudo mkdir -p /opt/openvpn/openvpn/clients
cd /opt/openvpn
/opt/openvpn/
├── compose.yml
├── .env settings and secrets
├── init.sh one-time setup
├── newclient.sh create a client
├── sendclient.sh copy a client's files to another machine
├── backup.sh / restore.sh / reset.sh
└── openvpn/ all server data (PKI, config, clients)
The rule is simple: scripts and settings live in /opt/openvpn, and everything the server generates lives in /opt/openvpn/openvpn. Back up that one folder and you have the whole server.
3. Settings in an .env file
Create /opt/openvpn/.env. Choose strong, unique values:
VPN_HOST=ovpn.example.com
VPN_DNS=192.168.1.53
LAN_NETWORK=192.168.1.0
LAN_NETMASK=255.255.255.0
CA_PASSPHRASE=change_me_long_random
EASYRSA_REQ_CN=ovpn.example.com
chmod 600 /opt/openvpn/.env
VPN_HOSTis the public name clients connect to. Make it a DNS-only record, because Cloudflare's proxy does not carry OpenVPN.VPN_DNSis the AdGuard server's address.CA_PASSPHRASEprotects the certificate authority's private key. Whoever has this key can issue clients, so treat it like a root password.
Keep .env out of any git repository.
4. The Compose file
Create /opt/openvpn/compose.yml:
services:
openvpn:
image: kylemanna/openvpn:latest
container_name: openvpn
restart: unless-stopped
env_file: .env
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun:/dev/net/tun
ports:
- "1194:1194/udp"
volumes:
- ./openvpn:/etc/openvpn
openvpn-monitor:
image: ruimarinho/openvpn-monitor:latest
container_name: openvpn-monitor
restart: unless-stopped
environment:
- OPENVPNMONITOR_DEFAULT_SITE=Home VPN
- OPENVPNMONITOR_SITES_0_ALIAS=home
- OPENVPNMONITOR_SITES_0_HOST=openvpn
- OPENVPNMONITOR_SITES_0_NAME=Home VPN
- OPENVPNMONITOR_SITES_0_PORT=5555
ports:
- "8080:80"
depends_on:
- openvpn
The line that matters most for an LXC is the devices entry. Without it the container log shows mknod: /dev/net/tun: Operation not permitted, and the server never starts. Passing the device through from the container fixes it.
The monitor's variable names come from its documentation and can change between versions, so check its README if the page stays empty.
5. The first-run script
The image ships helper commands that generate the server config and the certificate authority. Run by hand they ask many questions, which is easy to get wrong and impossible to repeat. A script reading its answers from .env fixes that. Create /opt/openvpn/init.sh:
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
set -a; . ./.env; set +a
mkdir -p openvpn/clients
# 1. generate the server config
docker compose run --rm openvpn ovpn_genconfig \
-u "udp://${VPN_HOST}" \
-N \
-n "${VPN_DNS}" \
-p "route ${LAN_NETWORK} ${LAN_NETMASK}" \
-e "management 0.0.0.0 5555"
# 2. build the certificate authority and server certificate, unattended
docker compose run --rm \
-e EASYRSA_BATCH=1 \
-e EASYRSA_REQ_CN="${EASYRSA_REQ_CN}" \
-e EASYRSA_PASSIN="pass:${CA_PASSPHRASE}" \
-e EASYRSA_PASSOUT="pass:${CA_PASSPHRASE}" \
openvpn ovpn_initpki
docker compose up -d
What the options mean (confirm them with docker compose run --rm openvpn ovpn_genconfig in your image version):
-u: the public address written into every client file.-N: adds NAT, so VPN clients can reach the rest of your LAN even though your other devices do not know about the VPN network.-n: the DNS server pushed to clients.-p "route ...": pushes a route so clients send traffic for your home network through the tunnel. Add-dto turn off the default full-tunnel behaviour, which then gives you a split tunnel where only home traffic uses the VPN. Leave-dout if you want all traffic to go through the VPN.-e "management 0.0.0.0 5555": turns on OpenVPN's management interface, which the monitor reads. It listens only inside Docker's network, and the compose file does not publish it.
The unattended certificate step is the part to test before you trust it. Try the whole script on a throwaway container first, and read the output to be sure it did not stop at a prompt.
Run it once:
chmod +x init.sh
./init.sh
docker logs openvpn
You should see the server start and listen on UDP 1194.
6. Create clients with their own passphrase
Every device gets its own certificate, so one lost phone can be revoked without touching the others. Giving each client key a passphrase means a stolen .ovpn file is not enough to connect.
Create /opt/openvpn/newclient.sh:
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
set -a; . ./.env; set +a
NAME="${1:-}"
[[ -n "$NAME" ]] || read -rp "Client name: " NAME
[[ "$NAME" =~ ^[A-Za-z0-9._-]+$ ]] || { echo "Use letters, numbers, . _ - only"; exit 1; }
mkdir -p openvpn/clients
PASS="$(openssl rand -base64 24 | tr -d '/+=' | cut -c1-20)"
docker compose run --rm -T \
-e EASYRSA_BATCH=1 \
-e EASYRSA_PASSIN="pass:${CA_PASSPHRASE}" \
-e EASYRSA_PASSOUT="pass:${PASS}" \
openvpn easyrsa build-client-full "$NAME"
docker compose run --rm -T openvpn ovpn_getclient "$NAME" > "openvpn/clients/${NAME}.ovpn"
umask 077
printf '%s\n' "$PASS" > "openvpn/clients/${NAME}.pass"
echo "Created openvpn/clients/${NAME}.ovpn and ${NAME}.pass"
Use it like this:
./newclient.sh phone
Each client gets two files: the .ovpn configuration and a .pass file with that client's key passphrase. Import the .ovpn into an OpenVPN client app, and enter the passphrase when it asks.
To list clients or revoke one:
docker compose run --rm openvpn ovpn_listclients
docker compose run --rm openvpn ovpn_revokeclient phone
7. Send a client's files to another machine
Moving files around by hand is how they end up in chat apps. A small script copies a client's two files to a storage machine over SSH. Create /opt/openvpn/sendclient.sh:
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/openvpn/clients"
DEST="[email protected]:/path/to/ovpn"
echo "Available clients:"
ls -1 *.ovpn 2>/dev/null | sed 's/\.ovpn$//'
read -rp "Client name: " NAME
[[ -f "${NAME}.ovpn" ]] || { echo "No such client"; exit 1; }
rsync -avPh "${NAME}.ovpn" "${NAME}.pass" "$DEST/"
Use SSH keys for the transfer so no password is stored. Keep the destination folder private, since it holds both a client and its passphrase.
8. DNS through AdGuard
Clients get VPN_DNS pushed when they connect, so their queries go to your AdGuard through the tunnel and ad blocking and local names work away from home.
AdGuard must accept queries from the VPN. Under its access settings, make sure your LAN and the VPN's address range are allowed. Because the server uses NAT, AdGuard sees all VPN traffic coming from the OpenVPN container's own LAN address, not from individual clients, so the AdGuard statistics show one busy client instead of many. That is the trade-off for keeping the rest of your network unaware of the VPN. The monitor shows who is actually connected.
9. The monitor page
The monitor shows connected clients, how long they have been on, and traffic. Publish it privately, because it exposes usernames and addresses. If your reverse proxy runs in another LXC, point it at the OpenVPN container's IP address and the published port:
ovpn.int.example.com {
@internal remote_ip private_ranges
handle @internal {
reverse_proxy 192.168.1.20:8080
}
handle {
respond "Forbidden" 403
}
}
Give that hostname a local DNS record and no public one. Since the monitor port is published on the LXC, also limit who can reach it with the container's firewall.
Never publish port 5555. The management interface has no password in this setup and must stay inside Docker's network.
10. Backups, restore and reset
Everything important is in /opt/openvpn/openvpn plus .env. Back up both:
#!/usr/bin/env bash
set -euo pipefail
cd /opt/openvpn
tar czf "/root/openvpn-backup-$(date +%F).tar.gz" openvpn .env
Keep copies off the machine, and store them securely because they contain the CA private key. A restore script is the reverse: stop the stack, unpack the archive into /opt/openvpn, and start it again.
A reset script is useful while testing. It should stop the stack and delete openvpn/, and nothing else. Make sure it never deletes your scripts, or you cannot run them again.
I keep all of these inside one bootstrap script that writes every file, so a clean rebuild is a single command and nothing gets forgotten.
11. Connect a device
- Run
./newclient.sh phone. - Move
phone.ovpnto the phone. Use a secure channel. - Import it into the OpenVPN Connect app.
- Connect, and enter the passphrase from
phone.pass.
To test it, turn off Wi-Fi so the phone uses mobile data. Then open a service on your home network by its LAN address, and check the monitor page shows the client.
Security checklist
- Public port: UDP 1194 only. Do not publish 5555 or the monitor to the internet.
- Keep
.envand backups private. The CA passphrase and key are the keys to everything. - One client per device, each with a passphrase, and revoke lost devices at once.
- Keep the images updated, and keep your router's port forward to this one container.
- Choose a VPN address range that does not overlap with your home network or with networks you visit.
Troubleshooting
mknod: /dev/net/tun: Operation not permitted: the container has no TUN device. Check the LXC config lines and thedevicesentry in the compose file.- Clients cannot connect (TLS handshake failed): the UDP port is not reaching the server. Check the DNS record, the router forward and any firewall, and look at
docker logs openvpn. - Connected, but no access to your network: NAT or routing is missing. Confirm
-Nwas used and that IP forwarding is on inside the container. - No internet, or names do not resolve: the clients' DNS server is unreachable or AdGuard rejects the queries. Allow the right ranges in AdGuard's access settings.
- The monitor shows nothing: the management line was not added to the server config, or the monitor's variables are wrong. Check both.
- The init script stops at a prompt: one of the environment variables is not reaching the container. Read its output and fix the
.envvalues. - Clients import but never ask for a passphrase: the client key was created without one. Revoke it and create it again with
newclient.sh.
Summary
OpenVPN in Docker is straightforward once the TUN device is available in the LXC. Keep the server data in one folder, drive the setup from an .env file and a few small scripts, give every client its own certificate and passphrase, send DNS to AdGuard, and publish the monitor only on a private hostname. With backups of the one data folder, the whole server can be rebuilt in minutes.