testcontainers-java :: index

Creating a container

Creating a generic container based on an image

Testcontainers' generic container support offers the most flexibility, and makes it easy to use virtually any container images as temporary test dependencies. For example, if you might use it to test interactions with:

With a generic container, you set the container image using a parameter to the rule constructor, e.g.:

new GenericContainer(DockerImageName.parse("jboss/wildfly:9.0.1.Final"))

Specifying an image

Many Container classes in Testcontainers have historically supported:

Since v1.15.0, both of these constructor types have been deprecated, for the reasons given above.

Instead, it is highly recommended that all containers be constructed using a constructor that accepts a DockerImageName object. The DockerImageName class is an unambiguous reference to a docker image.

It is suggested that developers treat DockerImageNames as you would any other potentially-constant value - consider defining a constant in your test codebase that matches the production version of the dependency you are using.


A generic container rule can be used with any public docker image; for example:

Creating a Redis container (JUnit 4) inside_block:simple

Further options may be specified:

Creating a container with more options (JUnit 4) inside_block:withOptions

These containers, as @ClassRules, will be started before any tests in the class run, and will be destroyed after all tests have run.