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
+1
View File
@@ -35,6 +35,7 @@ RUN apk --no-cache add crane
RUN crane pull "${CORROSION_IMAGE}" /corrosion.tar RUN crane pull "${CORROSION_IMAGE}" /corrosion.tar
# Uncloud-in-Docker (ucind) image for running Uncloud test clusters using Docker.
FROM docker:27.3.1-dind AS ucind FROM docker:27.3.1-dind AS ucind
# Create system group and user 'uncloud'. # Create system group and user 'uncloud'.
RUN addgroup -S uncloud && adduser -SHD -h /nonexistent -G uncloud -g "" uncloud RUN addgroup -S uncloud && adduser -SHD -h /nonexistent -G uncloud -g "" uncloud
+26 -2
View File
@@ -1,7 +1,28 @@
#!/bin/sh #!/bin/sh
set -eu 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 & dind dockerd &
echo "Waiting for Docker in Docker to be ready..." echo "Waiting for Docker in Docker to be ready..."
timeout 5s sh -c "until docker info &> /dev/null; do sleep 0.1; done" 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..." 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 & 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