Skip to content
Go back

TestContainers固定容器端口

Edit page

TestContainers固定容器端口

这篇文章的内容发生的背景和测试javaldapfailover有关.
测试failover需要启动两个ldap的容器并stop其中一个.
但是stop完之后遇到了一个问题当下次启动后分配的端口会变化(这是由于docker的内部机制导致的).
接下来将会介绍如何解决这个问题.

解决方案

启动容器时增加fixedExposedPort

val container = FixedHostPortGenericContainer<*>("<your-docker-image>")
container.withExposedPorts(389)
container.withFixedExposedPort(
	TestSocketUtils.findAvailableTcpPort(),
	389
) // we need to fix the port to make sure the port is the same when the container is restarted

注意事项

需要注意以下几点:

参考资料


Edit page
Share this post on:

Previous Post
transport endpoint is not connected问题解决
Next Post
通过增加timeout-control以解决当tcp连接一直处于SYN_SENT状态导致java中的ldap-client的failover不工作的问题