Two peers, one command: running the VPN in Docker
The namespace lab works, but every test meant several terminals and a few sudo commands. This pull request packages govpn as a Docker image and adds a Compose file that runs two peers, so the whole setup comes up with docker compose up.
A small image
The Dockerfile has two stages. The first uses the official Go image to build a static binary. The second copies only that binary and an entrypoint script into Alpine:
FROM golang:1.27.1 AS build
WORKDIR /govpn
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o ./bin/govpn ./cmd/govpn
FROM alpine:3.24.2
WORKDIR /govpn
COPY --from=build /govpn/bin/govpn ./bin/govpn
COPY --chmod=755 docker/entrypoint.sh ./docker/entrypoint.sh
ENTRYPOINT ["/govpn/docker/entrypoint.sh"]
Copying go.mod and go.sum before the source lets Docker cache the dependency download. CGO_ENABLED=0 produces a binary with no libc dependency, and -s -w strips debug information. The final image is about 17 MB. I kept Alpine instead of scratch because the entrypoint needs a shell and the ip command.
What a container needs for a TUN device
By default a container can’t create network interfaces. Each peer needs two things in the Compose file:
cap_add: NET_ADMIN, to create and configure interfaces.- The host’s
/dev/net/tundevice mapped into the container.
The two peers sit on a private bridge network with fixed addresses, 172.30.0.2 and 172.30.0.3, and point -peer at each other.
Configuring the interface
At this point govpn only creates the TUN device. Giving it an address, bringing it up, and adding a route still happen outside the program. In the lab I did that by hand. In Docker, the entrypoint does it.
There’s an ordering problem: the device doesn’t exist until govpn starts, but govpn must be the main process. The entrypoint solves it with a background subshell that waits for tun0 to appear, then configures it:
(
until ip link show tun0 >/dev/null 2>&1; do
sleep 0.2
done
ip addr add "$TUN_IP" dev tun0
ip link set tun0 up
ip route add "$REMOTE_NET" dev tun0
) &
exec ./bin/govpn "$@"
The real script also gives up after 50 retries and logs each step. The exec matters: it replaces the shell with govpn, so govpn runs as PID 1 and receives the SIGTERM from docker compose down directly. Without it, the shell would get the signal and the clean shutdown from the previous post would never run.
Knowing when it works
Each container has a healthcheck that verifies tun0 has the expected address and the route to the other peer exists. docker compose ps shows (healthy) once the tunnel is ready. With restart: on-failure, a peer that crashes comes back and the entrypoint configures it again.
Testing is now three commands: start, ping, stop.
docker compose up -d --build
docker compose exec govpn-a ping -c1 10.0.1.1
docker compose down
What’s next
Two peers now come up with one command, but nothing checks that the code works before it’s merged. Next are the first tests and a CI pipeline.