[Bug]: Kafka container fails with 'Container exited with code 126' due to a race between command wait loop and script installation
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 56/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- docker, java, kafka
- 领域
- devops, testing-qa
调研方向
从 KafkaContainer 启动和 GenericContainer.tryStart 开始,然后跟踪 containerIsStarting 和 execInContainer,其中会复制 /tmp/testcontainers_start.sh。在所示命令和并行负载条件下重现报告的代码 126 或“Text file busy”失败。当启动等待能够可靠地仅在复制完成后运行脚本,并且 Kafka 现有的等待策略通过时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Module
Kafka
Testcontainers version
1.21.4
Using the latest Testcontainers version?
Yes
Host OS
Linux
Host Arch
x86-64
Docker version
Client:
Version: 20.10.24+dfsg1
API version: 1.41
Go version: go1.19.8
Git commit: 297e128
Built: Sat Jan 3 00:46:39 2026
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.24+dfsg1
API version: 1.41 (minimum version 1.12)
Go version: go1.19.8
Git commit: 5d6db84
Built: Sat Jan 3 00:46:39 2026
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.20~ds1
GitCommit: 1.6.20~ds1-1+deb12u2
runc:
Version: 1.1.5+ds1
GitCommit: 1.1.5+ds1-1+deb12u1
docker-init:
Version: 0.19.0
GitCommit:
What happened?
During some CI test runs Kafka container startup fails with Wait strategy failed. Container exited with code 126. The tests are run in parallel, on a relatively small virtual machine, the machine can experience high load during the tests.
The cause seem to be a race-like condition between startup script installation and waiting for the script in container command. The command is set to while [ ! -f /tmp/testcontainers_start.sh ]; do sleep 0.1; done; /tmp/testcontainers_start.sh so the condition only checks if the file exists and it's possible that the command will try to execute the script before it's fully written (or maybe also before it has permissions set correctly - although I'm unsure whether this can happen as I didn't manage to reproduce such exact scenario) resulting in 126 error.
This can be reproduced by removing sleep 0.1 from the loop. When container is created like this:
KafkaContainer kafkaContainer = new KafkaContainer("apache/kafka:3.8.0")
.withCommand("sh", "-c", "while [ ! -f /tmp/testcontainers_start.sh ]; do true; done; /tmp/testcontainers_start.sh");
container startup repeatedly fails with:
2026-04-10 21:48:06,243 [main] ERROR tc.apache/kafka:3.8.0 - Could not start container
java.lang.IllegalStateException: Wait strategy failed. Container exited with code 126
at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:525)
at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:346)
at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:336)
at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:322)
(...)
Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for log output matching '.*Transitioning from RECOVERY to RUNNING.*'
at org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy.waitUntilReady(LogMessageWaitStrategy.java:47)
at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:52)
at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:909)
at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:492)
... 58 common frames omitted
2026-04-10 21:48:06,254 [main] ERROR tc.apache/kafka:3.8.0 - Log output from the failed container:
sh: /tmp/testcontainers_start.sh: Text file busy
and it seem likely then on busy hosts the sleep 0.1 version can get the same error from time to time.
An easy fix is to run a separate 'notify command' step after the script is fully copied. With following changes:
private final class MyKafkaContainer extends KafkaContainer {
public FixedKafkaContainer(String imageName) {
super(imageName);
withCommand("sh", "-c", "while [ ! -f /tmp/startup-done ]; do true; done; /tmp/testcontainers_start.sh");
}
@Override
protected void containerIsStarting(InspectContainerResponse containerInfo) {
super.containerIsStarting(containerInfo); // copy the script
try {
execInContainer("sh", "-c", "touch /tmp/startup-done"); // notify command *after* the copy has finished
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
the container starts repeatedly without any errors.
If you think that this is a good solution I'd be more than happy to create a PR.
Relevant log output
Additional Information
No response
- 主要语言
- Java
- 星标
- 8.7k
- 派生
- 1.9k
- 平均合并
- 20 小时 43 分钟
- 30 天内合并 PR
- 4
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testcontainers/testcontainers-java 的其他 Issue
-
[Bug]: Build fails with "Unknown cli flag: --scripts-prepend-node-path" due to outdated Spotless plugin可能已有人在做 @dhruv9b 于 36 天前认领。 未关闭type/bug
难度 2/5 1-3 小时 新手友好度 78/100
testcontainers/testcontainers-java#11997 · 1 条评论 ·
维护者通常 1 天内回复
-
[Enhancement]: Document why singleton containers are required under Spring's test context caching可能已有人在做 @zakaullah075 于 54 天前认领。 未关闭type/enhancement
难度 1/5 1 小时以内 新手友好度 85/100
testcontainers/testcontainers-java#11967 · 1 条评论 ·
维护者通常 1 天内回复
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under Linux可能已有人在做 @kdelay 于 75 天前认领。 未关闭type/bug
难度 2/5 1-3 小时 新手友好度 78/100
testcontainers/testcontainers-java#11941 ·
维护者通常 1 天内回复
-
[Bug]: DockerDesktopClientProviderStrategy is always applicable可能已有人在做 @seonwooj0810 于 124 天前认领。 未关闭type/bug
难度 2/5 1-3 小时 新手友好度 74/100
testcontainers/testcontainers-java#11829 · 1 条评论 ·
维护者通常 1 天内回复
-
[Enhancement]: cleanup document after removing junit4 support可能已有人在做 @SJvaca30 于 132 天前认领。 未关闭type/enhancement
难度 2/5 1-3 小时 新手友好度 65/100
testcontainers/testcontainers-java#11578 ·
维护者通常 1 天内回复
查看 testcontainers/testcontainers-java 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 88/100
portfolio-performance/portfolio#6119 ·
维护者通常 8 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
jenkinsci/ec2-plugin#2041 ·
-
L: github:actions L: php:composer
难度 2/5 1-3 小时 新手友好度 88/100
dependabot/dependabot-core#16493 ·
维护者通常 1 天内回复
-
SHOW EDIT of a subclass for an object of its superclass: the form fails to open with AssertionError未关闭
难度 2/5 1-3 小时 新手友好度 74/100
-
难度 1/5 1 小时以内 新手友好度 72/100