proper terminate all background processes in ucind containers

This commit is contained in:
Pavel Sviderski
2024-11-29 09:46:29 +10:00
parent 48767125f4
commit bdd034d169
2 changed files with 27 additions and 2 deletions
+26 -2
View File
@@ -1,7 +1,28 @@
#!/bin/sh
set -eu
# TODO: cleanup /run/docker.pid on exit to support container restart.
# Cleanup function to properly terminate background processes.
cleanup() {
echo "Terminating container processes..."
# Terminate the main process if it has been started.
if [ -n "$MAIN_PID" ]; then
kill "$MAIN_PID" 2>/dev/null || true
fi
# Terminate Docker daemon if PID file exists.
if [ -f /run/docker.pid ]; then
kill "$(cat /run/docker.pid)" 2>/dev/null || true
fi
# Terminate socat proxy if running.
pkill socat 2>/dev/null || true
# Wait for processes to terminate.
wait
}
trap cleanup INT TERM
dind dockerd &
echo "Waiting for Docker in Docker to be ready..."
timeout 5s sh -c "until docker info &> /dev/null; do sleep 0.1; done"
@@ -14,4 +35,7 @@ docker load < /images/corrosion.tar
echo "Proxying Uncloud API port 51000/tcp to Unix socket /run/uncloud/uncloud.sock..."
socat TCP-LISTEN:51000,reuseaddr,fork,bind="$(hostname -i)" UNIX-CONNECT:/run/uncloud/uncloud.sock &
exec "$@"
# Execute the passed command and wait for it while maintaining signal handling.
"$@" &
MAIN_PID=$!
wait $MAIN_PID