summaryrefslogtreecommitdiff
path: root/start_container.sh
diff options
context:
space:
mode:
Diffstat (limited to 'start_container.sh')
-rwxr-xr-xstart_container.sh21
1 files changed, 16 insertions, 5 deletions
diff --git a/start_container.sh b/start_container.sh
index 2299390..f93f5df 100755
--- a/start_container.sh
+++ b/start_container.sh
@@ -29,6 +29,7 @@ podman build -t cgit "$(dirname "$0")"
29 29
30# Run container 30# Run container
31podman run -d \ 31podman run -d \
32 --init \
32 --name ${CONTAINER_NAME} \ 33 --name ${CONTAINER_NAME} \
33 --network ${NETWORK} \ 34 --network ${NETWORK} \
34 --ip ${PRIVATE_IP} \ 35 --ip ${PRIVATE_IP} \
@@ -39,11 +40,21 @@ podman run -d \
39 -v /git:/git \ 40 -v /git:/git \
40 localhost/cgit 41 localhost/cgit
41 42
42# Setup public IP 43# Setup public IP via DNAT/SNAT
43sleep 2 44sleep 2
44# Get the container's network interface name (e.g. eth0) 45OIFACE=$(ip route show default | awk '{print $5; exit}')
45IFACE=$(podman exec ${CONTAINER_NAME} sh -c "ip -o link | grep -v lo | head -1 | cut -d: -f2 | tr -d ' ' | cut -d@ -f1") 46BRIDGE=$(podman network inspect ${NETWORK} 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)[0]['network_interface'])")
46podman exec ${CONTAINER_NAME} ip addr add ${PUBLIC_IP}/32 dev ${IFACE} 47
47ip route add ${PUBLIC_IP}/32 via ${PRIVATE_IP} 48# Clean up any stale rules for this IP
49nft -a list chain ip nat PREROUTING 2>/dev/null | grep "daddr ${PUBLIC_IP} " | grep -oP 'handle \K\d+' | while read h; do nft delete rule ip nat PREROUTING handle "$h"; done
50nft -a list chain ip nat POSTROUTING 2>/dev/null | grep "snat to ${PUBLIC_IP}" | grep -oP 'handle \K\d+' | while read h; do nft delete rule ip nat POSTROUTING handle "$h"; done
51nft -a list chain inet netavark FORWARD 2>/dev/null | grep "daddr ${PRIVATE_IP} " | grep -oP 'handle \K\d+' | while read h; do nft delete rule inet netavark FORWARD handle "$h"; done
52nft -a list chain ip nat POSTROUTING 2>/dev/null | grep "daddr ${PRIVATE_IP}.*masquerade" | grep -oP 'handle \K\d+' | while read h; do nft delete rule ip nat POSTROUTING handle "$h"; done
53ip route del ${PUBLIC_IP} 2>/dev/null || true
54
55nft add rule ip nat PREROUTING ip daddr ${PUBLIC_IP} dnat to ${PRIVATE_IP}
56nft add rule ip nat POSTROUTING ip saddr ${PRIVATE_IP} oifname ${OIFACE} snat to ${PUBLIC_IP}
57nft insert rule inet netavark FORWARD ip daddr ${PRIVATE_IP} oifname ${BRIDGE} accept
58nft add rule ip nat POSTROUTING ip saddr ${PRIVATE_SUBNET} ip daddr ${PRIVATE_IP} oifname ${BRIDGE} masquerade
48 59
49echo "Running at https://${DOMAIN}/" 60echo "Running at https://${DOMAIN}/"