Repository navigation
Open
Description
Expected Behaviour
After restart(), a consumer keeps receiving the container’s output. The internal logger already gets this behavior and Go keeps log consumers across stop/start.
Below is just for illustration:
4.3s [---] restart() called
4.5s [consumer] stream ended
4.8s [---] restart() done
4.8s [logs()] tick 1
+ 4.8s [consumer] tick 1
5.8s [logs()] tick 2
+ 5.8s [consumer] tick 2
6.8s [logs()] tick 3
+ 6.8s [consumer] tick 3
…
10.8s [logs()] tick 7
+ 10.8s [consumer] tick 7
- consumer: 4 lines before restart, 0 after (expected: keeps receiving)
+ consumer: 4 lines before restart, 7 after
logs(): 7 lines after restart (container is running)Actual Behaviour
At restart(), the consumer’s stream emits end. After that, the consumer receives nothing, while the container keeps emitting.
…
4.3s [consumer] tick 4
4.3s [---] restart() called
4.5s [consumer] stream ended
4.8s [---] restart() done
4.8s [logs()] tick 1
5.8s [logs()] tick 2
6.8s [logs()] tick 3
…
10.8s [logs()] tick 7
consumer: 4 lines before restart, 0 after (expected: keeps receiving)
logs(): 7 lines after restart (container is running)
Testcontainers Logs
Details
2026-11-20T19:26:27.247Z testcontainers [DEBUG] Checking container runtime strategy "TestcontainersHostStrategy"...
2026-11-20T19:26:27.249Z testcontainers [DEBUG] Container runtime strategy "TestcontainersHostStrategy" is not applicable
2026-11-20T19:26:27.249Z testcontainers [DEBUG] Checking container runtime strategy "ConfigurationStrategy"...
2026-11-20T19:26:27.249Z testcontainers [DEBUG] Container runtime strategy "ConfigurationStrategy" is not applicable
2026-11-20T19:26:27.249Z testcontainers [DEBUG] Checking container runtime strategy "UnixSocketStrategy"...
2026-11-20T19:26:27.250Z testcontainers [TRACE] Fetching Docker info...
2026-11-20T19:26:27.309Z testcontainers [TRACE] Fetching remote container runtime socket path...
2026-11-20T19:26:27.309Z testcontainers [TRACE] Resolving host...
2026-11-20T19:26:27.309Z testcontainers [TRACE] Fetching Compose info...
2026-11-20T19:26:27.310Z testcontainers [TRACE] Looking up host IPs...
2026-11-20T19:26:27.313Z testcontainers [TRACE] Initialising clients...
2026-11-20T19:26:27.313Z testcontainers [TRACE] Container runtime info:
{
"node": {
"version": "v24.19.0",
"architecture": "x64",
"platform": "linux"
},
"containerRuntime": {
"host": "localhost",
"hostIps": [
{
"address": "127.0.0.1",
"family": 4
}
],
"remoteSocketPath": "/var/run/docker.sock",
"indexServerAddress": "https://index.docker.io/v1/",
"serverVersion": "29.7.2",
"operatingSystem": "Ubuntu 24.04.4 LTS",
"operatingSystemType": "linux",
"architecture": "x86_64",
"cpus": 4,
"memory": 8054751232,
"runtimes": [
"io.containerd.runc.v2",
"runc"
],
"labels": []
}
}
2026-11-20T19:26:27.313Z testcontainers [DEBUG] Container runtime strategy "UnixSocketStrategy" works
2026-11-20T19:26:27.314Z testcontainers [DEBUG] Checking if image exists "alpine:latest"...
2026-11-20T19:26:27.389Z testcontainers [DEBUG] Checked if image exists "alpine:latest"
2026-11-20T19:26:27.389Z testcontainers [DEBUG] Image "alpine:latest" already exists
2026-11-20T19:26:27.391Z testcontainers [DEBUG] Acquiring lock file "/tmp/testcontainers-node-1000.lock"...
2026-11-20T19:26:27.395Z testcontainers [DEBUG] Acquired lock file "/tmp/testcontainers-node-1000.lock"
2026-11-20T19:26:27.395Z testcontainers [DEBUG] Listing containers...
2026-11-20T19:26:27.423Z testcontainers [DEBUG] Listed containers
2026-11-20T19:26:27.424Z testcontainers [DEBUG] Creating new Reaper for session "dfbb8f52a68a" with socket path "/var/run/docker.sock"...
2026-11-20T19:26:27.424Z testcontainers [DEBUG] Checking if image exists "testcontainers/ryuk:0.14.0"...
2026-11-20T19:26:27.493Z testcontainers [DEBUG] Checked if image exists "testcontainers/ryuk:0.14.0"
2026-11-20T19:26:27.493Z testcontainers [DEBUG] Image "testcontainers/ryuk:0.14.0" already exists
2026-11-20T19:26:27.494Z testcontainers [DEBUG] Creating container for image "testcontainers/ryuk:0.14.0"...
2026-11-20T19:26:27.610Z testcontainers [DEBUG] [fdf95914c2a3] Created container for image "testcontainers/ryuk:0.14.0"
2026-11-20T19:26:27.610Z testcontainers [INFO] [fdf95914c2a3] Starting container for image "testcontainers/ryuk:0.14.0"...
2026-11-20T19:26:27.611Z testcontainers [DEBUG] [fdf95914c2a3] Starting container...
2026-11-20T19:26:27.931Z testcontainers [DEBUG] [fdf95914c2a3] Started container
2026-11-20T19:26:27.931Z testcontainers [INFO] [fdf95914c2a3] Started container for image "testcontainers/ryuk:0.14.0"
2026-11-20T19:26:27.936Z testcontainers [DEBUG] [fdf95914c2a3] Fetching container logs...
2026-11-20T19:26:27.938Z testcontainers [DEBUG] [fdf95914c2a3] Waiting for container to be ready...
2026-11-20T19:26:27.938Z testcontainers [DEBUG] [fdf95914c2a3] Waiting for log message "/.*Started.*/"...
2026-11-20T19:26:27.938Z testcontainers [DEBUG] [fdf95914c2a3] Fetching container logs...
2026-11-20T19:26:27.941Z testcontainers [DEBUG] [fdf95914c2a3] Demuxing stream...
2026-11-20T19:26:27.942Z testcontainers [DEBUG] [fdf95914c2a3] Demuxed stream
2026-11-20T19:26:27.942Z testcontainers [DEBUG] [fdf95914c2a3] Demuxing stream...
2026-11-20T19:26:27.942Z testcontainers [DEBUG] [fdf95914c2a3] Demuxed stream
2026-11-20T19:26:28.191Z testcontainers:containers [fdf95914c2a3] time=2026-11-20T19:26:28.190Z level=INFO msg=starting connection_timeout=1m0s reconnection_timeout=10s request_timeout=10s shutdown_timeout=10m0s remove_retries=10 retry_offset=-1s changes_retry_interval=1s port=8080 verbose=false
2026-11-20T19:26:28.193Z testcontainers [DEBUG] [fdf95914c2a3] Log wait strategy complete
2026-11-20T19:26:28.196Z testcontainers [INFO] [fdf95914c2a3] Container is ready
2026-11-20T19:26:28.197Z testcontainers [DEBUG] [fdf95914c2a3] Connecting to Reaper (attempt 1) on "localhost:33012"...
2026-11-20T19:26:28.198Z testcontainers:containers [fdf95914c2a3] time=2026-11-20T19:26:28.190Z level=INFO msg=Started address=[::]:8080
2026-11-20T19:26:28.199Z testcontainers:containers [fdf95914c2a3] time=2026-11-20T19:26:28.190Z level=INFO msg="client processing started"
2026-11-20T19:26:28.199Z testcontainers [DEBUG] [fdf95914c2a3] Connected to Reaper
2026-11-20T19:26:28.200Z testcontainers [DEBUG] Releasing lock file "/tmp/testcontainers-node-1000.lock"...
2026-11-20T19:26:28.201Z testcontainers [DEBUG] Released lock file "/tmp/testcontainers-node-1000.lock"
2026-11-20T19:26:28.201Z testcontainers [DEBUG] Creating container for image "alpine:latest"...
2026-11-20T19:26:28.203Z testcontainers:containers [fdf95914c2a3] time=2026-11-20T19:26:28.200Z level=INFO msg="client connected" address=172.17.0.1:55756 clients=1
2026-11-20T19:26:28.203Z testcontainers:containers [fdf95914c2a3] time=2026-11-20T19:26:28.201Z level=INFO msg="adding filter" type=label values="[org.testcontainers.session-id=dfbb8f52a68a]"
2026-11-20T19:26:28.312Z testcontainers [DEBUG] [501c446c6ed7] Created container for image "alpine:latest"
2026-11-20T19:26:28.312Z testcontainers [INFO] [501c446c6ed7] Starting container for image "alpine:latest"...
2026-11-20T19:26:28.312Z testcontainers [DEBUG] [501c446c6ed7] Starting container...
2026-11-20T19:26:28.582Z testcontainers [DEBUG] [501c446c6ed7] Started container
2026-11-20T19:26:28.582Z testcontainers [INFO] [501c446c6ed7] Started container for image "alpine:latest"
2026-11-20T19:26:28.584Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:28.585Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:28.586Z testcontainers [DEBUG] [501c446c6ed7] Waiting for container to be ready...
2026-11-20T19:26:28.586Z testcontainers [DEBUG] [501c446c6ed7] Waiting for log message "tick 1"...
2026-11-20T19:26:28.586Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:28.587Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:28.588Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
1.3s [consumer] tick 1
2026-11-20T19:26:28.589Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:28.589Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
2026-11-20T19:26:28.589Z testcontainers:containers [501c446c6ed7] tick 1
2026-11-20T19:26:28.590Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:28.590Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
2026-11-20T19:26:28.591Z testcontainers [DEBUG] [501c446c6ed7] Log wait strategy complete
2026-11-20T19:26:28.591Z testcontainers [INFO] [501c446c6ed7] Container is ready
2026-11-20T19:26:29.575Z testcontainers:containers [501c446c6ed7] tick 2
2.3s [consumer] tick 2
3.3s [consumer] tick 3
2026-11-20T19:26:30.577Z testcontainers:containers [501c446c6ed7] tick 3
2026-11-20T19:26:31.578Z testcontainers:containers [501c446c6ed7] tick 4
4.3s [consumer] tick 4
4.3s [---] restart() called
2026-11-20T19:26:31.591Z testcontainers [INFO] [501c446c6ed7] Restarting container...
2026-11-20T19:26:31.592Z testcontainers [DEBUG] [501c446c6ed7] Restarting container...
4.5s [consumer] stream ended
2026-11-20T19:26:32.015Z testcontainers [DEBUG] [501c446c6ed7] Restarted container
2026-11-20T19:26:32.017Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:32.018Z testcontainers [DEBUG] [501c446c6ed7] Waiting for container to be ready...
2026-11-20T19:26:32.018Z testcontainers [DEBUG] [501c446c6ed7] Waiting for log message "tick 1"...
2026-11-20T19:26:32.018Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:32.019Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:32.019Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
2026-11-20T19:26:32.020Z testcontainers:containers [501c446c6ed7] tick 1
2026-11-20T19:26:32.020Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:32.020Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
2026-11-20T19:26:32.020Z testcontainers [DEBUG] [501c446c6ed7] Log wait strategy complete
2026-11-20T19:26:32.021Z testcontainers [INFO] [501c446c6ed7] Container is ready
2026-11-20T19:26:32.021Z testcontainers [INFO] [501c446c6ed7] Restarted container
4.8s [---] restart() done
2026-11-20T19:26:32.021Z testcontainers [DEBUG] [501c446c6ed7] Fetching container logs...
2026-11-20T19:26:32.023Z testcontainers [DEBUG] [501c446c6ed7] Demuxing stream...
2026-11-20T19:26:32.023Z testcontainers [DEBUG] [501c446c6ed7] Demuxed stream
4.8s [logs()] tick 1
5.8s [logs()] tick 2
2026-11-20T19:26:33.007Z testcontainers:containers [501c446c6ed7] tick 2
2026-11-20T19:26:34.008Z testcontainers:containers [501c446c6ed7] tick 3
6.8s [logs()] tick 3
7.8s [logs()] tick 4
2026-11-20T19:26:35.011Z testcontainers:containers [501c446c6ed7] tick 4
2026-11-20T19:26:36.013Z testcontainers:containers [501c446c6ed7] tick 5
8.8s [logs()] tick 5
9.8s [logs()] tick 6
2026-11-20T19:26:37.015Z testcontainers:containers [501c446c6ed7] tick 6
2026-11-20T19:26:38.017Z testcontainers:containers [501c446c6ed7] tick 7
10.8s [logs()] tick 7
consumer: 4 lines before restart, 0 after (expected: keeps receiving)
logs(): 7 lines after restart (container is running)
Steps to Reproduce
npm install testcontainers@12.2.2- Save the code as
restart-repro.ts. - Run
node restart-repro.ts(Node 24+, for type stripping and await using).
Repro script (restart-repro.ts)
import { GenericContainer, Wait } from "testcontainers";
import type { Readable } from "node:stream";
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
const t0 = Date.now();
const t = () => ((Date.now() - t0) / 1000).toFixed(1) + "s";
const log = (label: string, msg: string) => console.log(`${t()} [${label}] ${msg}`);
const counts: Record<string, number> = {};
const watch = (label: string, stream: Readable) => {
counts[label] = 0;
stream.on("data", (chunk: string) => { counts[label]++; log(label, chunk.trim()); });
stream.on("end", () => log(label, "stream ended"));
};
await using container = await new GenericContainer("alpine")
.withCommand(["sh", "-c", 'i=1; while true; do echo "tick $i"; i=$(( i+1 )); sleep 1; done'])
.withLogConsumer((stream) => watch("consumer", stream))
.withWaitStrategy(Wait.forLogMessage("tick 1"))
.start();
await sleep(3000);
const since = Date.now() / 1000;
const linesConsumerBeforeRestart = counts["consumer"];
log("---", "restart() called");
await container.restart();
log("---", "restart() done");
const stream = await container.logs({ since }); // a fresh stream
watch("logs()", stream);
await sleep(6000);
console.log(`consumer: ${linesConsumerBeforeRestart} lines before restart, ${counts["consumer"] - linesConsumerBeforeRestart} after (expected: keeps receiving)`);
console.log(`logs(): ${counts["logs()"]} lines after restart (container is running)`);
process.exit();Environment Information
- Operating System: Ubuntu 24.04.4 LTS (x86_64)
- Container runtime and version: Docker Engine 29.7.2
- Node version: v24.19.0
- Test runner and version: None (standalone script)
- Testcontainers version: 12.2.2 (published), image alpine:latest (Alpine 3.24.4)
- Last Testcontainers version where this worked, if any: None
Notes
- No test checks this. The existing test only covers the internal logger. I would add a test for the consumer case.
- A fix could be for
restart()to reattach the consumer the same way it reattaches the internal logger. - Related: Call the lifecycle hooks and log consumer when a container is reused #1521, the same gap for a reused container instead of a restarted one. Add
getLogs()and a line-based log consumer #1544/AddLogConsumers.lineshelper to consume container logs line by line #1661, consumers built withLogConsumers.lineshave the same problem. - Happy to take this after Add
LogConsumers.lineshelper to consume container logs line by line #1661. - I used Claude to understand the codebase and search for relevant issues and code in this repo and testcontainers-go. Also, I asked it to run experiments to clarify the bug and format the code snippets in this issue. I wrote the repro code and this issue myself.
Metadata
Metadata
Assignees
Labels
No labels
Activity