Skip to content

Commit 3b24a7d

Browse files
eddumelendezkiview
andauthored
Accept image names with library/ prefix as a valid substitute (#6174)
Docker official images can be pulled with or without `library/` prefix as part of the image name. For example, `mysql` or `library/mysql`. This is explicit when using private repositories. Co-authored-by: Kevin Wittek <[email protected]>
1 parent 221a8da commit 3b24a7d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

core/src/main/java/org/testcontainers/utility/DockerImageName.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public final class DockerImageName {
2626

2727
private static final Pattern REPO_NAME = Pattern.compile(REPO_NAME_PART + "(/" + REPO_NAME_PART + ")*");
2828

29+
private static final String LIBRARY_PREFIX = "library/";
30+
2931
private final String rawName;
3032

3133
@With
@@ -231,8 +233,15 @@ public DockerImageName asCompatibleSubstituteFor(DockerImageName otherImageName)
231233
* @return whether this image has declared compatibility.
232234
*/
233235
public boolean isCompatibleWith(DockerImageName other) {
234-
// is this image already the same or equivalent?
235-
if (other.equals(this)) {
236+
// Make sure we always compare against a version of the image name containing the LIBRARY_PREFIX
237+
String finalImageName;
238+
if (this.repository.startsWith(LIBRARY_PREFIX)) {
239+
finalImageName = this.repository;
240+
} else {
241+
finalImageName = LIBRARY_PREFIX + this.repository;
242+
}
243+
DockerImageName imageWithLibraryPrefix = DockerImageName.parse(finalImageName);
244+
if (other.equals(this) || imageWithLibraryPrefix.equals(this)) {
236245
return true;
237246
}
238247

core/src/test/java/org/testcontainers/utility/DockerImageNameCompatibilityTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public void testAssertMethodAcceptsCompatible() {
9191
subject.assertCompatibleWith(DockerImageName.parse("bar"));
9292
}
9393

94+
@Test
95+
public void testAssertMethodAcceptsCompatibleLibraryPrefix() {
96+
DockerImageName subject = DockerImageName.parse("library/foo");
97+
subject.assertCompatibleWith(DockerImageName.parse("foo"));
98+
}
99+
94100
@Test
95101
public void testAssertMethodRejectsIncompatible() {
96102
DockerImageName subject = DockerImageName.parse("foo");

0 commit comments

Comments
 (0)