Files
gitea-ci-in-gvisor/runner/start-dind.sh
T

44 lines
1.6 KiB
Bash

#!/bin/sh
set -eu
# This script runs *inside* a gVisor sandbox. It starts a Docker daemon whose
# containers also live inside that same sandbox. Jobs connect to this daemon
# through a shared unix-socket volume; they never see the privileged runner
# daemon's socket.
# In gVisor, `ip route show default` returns all routes, not only the default.
route=$(ip route show default | grep '^default ' | head -n1 || true)
dev=""
addr=""
if [ -n "$route" ]; then
dev=$(printf '%s\n' "$route" | sed -E 's/.* dev ([^ ]+).*/\1/')
addr=$(ip -o -4 addr show dev "$dev" | awk '{print $4}' | cut -d/ -f1)
fi
# Nested containers use the daemon's bridge network. Because dockerd is started
# with iptables disabled (required by gVisor), add the SNAT rule ourselves so
# those containers can reach the internet.
if [ -n "$dev" ] && [ -n "$addr" ]; then
echo 1 > /proc/sys/net/ipv4/ip_forward
if command -v iptables-legacy >/dev/null 2>&1; then
IPTABLES=iptables-legacy
else
IPTABLES=iptables
fi
"$IPTABLES" -t nat -A POSTROUTING -o "$dev" -j SNAT --to-source "$addr" -p tcp
"$IPTABLES" -t nat -A POSTROUTING -o "$dev" -j SNAT --to-source "$addr" -p udp
fi
rm -f /var/run/docker.sock
# The TCP listener is published only on the runner container's loopback. A
# unix socket created inside a gVisor sandbox cannot usefully be shared through
# a Docker volume, so jobs reach this daemon over loopback TCP instead.
exec dockerd \
--host=unix:///var/run/docker.sock \
--host=tcp://0.0.0.0:2375 \
--tls=false \
--iptables=false \
--ip6tables=false \
"$@"