docs/features/image_name_substitution.md
Testcontainers supports automatic substitution of Docker image names.
This allows replacement of an image name specified in test code with an alternative name - for example, to replace the name of a Docker Hub image dependency with an alternative hosted on a private image registry.
This is advisable to avoid Docker Hub rate limiting, and some companies will prefer this for policy reasons.
This page describes four approaches for image name substitution:
It is assumed that you have already set up a private registry hosting all the Docker images your build requires.
Consider this if:
This approach simply entails modifying test code manually, e.g. changing:
For example, you may have a test that uses the mysql container image from Docker Hub:
Direct Docker Hub image name inside_block:directDockerHubReference
<!--/codeinclude-->to:
<!--codeinclude-->Private registry image name inside_block:hardcodedMirror
<!--/codeinclude-->Testcontainers can be configured to modify Docker Hub image names on the fly to apply a prefix string.
Consider this if:
registry.mycompany.com/mirror/mysql:8.0.36 can be derived from the original Docker Hub image name (mysql:8.0.36) with a consistent prefix string: registry.mycompany.com/mirror/In this case, image name references in code are unchanged. i.e. you would leave as-is:
<!--codeinclude-->Unchanged direct Docker Hub image name inside_block:directDockerHubReference
<!--/codeinclude-->You can then configure Testcontainers to apply the prefix registry.mycompany.com/mirror/ to every image that it tries to pull from Docker Hub.
This can be done in one of two ways:
TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/hub.image.name.prefix in either:
~/.testcontainers.properties file in your user home directory, ortestcontainers.properties on the classpathTestcontainers will automatically apply the prefix to every image that it pulls from Docker Hub - please verify that all the required images exist in your registry.
Testcontainers will not apply the prefix to:
docker.io or registry.hub.docker.com host part)Consider this if:
In this case, image name references in code are unchanged. i.e. you would leave as-is:
<!--codeinclude-->Unchanged direct Docker Hub image name inside_block:directDockerHubReference
<!--/codeinclude-->You can implement a custom image name substitutor by:
org.testcontainers.utility.ImageNameSubstitutorThe following is an example image substitutor implementation:
<!--codeinclude-->Example Image Substitutor block:ExampleImageNameSubstitutor
<!--/codeinclude-->Testcontainers can be configured to find it at runtime via configuration.
To do this, create or modify a file on the classpath named testcontainers.properties.
For example:
=== "src/test/resources/testcontainers.properties"
text image.substitutor=com.mycompany.testcontainers.ExampleImageNameSubstitutor
Note that it is also possible to provide this same configuration property:
testcontainers.properties file at the root of a library JAR file (useful if you wish to distribute a drop-in image substitutor JAR within an organization)~/.testcontainers.properties; note the leading .)TESTCONTAINERS_IMAGE_SUBSTITUTOR=com.mycompany.testcontainers.ExampleImageNameSubstitutor).Please see the documentation on configuration mechanisms for more information.
Also, you can use the ServiceLoader mechanism to provide the fully qualified class name of the ImageNameSubstitutor implementation:
=== "src/test/resources/META-INF/services/org.testcontainers.utility.ImageNameSubstitutor"
text com.mycompany.testcontainers.ExampleImageNameSubstitutor
!!! note This approach is discouraged and deprecated, but is documented for completeness. Please consider one of the other approaches outlined in this page instead. Overriding individual image names via configuration may be removed in the future.
Consider this if:
In this case, image name references in code are left unchanged. i.e. you would leave as-is:
<!--codeinclude-->Unchanged direct Docker Hub image name inside_block:directDockerHubReference
<!--/codeinclude-->You can force Testcontainers to substitute in a different image using a configuration file, which allows some (but not all) container names to be substituted.