testcontainers-java
testcontainers-java copied to clipboard
[Bug]: Post-hook script not triggered when stopping custom Docker container via Testcontainers due to trap signal not being invoked
Module
Core
Testcontainers version
1.20.6
Using the latest Testcontainers version?
Yes
Host OS
Windows
Host Arch
x86
Docker version
Client:
Version: 27.5.1-rd
API version: 1.45 (downgraded from 1.47)
Go version: go1.22.11
Git commit: 0c97515
Built: Thu Jan 23 18:14:31 2025
OS/Arch: windows/amd64
Context: default
Server:
Engine:
Version: 26.1.5
API version: 1.45 (minimum version 1.24)
Go version: go1.22.5
Git commit: 411e817ddf710ff8e08fa193da80cb78af708191
Built: Fri Jul 26 17:51:06 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.7.17
GitCommit: 3a4de459a68952ffb703bbe7f2290861a75b6b67
runc:
Version: 1.1.14
GitCommit: 2c9f5602f0ba3d9da1c2596322dfc4e156844890
docker-init:
Version: 0.19.0
GitCommit:
What happened?
I’ve implemented pre-hook and post-hook scripts for a custom Docker image. The intention is for the pre-hook to execute when the container starts, and the post-hook to run when the container stops. When I manually start and stop the container using the docker run command and terminate it with Ctrl+C, both hooks function correctly. However, when I run the same Docker image using Testcontainers, the post-hook doesn’t get triggered. I’ve configured a trap to catch SIGTERM, SIGINT, and EXIT signals to invoke the on_shutdown function as the post-hook, but it doesn’t appear to execute in the Testcontainers environment. How can we make test container trap any SIGNAL to make post hook executed?
Relevant log output
Additional Information
`#!/bin/bash
MODE="$1"
Trap shutdown to send metrics
function on_shutdown() { echo "🔌 $MODE container ..." echo "📊 Logging to New Relic logs $MODE container..." echo "✅ Post-hook complete." }
-------- PRE-HOOK: TaaS Authentication --------
echo "🔐 Validating TaaS authentication for $MODE container." echo "✅ Authentication successful for $MODE container."
if [ "$MODE" == "test" ] || [ "$MODE" == "stub" ]; then trap on_shutdown SIGTERM SIGINT EXIT
echo "Running in $MODE mode..." echo "🚀 Executing Specmatic with args : $@" exec java -jar /app.jar "$@" exit_code=$? echo "Exiting with code: $exit_code" else echo "Unknown MODE: $MODE. Use 'test' or 'stub'." exit 1 fi
Below is the Dockerfile configuration: FROM myapp_image:latest RUN apk add --no-cache tini bash COPY script.sh /entrypoint-wrapper.sh RUN chmod +x /entrypoint-wrapper.sh ENTRYPOINT ["/sbin/tini", "--", "/entrypoint-wrapper.sh"]
Below is Container configuration in my SpringBoot application GenericContainer<?> specmaticProductStubContainer = new FixedHostPortGenericContainer<>("myapp_image:latest") .withLabel("name", "product-stub-container") .withCommand("stub", "--examples=examples", "--port=" + PRODUCT_STUB_PORT) .withFixedExposedPort(PRODUCT_STUB_PORT, PRODUCT_STUB_PORT) .withLogConsumer(outputFrame -> System.out.print(outputFrame.getUtf8String())) .waitingFor(Wait.forListeningPort()) .withStartupTimeout(Duration.ofSeconds(30)); `