The new release of spring-boot 3.1 added support for managing testcontainers when running your app in development: https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/reference/html/features.html#features.testing.testcontainers.at-development-time. I’m attempting to get it to work with the specific postgres version I’m using for my project, but I’m running into issues.
My first attempt:
public class TestDemoApplication {
public static void main(String[] args) {
SpringApplication.from(DemoApplication::main)
.with(MyContainersConfiguration.class)
.run(args);
}
@TestConfiguration
public static class MyContainersConfiguration {
@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>("postgresql:15-alpine3.17");
}
}
}
This gives me the error:
Caused by: java.lang.IllegalStateException: Failed to verify that image ‘postgresql:15-alpine3.17’ is a compatible substitute for ‘postgres’. This generally means that you are trying to use an image that Testcontainers has not been designed to use. If this is deliberate, and if you are confident that the image is compatible, you should declare compatibility in code using the
asCompatibleSubstituteFor
method. For example:
DockerImageName myImage = DockerImageName.parse("postgresql:15-alpine3.17").asCompatibleSubstituteFor("postgres");
and then usemyImage
instead.
So I try out that suggestion and change the container definition to:
@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(
DockerImageName.parse("postgresql:15-alpine3.17")
.asCompatibleSubstituteFor("postgres"));
}
That gives me the error:
Caused by: com.github.dockerjava.api.exception.NotFoundException: Status 404: {"message":"pull access denied for postgresql, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:241) ~[testcontainers-1.18.0.jar:1.18.0]
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269) ~[testcontainers-1.18.0.jar:1.18.0]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Any ideas about how to get this working?
2
Answers
Andrey's suggestion was correct. I used the same image name from my current configuration in application.yml. However, the image name needs to change for some reason.
This is the old configuration that was working:
To get the spring-boot java-style configuration working, this is the equivalent:
It's not clear to me why the image name is different, or where this is documented.
The right image is postgres:15-alpine3.17, which you can find in DockerHub. Regarding to the doubt about
jdbc:tc:postgresql:15-alpine3.17:///test?TC_TMPFS=/testtmpfs:rw
,postgresql
is the database type name in Testcontainers and it is not related to the image name. You can find some examples here. Also, the exception message was clear about the image to use 🙂