Reputation: 29
I'm trying to implement a JUnit (v4.13.2) test using testcontainers (v1.16.2) to better test our DAO/JPA classes. I'm using the centos/postgresql-96-centos7 official docker image and I can run it on the cmd line just fine using: docker run -d --name postgres -e POSTGRESQL_USER=user -e POSTGRESQL_PASSWORD=pass -e POSTGRES_DATABASE=db -p 5432:5432 centos/postgresql-96-centos7
. I removed the container prior to running the unit test to avoid any conflicts in my docker environment. However, when I run my test, I consistently get the following errors:
java.lang.ExceptionInInitializerError
Caused by: org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: java.lang.IllegalStateException: Container exited with code 2
I have tried increasing the timeout value to 5 mins and simplified to the JUnit4 testing pattern (to avoid running a mixed testing environment) to no avail. I'm only including the code for the abstract class because it is throwing the exception before calling POSTGRESQL_CONTAINER.start()
.
public abstract class AbstractRdbmsTest {
private static final String DOCKER_IMAGE_NAME = "centos/postgresql-96-centos7";
private static final String POSTGRESQL_USER = "user";
private static final String POSTGRESQL_PASSWORD = "pass";
private static final String POSTGRESQL_DATABASE = "db";
protected static final PostgreSQLContainer POSTGRESQL_CONTAINER;
static {
DockerImageName imgName = DockerImageName.parse(DOCKER_IMAGE_NAME).asCompatibleSubstituteFor("postgres");
POSTGRESQL_CONTAINER = (PostgreSQLContainer) new PostgreSQLContainer(imgName)
.withDatabaseName(POSTGRESQL_DATABASE)
.withUsername(POSTGRESQL_USER)
.withPassword(POSTGRESQL_PASSWORD)
.withExposedPorts(PostgreSQLContainer.POSTGRESQL_PORT);
POSTGRESQL_CONTAINER.start();
}
}
EDIT: Local Docker Env: Client/Server: Docker Engine - Community v20.10.7
Upvotes: 1
Views: 16593
Reputation: 29
So as stated above, what appears to have gotten around the problem is the following:
public abstract class AbstractRdbmsTest {
private static final String DOCKER_IMAGE_NAME = "centos/postgresql-96-centos7";
private static final String POSTGRESQL_USER = "user";
private static final String POSTGRESQL_PASSWORD = "pass";
private static final String POSTGRESQL_DATABASE = "db";
protected static final PostgreSQLContainer CONTAINER;
static {
DockerImageName imgName = DockerImageName.parse(DOCKER_IMAGE_NAME).asCompatibleSubstituteFor("postgres");
CONTAINER = (PostgreSQLContainer) new PostgreSQLContainer(imgName)
.withDatabaseName(POSTGRESQL_DATABASE)
.withUsername(POSTGRESQL_USER)
.withPassword(POSTGRESQL_PASSWORD)
.withEnv("POSTGRESQL_DATABASE", POSTGRESQL_DATABASE)
.withEnv("POSTGRESQL_USER", POSTGRESQL_USER)
.withEnv("POSTGRESQL_PASSWORD", POSTGRESQL_PASSWORD)
.withExposedPorts(PostgreSQLContainer.POSTGRESQL_PORT);
CONTAINER.setWaitStrategy(Wait.defaultWaitStrategy()
.withStartupTimeout(Duration.of(60, SECONDS)));
CONTAINER.start();
}
}
Added the correct environment variables and changed the wait strategy. As a note: Apparently, the image I'm using is more non-standard than I realized. The official container may not need the change to the wait strategy.
Upvotes: 1