44
55## Overview
66
7- This repository provides a containerized Wireguard VPN solution, including health checks .
7+ This repository provides a lightweight, containerized Wireguard VPN solution built on Alpine Linux. It includes comprehensive health monitoring, automatic key management, and follows Docker best practices for security and reliability .
88
99---
1010
11+ ## Features
12+
13+ - β
** Lightweight Alpine-based container** (~ 50MB)
14+ - β
** Automated health checks** via HTTP endpoint (port 8080)
15+ - β
** Graceful shutdown** with proper signal handling
16+ - β
** Easy key generation** using Wireguard tools
17+ - β
** Secure by default** with minimal attack surface
18+ - β
** Production-ready** with proper logging and monitoring
19+
1120## Quick Start
1221
1322### 1. Generate Public and Private Keys
@@ -27,16 +36,51 @@ docker run -i --rm eftechcombr/wireguard wg genkey | tee ./etc/privatekey | dock
2736docker build -t eftechcombr/wireguard .
2837docker run --rm -it \
2938 --cap-add=NET_ADMIN \
39+ --cap-add=SYS_MODULE \
40+ --sysctl=" net.ipv4.conf.all.src_valid_mark=1" \
3041 -v $( pwd) /etc:/etc/wireguard \
42+ -v /lib/modules:/lib/modules \
43+ -p 51820:51820/udp \
44+ -p 8080:8080/tcp \
45+ --restart unless-stopped \
3146 eftechcombr/wireguard
3247```
3348
3449### 4. Health Check
3550
36- A health check script is provided :
51+ The container includes an automated health check server :
3752
3853``` sh
39- python wireguard_healthcheck.py
54+ # Check health status
55+ curl http://localhost:8080
56+
57+ # Expected response: HTTP 200 if VPN is active, 503 if down
58+ ```
59+
60+ ### 5. Docker Compose (Recommended)
61+
62+ ``` yaml
63+ version : ' 3.8'
64+ services :
65+ wireguard :
66+ build : .
67+ container_name : wireguard
68+ cap_add :
69+ - NET_ADMIN
70+ - SYS_MODULE
71+ sysctls :
72+ - net.ipv4.conf.all.src_valid_mark=1
73+ volumes :
74+ - ./etc:/etc/wireguard
75+ - /lib/modules:/lib/modules
76+ ports :
77+ - " 51820:51820/udp"
78+ - " 8080:8080/tcp"
79+ restart : unless-stopped
80+ environment :
81+ - PUID=1000
82+ - PGID=1000
83+ - TZ=UTC
4084` ` `
4185
4286---
@@ -50,39 +94,154 @@ python wireguard_healthcheck.py
5094| WG_PUBLIC_KEY | Path to public key file | /etc/wireguard/publickey |
5195| WG_INTERFACE | Wireguard interface name | wg0 |
5296
97+ ### Advanced Configuration (Optional)
98+
99+ For production deployments, consider these additional environment variables:
100+
101+ | Variable | Description | Default |
102+ |------------------|-----------------------------------|-----------------|
103+ | PUID | User ID for container processes | 1000 |
104+ | PGID | Group ID for container processes | 1000 |
105+ | TZ | Container timezone | UTC |
106+ | SERVERURL | External server URL/IP | auto |
107+ | SERVERPORT | External server port | 51820 |
108+ | INTERNAL_SUBNET | VPN internal subnet | 10.13.13.0/24 |
109+ | PEERDNS | DNS server for clients | auto |
110+
53111---
54112
55113## File Structure
56114
57115` ` `
58- etc/
59- privatekey
60- publickey
61- wg0.conf
62- wireguard_healthcheck.py
63- Dockerfile
64- entrypoint.sh
116+ βββ etc/
117+ β βββ privatekey # Wireguard private key (generated)
118+ β βββ publickey # Wireguard public key (generated)
119+ β βββ wg0.conf # Main Wireguard configuration
120+ βββ wireguard_healthcheck.py # HTTP health check server
121+ βββ Dockerfile # Container build definition
122+ βββ entrypoint.sh # Container startup script
123+ βββ docker-compose.yml # Orchestration configuration
124+ βββ README.md # This documentation
125+ ```
126+
127+ ## Architecture
128+
129+ ```
130+ βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
131+ β Docker Host β β Wireguard β β Health Check β
132+ β β β Container β β Server (8080) β
133+ β βββββββββββββββ β β β β β
134+ β β 51820/UDP βββΌβββββ€ wg0 interface β β HTTP/GET,HEAD β
135+ β β VPN Traffic β β β β β 200/503 status β
136+ β βββββββββββββββ β β βββββββββββββββ β β β
137+ β βββββββββββββββ β β β wg-quick β β β βββββββββββββββ β
138+ β β 8080/TCP βββΌβββββ€ β daemon β β β β Python β β
139+ β β Health Checkβ β β βββββββββββββββ β β β HTTP Serverβ β
140+ β βββββββββββββββ β β β β βββββββββββββββ β
141+ βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
65142```
66143
67144---
68145
69146## Useful Commands
70147
71- - Generate keys: see above
72- - Run health check: ` python wireguard_healthcheck.py `
73- - Build Docker image: ` docker build -t eftechcombr/wireguard . `
74- - Run container: see above
148+ ### Key Management
149+ ``` sh
150+ # Generate new key pair
151+ docker run -i --rm eftechcombr/wireguard wg genkey | tee ./etc/privatekey | docker run -i --rm eftechcombr/wireguard wg pubkey > ./etc/publickey
152+
153+ # Show current public key
154+ docker run -i --rm eftechcombr/wireguard wg pubkey < ./etc/privatekey
155+ ```
156+
157+ ### Container Management
158+ ``` sh
159+ # Build Docker image
160+ docker build -t eftechcombr/wireguard .
161+
162+ # Run with all recommended settings
163+ docker compose up -d
164+
165+ # View container logs
166+ docker logs -f wireguard
167+
168+ # Check VPN status inside container
169+ docker exec wireguard wg show
170+
171+ # Test health endpoint
172+ curl -f http://localhost:8080 || echo " Health check failed"
173+ ```
174+
175+ ### Monitoring
176+ ``` sh
177+ # Monitor interface status
178+ watch -n 1 ' docker exec wireguard wg show'
179+
180+ # Check system resources
181+ docker stats wireguard
182+ ```
75183
76184---
77185
186+ ## Security Considerations
187+
188+ - π ** Container capabilities** : Requires ` NET_ADMIN ` for network interface management
189+ - π‘οΈ ** Key protection** : Private keys should be stored securely with proper permissions (600)
190+ - π ** Network isolation** : Consider running in isolated Docker networks
191+ - π ** Monitoring** : Use the health check endpoint for load balancer integration
192+ - π¨ ** Signal handling** : Container responds gracefully to SIGTERM/SIGINT
193+
194+ ## Troubleshooting
195+
196+ ### Common Issues
197+
198+ ** Problem** : Container fails to start with permission errors
199+ ``` sh
200+ # Fix: Ensure proper permissions
201+ chmod 600 ./etc/privatekey
202+ chmod 644 ./etc/publickey
203+ chmod 644 ./etc/wg0.conf
204+ ```
205+
206+ ** Problem** : VPN traffic not routing
207+ ``` sh
208+ # Check if interface is up
209+ docker exec wireguard ip link show wg0
210+
211+ # Verify Wireguard status
212+ docker exec wireguard wg show
213+ ```
214+
215+ ** Problem** : Health check returning 503
216+ ``` sh
217+ # Verify interface carrier
218+ docker exec wireguard cat /sys/class/net/wg0/carrier
219+
220+ # Expected output: "1" (interface up)
221+ ```
222+
223+ ## Performance Tuning
224+
225+ - ** MTU** : Set appropriate MTU in wg0.conf (typically 1420)
226+ - ** Keepalive** : Use ` PersistentKeepalive = 25 ` for NAT traversal
227+ - ** DNS** : Configure appropriate DNS servers for client configurations
228+ - ** Buffer sizes** : Adjust TCP/UDP buffers for high-throughput scenarios
229+
78230## References
79231
80232- [ Wireguard Documentation] ( https://www.wireguard.com/ )
233+ - [ Wireguard Tools Manual] ( https://manpages.debian.org/unstable/wireguard-tools/wg.8.en.html )
234+ - [ Docker Networking Best Practices] ( https://docs.docker.com/network/ )
235+ - [ Linux Security Modules] ( https://www.kernel.org/doc/html/latest/security/lsm.html )
81236
82237---
83238
84239## License
85240
86241MIT
87242
243+ ---
244+
245+ ** π§ Tested on Alpine Linux 3.22 | π³ Docker Ready | π Production Grade**
246+
88247
0 commit comments