Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Last active January 17, 2025 09:55
Show Gist options
  • Save mumoshu/749091e6aa8ef20815e9827cb90553b7 to your computer and use it in GitHub Desktop.
Save mumoshu/749091e6aa8ef20815e9827cb90553b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
dockerfile=$(mktemp)
on_exit() {
echo "Removing $dockerfile";
rm -f $dockerfile
}
on_sigint() {
echo "SIGINT received. Exiting...";
exit 0
}
trap on_sigint SIGINT
trap on_exit EXIT
cat <<'EOF' > $dockerfile
FROM ubuntu:22.04
# Create a script that generates sample data and then ignores SIGTERM
RUN echo '#!/bin/bash\n\
SIZE_MB=${1:-100} # Default to 100MB if no size specified\n\
\n\
echo "Generating ${SIZE_MB}MB of sample data..."\n\
dd if=/dev/urandom of=/data.bin bs=1M count=$SIZE_MB\n\
echo "Generated ${SIZE_MB}MB of data at /data.bin"\n\
\n\
trap "" SIGTERM\n\
echo "Started process that ignores SIGTERM"\n\
while true; do\n\
echo "Still running... (will ignore SIGTERM)"\n\
sleep 1\n\
done' > /entrypoint.sh
# Make the script executable
RUN chmod +x /entrypoint.sh
# Run the script with default 100MB
CMD ["/entrypoint.sh"]
EOF
# Build and run
docker build -t unstoppable -f $dockerfile .
# Try to stop it and observe the delay
# time docker stop test
set -ex
for i in {1..100}; do
# time docker kill test || true
# time docker stop -t 20 test || true
time docker rm -f test
docker run --init -d --name test unstoppable /entrypoint.sh 101 || (docker logs test; docker inspect test; exit 1)
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment