无法 运行 容器使用测试容器
Unable to Run Container Using testcontainers
我正在尝试使用 testcontainers (v1.16.2) 实施 JUnit (v4.13.2) 测试,以更好地测试我们的 DAO/JPA classes。我正在使用 centos/postgresql-96-centos7 官方 docker 图像,我可以 运行 在 cmd 行上使用 docker run -d --name postgres -e POSTGRESQL_USER=user -e POSTGRESQL_PASSWORD=pass -e POSTGRES_DATABASE=db -p 5432:5432 centos/postgresql-96-centos7
就可以了。我在 运行 进行单元测试之前删除了容器,以避免在我的 docker 环境中发生任何冲突。然而,当我 运行 我的测试时,我总是得到以下错误:
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
我曾尝试将超时值增加到 5 分钟并简化为 JUnit4 测试模式(以避免 运行混合测试环境)但无济于事。我只包含抽象 class 的代码,因为它在调用 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();
}
}
编辑:本地 Docker 环境:
Client/Server: Docker 引擎 - 社区 v20.10.7
因此,如上所述,似乎已经解决了以下问题:
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();
}
}
添加了正确的环境变量并更改了等待策略。请注意:显然,我使用的图像比我意识到的更不标准。官方容器可能不需要更改等待策略。
我正在尝试使用 testcontainers (v1.16.2) 实施 JUnit (v4.13.2) 测试,以更好地测试我们的 DAO/JPA classes。我正在使用 centos/postgresql-96-centos7 官方 docker 图像,我可以 运行 在 cmd 行上使用 docker run -d --name postgres -e POSTGRESQL_USER=user -e POSTGRESQL_PASSWORD=pass -e POSTGRES_DATABASE=db -p 5432:5432 centos/postgresql-96-centos7
就可以了。我在 运行 进行单元测试之前删除了容器,以避免在我的 docker 环境中发生任何冲突。然而,当我 运行 我的测试时,我总是得到以下错误:
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
我曾尝试将超时值增加到 5 分钟并简化为 JUnit4 测试模式(以避免 运行混合测试环境)但无济于事。我只包含抽象 class 的代码,因为它在调用 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();
}
}
编辑:本地 Docker 环境: Client/Server: Docker 引擎 - 社区 v20.10.7
因此,如上所述,似乎已经解决了以下问题:
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();
}
}
添加了正确的环境变量并更改了等待策略。请注意:显然,我使用的图像比我意识到的更不标准。官方容器可能不需要更改等待策略。