-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default prefixing image substitutor #3413
Conversation
Builds upon #3021 and #3411: * adds a pluggable image substitution mechanism using ServiceLoader, enabling users to perform custom substitution/auditing of images being used by their tests * provides a default implementation that behaves similarly to legacy `TestcontainersConfiguration` approach (`testcontainers.properties`) Notes: * behaviour is similar but not quite identical to `TestcontainersConfiguration`: use of a configured custom image for, e.g. Kafka/Pulsar that does not have a tag specified causes the substitution to take effect for all usages. It seems very unlikely that people would be using a mix of the config file image overrides in some places _and_ specific images specified in code in others. * Duplication of default image names in modules vs `TestcontainersConfiguration` class is intentional: specifying image overrides in `testcontainers.properties` should be removed in the future. * ~Add log deprecation warnings when `testcontainers.properties` image overrides are used.~ Defer to a future release?
Builds upon #3021 and #3411: * adds a pluggable image substitution mechanism using ServiceLoader, enabling users to perform custom substitution/auditing of images being used by their tests * provides a default implementation that behaves similarly to legacy `TestcontainersConfiguration` approach (`testcontainers.properties`) Notes: * behaviour is similar but not quite identical to `TestcontainersConfiguration`: use of a configured custom image for, e.g. Kafka/Pulsar that does not have a tag specified causes the substitution to take effect for all usages. It seems very unlikely that people would be using a mix of the config file image overrides in some places _and_ specific images specified in code in others. * Duplication of default image names in modules vs `TestcontainersConfiguration` class is intentional: specifying image overrides in `testcontainers.properties` should be removed in the future. * ~Add log deprecation warnings when `testcontainers.properties` image overrides are used.~ Defer to a future release?
For many orgs, sticking a prefix on the front of image names might be enough to use a private registry. I've added a default behaviour whereby, if a particular environment variable is present, image names are automatically substituted. e.g. `TESTCONTAINERS_IMAGE_NAME_PREFIX=my.registry.com/` would transform `redis` to `my.registry.com/redis` etc.
Docs update required
## Automatically modifying Docker Hub image names | ||
|
||
Testcontainers can be configured to modify Docker Hub image names on the fly to apply a prefix string. | ||
|
||
Consider this if: | ||
|
||
* Developers and CI machines need to use different image names. For example, developers are able to pull images from Docker Hub, but CI machines need to pull from a private registry | ||
* Your private registry has copies of images from Docker Hub where the names are predictable, and just adding a prefix is enough. | ||
For example, `registry.mycompany.com/mirror/mysql:8.0.22` can be derived from the original Docker Hub image name (`mysql:8.0.22`) 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](../examples/junit4/generic/src/test/java/generic/ImageNameSubstitutionTest.java) 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: | ||
|
||
* Setting environment variables `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/` | ||
* Via config file, setting `hub.image.name.prefix` in either: | ||
* the `~/.testcontainers.properties` file in your user home directory, or | ||
* a file named `testcontainers.properties` on the classpath | ||
|
||
Testcontainers will automatically apply the prefix to every image that it pulls from Docker Hub - please verify that all [the required images](./pull_rate_limiting.md#which-images-are-used-by-testcontainers) exist in your registry. | ||
|
||
Testcontainers will not apply the prefix to non-Hub image names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testcontainers will not apply the prefix to non-Hub image names.
Judgement call required on this specific point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the offender is Docker Hub with its rate limits, I would say let's focus on it first 👍
…tor' into default-prefixing-image-substitutor
Any chances this could be merged and released anytime soon ? |
core/src/main/java/org/testcontainers/utility/PrefixingImageNameSubstitutor.java
Outdated
Show resolved
Hide resolved
core/src/main/java/org/testcontainers/utility/PrefixingImageNameSubstitutor.java
Outdated
Show resolved
Hide resolved
core/src/main/java/org/testcontainers/utility/PrefixingImageNameSubstitutor.java
Outdated
Show resolved
Hide resolved
core/src/main/java/org/testcontainers/utility/PrefixingImageNameSubstitutor.java
Show resolved
Hide resolved
core/src/test/java/org/testcontainers/utility/PrefixingImageNameSubstitutorTest.java
Show resolved
Hide resolved
…meSubstitutor.java Co-authored-by: Sergei Egorov <[email protected]>
…t and -absent cases, the prefix is applied as-is)
Co-authored-by: Sergei Egorov <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Builds on #3411, #3021
Split out from #3021
For many orgs, sticking a prefix on the front of image names might be enough to use a private registry. I've added a default behaviour whereby, if a particular environment variable is present, image names are automatically substituted.