我尝试dockerize我的java spring应用程序。
在my build.gradle
implementation 'org.testcontainers:junit-jupiter'
implementation 'org.testcontainers:postgresql'
在我的测试文件中:
@SpringBootTest
class ApplicationTests {
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa");
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}
Dockerfile:
FROM adoptopenjdk/openjdk16:jdk-16.0.2_7-alpine-slim
MAINTAINER myapplication
ARG JAR_FILE=build/libs/myapplication-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} myapplication-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/myapplication-0.0.1-SNAPSHOT.jar"]
然后从终端I运行:
docker build --no-cache --tag=myapplication:latest .
docker run -v /var/run/docker.sock:/var/run/docker.sock -p8080:8080 myapplication:latest
毕竟我看到了一个错误:
2010-10-24 09:12:25.781 WARN[,,] 1—[container -ryuk]testcontainers.utility. resourcereaper:无法连接到Ryuk172.17.0.1:50582
java.net.ConnectException: Connection refused atjava.base/sun.nio.ch.Net。pollConnect(本机方法)~[na:na] atjava.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na] atjava.base/sun.nio.ch.NioSocketImpl.timedFinishConnect (NioSocketImpl.java: 542)~ (na na):java.base/sun.nio.ch.NioSocketImpl.connect (NioSocketImpl.java: 597)~ (na na):java.base/java.net.SocksSocketImpl.connect (SocksSocketImpl.java: 333)~[na:na]在java.base/java.net.Socket.connect(Socket.java:645)~ (na na):org.testcontainers.utility.ResourceReaper.lambda零1美元(ResourceReaper.java: 189)~ (testcontainers-1.16.0.jar !/na):org.rnorth.ducttape.ratelimits.RateLimiter.doWhenReady (RateLimiter.java: 27)~ (duct-tape-1.0.8.jar !/na):org.testcontainers.utility.ResourceReaper.lambda开始2美元(ResourceReaper.java: 185)~ (testcontainers-1.16.0.jar !/na):java.base/java.lang.Thread.run (Thread.java: 831) ~ (na na):
我做错了什么?不幸的是,经过两天的努力,我已经没有主意了。
btw:我禁用了使用gRPC FUSE从Docker共享文件,没有帮助。我用macos。
在运行命令后添加-e TESTCONTAINERS_HOST_OVERRIDE=docker.for.mac.host.internal
docker run -e TESTCONTAINERS_HOST_OVERRIDE=docker.for.mac.host.internal ...
根据Testcontainers文档,WORKDIR
应该具有与OS启动点相同的绝对路径。尝试添加这些配置
docker run -v /var/run/docker.sock:/var/run/docker.sock
-p 8080:8080
-v $PWD:$PWD
-w $PWD
myapplication:latest