【发布时间】:2020-01-31 19:16:43
【问题描述】:
我使用 javax.mail 实现了一个 POP3 服务器和客户端,只是为了尝试使用 Docker 进行集成测试。所以我基于 openjdk:8-jre 镜像创建了两个 Docker 镜像,并将我的 jar 复制到它们并启动它。根据我的配置(见下文),它正在工作。他们正在互相交谈。
但是由于想要进行多个集成测试,为每个测试构建一个映像并启动它们将会很乏味。我也不知道如何自动化结果。 但后来我偶然发现了 TestContainers,这似乎对实现这些测试很有帮助。
所以我开始使用我的 POP3 服务器映像作为 GenericContainer 将这些测试移植到 TestContainers,并在 JUnit 测试方法中启动我的 POP3 客户端类。我暴露了我的 POP3 服务器正在监听的端口 24999。但是当我尝试连接到服务器时,出现以下错误:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 32782; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused
...
TestContainers 中可能缺少一些设置。你能帮帮我吗?
这是我正在使用的代码:
public class DockerPop3AutocryptKeyProvidingAndReceivingTest {
@Test
public void test() throws InterruptedException {
GenericContainer container = new GenericContainer<>("immerfroehlich/emailfilter:latest")
.withExposedPorts(24999);
container.start();
String host = container.getContainerIpAddress();
String port = container.getFirstMappedPort().toString();
//The following is simplified, but copied from the working jar used in the Docker Client image/container
MyPOP3Client client = new MyPOP3Client(host, port);
client.connect();
container.stop();
}
}
这就是我创建 Docker 映像的方式:
FROM openjdk:8-jre
ADD build/distributions/MyPOP3Server.tar . #This is where I have packeded all the needed files to. It gets unpackeded by Docker.
#EXPOSE 24999 #I tried both with and without this expose
WORKDIR /MyPOP3Server/bin
ENTRYPOINT ["sh","MyPOP3Server"] #Executes the shell script wich runs java with my jar
这是在服务器 Jar 中运行的代码的简化版本:
MyPOP3Server server = new MyPOP3Server();
server.listenToPort(24999);
请告诉我我错过了什么。这里有什么问题?
感谢和亲切的问候。
【问题讨论】:
-
能否在容器启动后下断点并检查它以查看映射的端口。无论如何,如果您执行类似 port = container.getMappedPort(24999) 的操作
-
好的,我改成 port = container.getMappedPort(24999) 但它还是不行。我做了调试,但我不知道在哪里看。 container.exposedPorts 是 [24999]; container.portBindings 是 []。
标签: java docker junit dockerfile testcontainers