Skip to content

Commit de6ad05

Browse files
Manage Wireguard restarts from docker container (ngoduykhanh#267)
1 parent be2ffba commit de6ad05

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ FROM alpine:3.16
6262
RUN addgroup -S wgui && \
6363
adduser -S -D -G wgui wgui
6464

65-
RUN apk --no-cache add ca-certificates
65+
RUN apk --no-cache add ca-certificates wireguard-tools jq
6666

6767
WORKDIR /app
6868

6969
RUN mkdir -p db
7070

7171
# Copy binary files
72-
COPY --from=builder --chown=wgui:wgui /build/wg-ui /app
73-
72+
COPY --from=builder --chown=wgui:wgui /build/wg-ui .
7473
RUN chmod +x wg-ui
74+
COPY init.sh .
7575

7676
EXPOSE 5000/tcp
7777
HEALTHCHECK CMD ["wget","--output-document=-","--quiet","--tries=1","http://127.0.0.1:5000/_health"]
78-
ENTRYPOINT ["./wg-ui"]
78+
ENTRYPOINT ["./init.sh"]

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Note:
3434

3535
- There is a Status option that needs docker to be able to access the network of the host in order to read the
3636
wireguard interface stats. See the `cap_add` and `network_mode` options on the docker-compose.yaml
37+
- Similarly the `WGUI_MANAGE_START` and `WGUI_MANAGE_RESTART` settings need the same access, in order to restart the wireguard interface.
3738
- Because the `network_mode` is set to `host`, we don't need to specify the exposed ports. The app will listen on port `5000` by default.
3839

3940

@@ -75,6 +76,15 @@ These environment variables are used to set the defaults used in `New Client` di
7576
| `WGUI_DEFAULT_CLIENT_USE_SERVER_DNS` | Boolean value [`0`, `f`, `F`, `false`, `False`, `FALSE`, `1`, `t`, `T`, `true`, `True`, `TRUE`] (default `true`) |
7677
| `WGUI_DEFAULT_CLIENT_ENABLE_AFTER_CREATION` | Boolean value [`0`, `f`, `F`, `false`, `False`, `FALSE`, `1`, `t`, `T`, `true`, `True`, `TRUE`] (default `true`) |
7778

79+
### Docker only
80+
81+
These environment variables only apply to the docker container.
82+
83+
| Variable | Description |
84+
|-----------------------|----------------------------------------------------------------------------------|
85+
| `WGUI_MANAGE_START` | Start/stop WireGaurd when the container is started/stopped. (default `false`) |
86+
| `WGUI_MANAGE_RESTART` | Auto restart WireGuard when we Apply Config changes in the UI. (default `false`) |
87+
7888
### Email configuration
7989

8090
To use custom `wg.conf` template set the `WG_CONF_TEMPLATE` environment variable to a path to such file. Make sure `wireguard-ui` will be able to work with it - use [default template](templates/wg.conf) for reference.
@@ -168,6 +178,12 @@ rc-service wgui start
168178
rc-update add wgui default
169179
```
170180

181+
### docker
182+
183+
Set `WGUI_MANAGE_RESTART=true` to manage Wireguard interface restarts.
184+
Using `WGUI_MANAGE_START=true` can also replace the function of `wg-quick@wg0` service, to start Wireguard at boot, by running the container with `restart: unless-stopped`.
185+
These settings can also pick up changes to Wireguard Config File Path, after restarting the container.
186+
171187
## Build
172188

173189
### Build docker image

docker-compose.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ services:
1616
- WGUI_USERNAME=alpha
1717
- WGUI_PASSWORD=this-unusual-password
1818
- WG_CONF_TEMPLATE
19+
- WGUI_MANAGE_START=false
20+
- WGUI_MANAGE_RESTART=false
1921
logging:
2022
driver: json-file
2123
options:

init.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# extract wg config file path, or use default
4+
conf="$(jq -r .config_file_path db/server/global_settings.json || echo /etc/wireguard/wg0.conf)"
5+
6+
# manage wireguard stop/start with the container
7+
case $WGUI_MANAGE_START in (1|t|T|true|True|TRUE)
8+
wg-quick up "$conf"
9+
trap 'wg-quick down "$conf"' SIGTERM # catches container stop
10+
esac
11+
12+
# manage wireguard restarts
13+
case $WGUI_MANAGE_RESTART in (1|t|T|true|True|TRUE)
14+
[[ -f $conf ]] || touch "$conf" # inotifyd needs file to exist
15+
inotifyd - "$conf":w | while read -r event file; do
16+
wg-quick down "$file"
17+
wg-quick up "$file"
18+
done &
19+
esac
20+
21+
22+
./wg-ui &
23+
wait $!

0 commit comments

Comments
 (0)