我有这个非常简单的代码来运行我的集成测试: (使用 Testcontainer 工件版本 1.16.2) 注意:在运行测试用例之前,我已经测试了test中的该端口是否可用。
@Testcontainers
public class SftpServerTestContainer {
private static final int PORT = 2222;
private static final String DOCKER_IMAGE_NAME = "atmoz/sftp:latest";
private static final String USER_PASS = "foo:pass:::folder";
@Container
private static final GenericContainer sftpContainer =
new GenericContainer<>(DockerImageName.parse(DOCKER_IMAGE_NAME));
@Bean
@Profile("test")
public GenericContainer sftpContainerAsABean(){
sftpContainer
.withAccessToHost(true)
.withExposedPorts(PORT)
.withCommand(USER_PASS);
sftpContainer.start();
return sftpContainer;
}
}
它有一个非常简单的具有给定名称的 GenericContainer
启动过程。但我永远无法启动它并最终导致以下失败:
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "Content-Length: 26[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "Host: localhost:2375[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "Connection: keep-alive[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "User-Agent: Apache-HttpClient/5.0.3 (Java/17)[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 >> "{"Detach":null,"Tty":null}"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "HTTP/1.1 200 OK[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "Content-Type: application/vnd.docker.raw-stream[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "Api-Version"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << ": 1.41[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "Docker-Experimental: false[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "Ostype: linux[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "Server: Docker/20.10.13 (linux)[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] wire - http-outgoing-4 << "[\r][\n]"
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << HTTP/1.1 200 OK
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << Content-Type: application/vnd.docker.raw-stream
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << Api-Version: 1.41
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << Docker-Experimental: false
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << Ostype: linux
[DEBUG] 2022-03-25 09:06:43.078 [docker-java-stream-164976604] headers - http-outgoing-4 << Server: Docker/20.10.13 (linux)
[DEBUG] 2022-03-25 09:06:43.137 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.138 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.138 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.242 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.243 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.243 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.346 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.347 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.347 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.451 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.452 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.452 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.555 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.556 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.556 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.665 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.668 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.668 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.778 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0][0x1a]/bin/sh: 1: nc: not found[\n]"
[DEBUG] 2022-03-25 09:06:43.780 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]x/bin/bash: connect: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.780 [docker-java-stream-164976604] wire - http-outgoing-4 << "/bin/bash: /dev/tcp/localhost/2222: Cannot assign requested address[\n]"
[DEBUG] 2022-03-25 09:06:43.890 [docker-java-stream-164976604] wire - http-outgoing-4 << "[0x2][0x0][0
出于某种原因,我认为 TestContainers
的 docker-java
lib 正在使用 nc
来执行一些网络操作。但如果是因为这个,我很快将图像更改为 atmoz/sftp:alpine
并且收到以下错误(类似但不一样):
[DEBUG] 2022-03-25 09:11:36.145 [docker-java-stream--355075128] wire - http-outgoing-4 << "/bin/bash: line 1: /dev/tcp/localhost/2222: Address not available[\n]"
[DEBUG] 2022-03-25 09:11:36.250 [docker-java-stream--355075128] wire - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x0]l/bin/bash: connect: Address not available[\n]"
[DEBUG] 2022-03-25 09:11:36.250 [docker-java-stream--355075128] wire - http-outgoing-4 << "/bin/bash: line 1: /dev/tcp/localhost/2222: Address not available[\n]"
所以我不明白这种情况下Testcontainers的底层机制。
顺便说一下,我还在 keycloak 图像上运行了相同的集成测试,发现我也总是出现 nc not found
错误。但我认为这是因为 Keycloak 基础镜像出于安全目的对镜像进行了强化。
最佳答案
Testcontainers 从容器内运行几个命令来确定容器的内部端口何时可用: https://github.com/testcontainers/testcontainers-java/blob/5e6dbc791b6184f0de6d1f9d184a78324c1ec945/core/src/main/java/org/testcontainers/containers/wait/internal/InternalCommandPortListeningCheck.java#L29-L38
这是默认HostPortWaitStrategy
的一部分。可能的情况是,atmoz/sftp:latest
不允许运行任何这些命令。
此外,您的日志级别为DEBUG
,您不应该额外关注这些消息。您收到的实际错误是什么?
关于java - 具有 sftp 图像的测试容器在 "nc not found"或 "Address Not Available"上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71613887/