Unable to Run Container Using testcontainers

Unable to Run Container Using testcontainers

Questions : Unable to Run Container Using testcontainers

595

I’m trying to implement a JUnit (v4.13.2) in4codes_junit4 test using testcontainers (v1.16.2) to in4codes_junit4 better test our DAO/JPA classes. I’m using in4codes_junit4 the centos/postgresql-96-centos7 official in4codes_junit4 docker image and I can run it on the cmd in4codes_junit4 line just fine using: docker run -d –name in4codes_junit4 postgres -e POSTGRESQL_USER=user -e in4codes_junit4 POSTGRESQL_PASSWORD=pass -e in4codes_junit4 POSTGRES_DATABASE=db -p 5432:5432 in4codes_junit4 centos/postgresql-96-centos7. I removed the in4codes_junit4 container prior to running the unit test to in4codes_junit4 avoid any conflicts in my docker in4codes_junit4 environment. However, when I run my test, I in4codes_junit4 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 in4codes_junit4 5 mins and simplified to the JUnit4 testing in4codes_junit4 pattern (to avoid running a mixed testing in4codes_junit4 environment) to no avail. I’m only including in4codes_junit4 the code for the abstract class because it in4codes_junit4 is throwing the exception before calling in4codes_junit4 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: in4codes_junit4 Docker Engine – Community v20.10.7

Total Answers 1
30

Answers 1 : of Unable to Run Container Using testcontainers

So as stated above, what appears to have in4codes_junit4 gotten around the problem is the in4codes_junit4 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 in4codes_junit4 and changed the wait strategy. As a in4codes_junit4 note: Apparently, the image I’m using is in4codes_junit4 more non-standard than I realized. The in4codes_junit4 official container may not need the in4codes_junit4 change to the wait strategy.

0