File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed
main/java/org/testcontainers/images/builder
test/java/org/testcontainers/images/builder Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -87,9 +87,6 @@ protected final String resolve() {
8787 DockerClient dockerClient = DockerClientFactory .instance ().client ();
8888
8989 try {
90- if (deleteOnExit ) {
91- ResourceReaper .instance ().registerImageForCleanup (dockerImageName );
92- }
9390
9491 BuildImageResultCallback resultCallback = new BuildImageResultCallback () {
9592 @ Override
@@ -114,9 +111,12 @@ public void onNext(BuildResponseItem item) {
114111 if (buildImageCmd .getLabels () != null ) {
115112 labels .putAll (buildImageCmd .getLabels ());
116113 }
114+
117115 labels .putAll (DockerClientFactory .DEFAULT_LABELS );
118- //noinspection deprecation
119- labels .putAll (ResourceReaper .instance ().getLabels ());
116+ if (deleteOnExit ) {
117+ //noinspection deprecation
118+ labels .putAll (ResourceReaper .instance ().getLabels ());
119+ }
120120 buildImageCmd .withLabels (labels );
121121
122122 prePullDependencyImages (dependencyImageNames );
Original file line number Diff line number Diff line change 44import com .github .dockerjava .api .command .InspectImageResponse ;
55import org .junit .Test ;
66import org .testcontainers .DockerClientFactory ;
7+ import org .testcontainers .utility .Base58 ;
78
89import static org .assertj .core .api .Assertions .assertThat ;
910
@@ -24,4 +25,22 @@ public void shouldAddDefaultLabels() {
2425 .containsAllEntriesOf (DockerClientFactory .DEFAULT_LABELS );
2526 }
2627
28+ @ Test
29+ public void shouldNotAddSessionLabelIfDeleteOnExitIsFalse () {
30+ ImageFromDockerfile image = new ImageFromDockerfile ("localhost/testcontainers/" + Base58 .randomString (16 ).toLowerCase (), false )
31+ .withDockerfileFromBuilder (it -> it .from ("scratch" ));
32+ String imageId = image .resolve ();
33+ DockerClient dockerClient = DockerClientFactory .instance ().client ();
34+
35+ try {
36+ InspectImageResponse inspectImageResponse = dockerClient .inspectImageCmd (imageId ).exec ();
37+ assertThat (inspectImageResponse .getConfig ().getLabels ())
38+ .doesNotContainKey (DockerClientFactory .TESTCONTAINERS_SESSION_ID_LABEL );
39+ } finally {
40+ // ensure the image is deleted, even if the test fails
41+ dockerClient .removeImageCmd (imageId ).exec ();
42+ }
43+
44+ }
45+
2746}
You can’t perform that action at this time.
0 commit comments