`"err"` event on log streams never fires: stderr arrives as `"data"`

未关闭
#1,642 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
docker, node.js, typescript
领域
testing

调研方向

Start in packages/testcontainers/src/container-runtime/clients/container/docker-container-client.ts, where the log stream is demuxed by passing one PassThrough as both stdout and stderr targets, and compare it with the exec path that uses separate streams. The issue also lists the log-wait-strategy.ts and generic-container.ts err listeners. Done means settling the API direction (the author proposes options 1 to 3) with maintainers before code changes, then a test showing stderr reaching the agreed listener.

由索引模型根据 Issue 内容生成。

描述

Expected Behaviour
Per the docs, stdout arrives through "data", stderr through "err".

import { GenericContainer, Wait } from "testcontainers";

await using container = await new GenericContainer("alpine")
  .withCommand(["sh", "-c", "echo to-stdout; echo to-stderr >&2; sleep infinity"])
  .withLogConsumer((stream) => {
    stream.on("data", (line) => console.log(`data: ${line}`));
    stream.on("err", (line) => console.error(`err: ${line}`));
  })
  .withWaitStrategy(Wait.forLogMessage("to-stderr"))
  .start();

Expected output:

data: to-stdout
err: to-stderr

Actual Behaviour

Actual output:

data: to-stdout
data: to-stderr

"err" never fires. stderr arrives on "data".

Testcontainers Logs

2026-11-13T20:29:02.729Z testcontainers [DEBUG] Checking container runtime strategy "TestcontainersHostStrategy"...
2026-11-13T20:29:02.730Z testcontainers [DEBUG] Container runtime strategy "TestcontainersHostStrategy" is not applicable
2026-11-13T20:29:02.730Z testcontainers [DEBUG] Checking container runtime strategy "ConfigurationStrategy"...
2026-11-13T20:29:02.731Z testcontainers [DEBUG] Container runtime strategy "ConfigurationStrategy" is not applicable
2026-11-13T20:29:02.731Z testcontainers [DEBUG] Checking container runtime strategy "UnixSocketStrategy"...
2026-11-13T20:29:02.731Z testcontainers [TRACE] Fetching Docker info...
2026-11-13T20:29:02.776Z testcontainers [TRACE] Fetching remote container runtime socket path...
2026-11-13T20:29:02.776Z testcontainers [TRACE] Resolving host...
2026-11-13T20:29:02.777Z testcontainers [TRACE] Fetching Compose info...
2026-11-13T20:29:02.777Z testcontainers [TRACE] Looking up host IPs...
2026-11-13T20:29:02.782Z testcontainers [TRACE] Initialising clients...
2026-11-13T20:29:02.782Z 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-13T20:29:02.782Z testcontainers [DEBUG] Container runtime strategy "UnixSocketStrategy" works
2026-11-13T20:29:02.783Z testcontainers [DEBUG] Checking if image exists "alpine:latest"...
2026-11-13T20:29:02.855Z testcontainers [DEBUG] Checked if image exists "alpine:latest"
2026-11-13T20:29:02.855Z testcontainers [DEBUG] Image "alpine:latest" already exists
2026-11-13T20:29:02.857Z testcontainers [DEBUG] Acquiring lock file "/tmp/testcontainers-node-1000.lock"...
2026-11-13T20:29:02.859Z testcontainers [DEBUG] Acquired lock file "/tmp/testcontainers-node-1000.lock"
2026-11-13T20:29:02.859Z testcontainers [DEBUG] Listing containers...
2026-11-13T20:29:02.881Z testcontainers [DEBUG] Listed containers
2026-11-13T20:29:02.885Z testcontainers [DEBUG] Creating new Reaper for session "30b4719e4c28" with socket path "/var/run/docker.sock"...
2026-11-13T20:29:02.886Z testcontainers [DEBUG] Checking if image exists "testcontainers/ryuk:0.14.0"...
2026-11-13T20:29:02.945Z testcontainers [DEBUG] Checked if image exists "testcontainers/ryuk:0.14.0"
2026-11-13T20:29:02.945Z testcontainers [DEBUG] Image "testcontainers/ryuk:0.14.0" already exists
2026-11-13T20:29:02.946Z testcontainers [DEBUG] Creating container for image "testcontainers/ryuk:0.14.0"...
2026-11-13T20:29:03.054Z testcontainers [DEBUG] [12fc1477fcca] Created container for image "testcontainers/ryuk:0.14.0"
2026-11-13T20:29:03.054Z testcontainers [INFO] [12fc1477fcca] Starting container for image "testcontainers/ryuk:0.14.0"...
2026-11-13T20:29:03.054Z testcontainers [DEBUG] [12fc1477fcca] Starting container...
2026-11-13T20:29:03.322Z testcontainers [DEBUG] [12fc1477fcca] Started container
2026-11-13T20:29:03.322Z testcontainers [INFO] [12fc1477fcca] Started container for image "testcontainers/ryuk:0.14.0"
2026-11-13T20:29:03.326Z testcontainers [DEBUG] [12fc1477fcca] Fetching container logs...
2026-11-13T20:29:03.328Z testcontainers [DEBUG] [12fc1477fcca] Waiting for container to be ready...
2026-11-13T20:29:03.328Z testcontainers [DEBUG] [12fc1477fcca] Waiting for log message "/.*Started.*/"...
2026-11-13T20:29:03.328Z testcontainers [DEBUG] [12fc1477fcca] Fetching container logs...
2026-11-13T20:29:03.334Z testcontainers [DEBUG] [12fc1477fcca] Demuxing stream...
2026-11-13T20:29:03.334Z testcontainers [DEBUG] [12fc1477fcca] Demuxed stream
2026-11-13T20:29:03.336Z testcontainers [DEBUG] [12fc1477fcca] Demuxing stream...
2026-11-13T20:29:03.336Z testcontainers [DEBUG] [12fc1477fcca] Demuxed stream
2026-11-13T20:29:03.618Z testcontainers [DEBUG] [12fc1477fcca] Log wait strategy complete
2026-11-13T20:29:03.626Z testcontainers [INFO] [12fc1477fcca] Container is ready
2026-11-13T20:29:03.626Z testcontainers [DEBUG] [12fc1477fcca] Connecting to Reaper (attempt 1) on "localhost:32817"...
2026-11-13T20:29:03.628Z testcontainers:containers [12fc1477fcca] time=2026-11-13T20:29:03.612Z 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-13T20:29:03.629Z testcontainers:containers [12fc1477fcca] time=2026-11-13T20:29:03.613Z level=INFO msg=Started address=[::]:8080
2026-11-13T20:29:03.629Z testcontainers:containers [12fc1477fcca] time=2026-11-13T20:29:03.613Z level=INFO msg="client processing started"
2026-11-13T20:29:03.630Z testcontainers [DEBUG] [12fc1477fcca] Connected to Reaper
2026-11-13T20:29:03.630Z testcontainers [DEBUG] Releasing lock file "/tmp/testcontainers-node-1000.lock"...
2026-11-13T20:29:03.631Z testcontainers [DEBUG] Released lock file "/tmp/testcontainers-node-1000.lock"
2026-11-13T20:29:03.634Z testcontainers [DEBUG] Creating container for image "alpine:latest"...
2026-11-13T20:29:03.635Z testcontainers:containers [12fc1477fcca] time=2026-11-13T20:29:03.632Z level=INFO msg="client connected" address=172.17.0.1:49260 clients=1
2026-11-13T20:29:03.636Z testcontainers:containers [12fc1477fcca] time=2026-11-13T20:29:03.633Z level=INFO msg="adding filter" type=label values="[org.testcontainers.session-id=30b4719e4c28]"
2026-11-13T20:29:03.738Z testcontainers [DEBUG] [dbca518416dd] Created container for image "alpine:latest"
2026-11-13T20:29:03.738Z testcontainers [INFO] [dbca518416dd] Starting container for image "alpine:latest"...
2026-11-13T20:29:03.738Z testcontainers [DEBUG] [dbca518416dd] Starting container...
2026-11-13T20:29:03.971Z testcontainers [DEBUG] [dbca518416dd] Started container
2026-11-13T20:29:03.971Z testcontainers [INFO] [dbca518416dd] Started container for image "alpine:latest"
2026-11-13T20:29:03.973Z testcontainers [DEBUG] [dbca518416dd] Fetching container logs...
2026-11-13T20:29:03.974Z testcontainers [DEBUG] [dbca518416dd] Fetching container logs...
2026-11-13T20:29:03.978Z testcontainers [DEBUG] [dbca518416dd] Waiting for container to be ready...
2026-11-13T20:29:03.978Z testcontainers [DEBUG] [dbca518416dd] Waiting for log message "to-stderr"...
2026-11-13T20:29:03.978Z testcontainers [DEBUG] [dbca518416dd] Fetching container logs...
2026-11-13T20:29:03.980Z testcontainers [DEBUG] [dbca518416dd] Demuxing stream...
2026-11-13T20:29:03.980Z testcontainers [DEBUG] [dbca518416dd] Demuxed stream
data: to-stderr

data: to-stdout

2026-11-13T20:29:03.982Z testcontainers [DEBUG] [dbca518416dd] Demuxing stream...
2026-11-13T20:29:03.982Z testcontainers [DEBUG] [dbca518416dd] Demuxed stream
2026-11-13T20:29:03.983Z testcontainers:containers [dbca518416dd] to-stderr
2026-11-13T20:29:03.983Z testcontainers:containers [dbca518416dd] to-stdout
2026-11-13T20:29:03.983Z testcontainers [DEBUG] [dbca518416dd] Demuxing stream...
2026-11-13T20:29:03.983Z testcontainers [DEBUG] [dbca518416dd] Demuxed stream
2026-11-13T20:29:03.983Z testcontainers [DEBUG] [dbca518416dd] Log wait strategy complete
2026-11-13T20:29:03.984Z testcontainers [INFO] [dbca518416dd] Container is ready

Steps to Reproduce

  1. npm install testcontainers
  2. Save the code as repro.ts.
  3. Run node repro.ts (Node 24+, for type stripping and await using).

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.1 (built from main c79a5a56; same code in the published 12.2.1), image alpine:latest
  • Last Testcontainers version where this worked, if any: None

Cause

https://github.com/testcontainers/testcontainers-node/blob/c79a5a56bcc824e207ac243207bd83e5b0459dd3/packages/testcontainers/src/container-runtime/clients/container/docker-container-client.ts#L353

modem.demuxStream(stream, demuxedStream, demuxedStream) passes the same PassThrough as both the stdout and the stderr target, so the label is lost. By contrast, exec passes separate stdout and stderr streams:

https://github.com/testcontainers/testcontainers-node/blob/c79a5a56bcc824e207ac243207bd83e5b0459dd3/packages/testcontainers/src/container-runtime/clients/container/docker-container-client.ts#L227

Other "err" listeners that never fire

Notes

  • Three ways to fix this I can think of:
  1. only take out "err" listener in the docs and the library files above so readers don't expect stderr on "err".
  2. keep "data" for both and add new filter options stdout and stderr to logs(), e.g. container.logs({ stdout: true, stderr: false }). This can leave demuxStream alone.
  3. split like exec (#842): keep the merged stream and add separate stdout and stderr streams, which requires new API.
  • 2 and 3 will still need 1 and a docs example for the new option. I'd lean toward 2 because it enables stderr filtering but is smaller than 3. Does that sound right?

  • Related: #1544. Its example text uses stream.on("err"), and its proposed getLogs() stdout/stderr option depends on this decision. With 1 alone, getLogs() couldn’t provide stdout/stderr option.

  • Happy to take this after my #1544 PR.

  • I used Claude to understand the codebase and search issues. I wrote this issue and will write the code myself.

主要语言
TypeScript
星标
2.6k
派生
287
平均合并
3 天 20 小时
30 天内合并 PR
11

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

testcontainers/testcontainers-node 的其他 Issue

查看 testcontainers/testcontainers-node 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。