3131import org .testcontainers .utility .ImageNameSubstitutor ;
3232import org .testcontainers .utility .MountableFile ;
3333import org .testcontainers .utility .ResourceReaper ;
34+ import org .testcontainers .utility .RyukResourceReaper ;
3435import org .testcontainers .utility .TestcontainersConfiguration ;
3536
3637import java .io .ByteArrayOutputStream ;
3738import java .io .IOException ;
3839import java .io .InputStream ;
3940import java .net .URI ;
4041import java .util .ArrayList ;
42+ import java .util .HashMap ;
4143import java .util .List ;
4244import java .util .Map ;
4345import java .util .Optional ;
@@ -61,8 +63,7 @@ public class DockerClientFactory {
6163 public static final String SESSION_ID = UUID .randomUUID ().toString ();
6264
6365 public static final Map <String , String > DEFAULT_LABELS = ImmutableMap .of (
64- TESTCONTAINERS_LABEL , "true" ,
65- TESTCONTAINERS_SESSION_ID_LABEL , SESSION_ID
66+ TESTCONTAINERS_LABEL , "true"
6667 );
6768
6869 private static final DockerImageName TINY_IMAGE = DockerImageName .parse ("alpine:3.14" );
@@ -73,7 +74,7 @@ public class DockerClientFactory {
7374 DockerClientProviderStrategy strategy ;
7475
7576 @ VisibleForTesting
76- DockerClient dockerClient ;
77+ DockerClient client ;
7778
7879 @ VisibleForTesting
7980 RuntimeException cachedClientFailure ;
@@ -174,21 +175,20 @@ public String getRemoteDockerUnixSocketPath() {
174175 */
175176 @ Synchronized
176177 public DockerClient client () {
177-
178- if (dockerClient != null ) {
179- return dockerClient ;
180- }
181-
182178 // fail-fast if checks have failed previously
183179 if (cachedClientFailure != null ) {
184180 log .debug ("There is a cached checks failure - throwing" , cachedClientFailure );
185181 throw cachedClientFailure ;
186182 }
187183
184+ if (client != null ) {
185+ return client ;
186+ }
187+
188188 final DockerClientProviderStrategy strategy = getOrInitializeStrategy ();
189189
190190 log .info ("Docker host IP address is {}" , strategy .getDockerHostIpAddress ());
191- final DockerClient client = new DockerClientDelegate () {
191+ client = new DockerClientDelegate () {
192192
193193 @ Getter
194194 final DockerClient dockerClient = strategy .getDockerClient ();
@@ -209,25 +209,14 @@ public void close() {
209209 " Operating System: " + dockerInfo .getOperatingSystem () + "\n " +
210210 " Total Memory: " + dockerInfo .getMemTotal () / (1024 * 1024 ) + " MB" );
211211
212- final String ryukContainerId ;
213-
214- boolean useRyuk = !Boolean .parseBoolean (System .getenv ("TESTCONTAINERS_RYUK_DISABLED" ));
215- if (useRyuk ) {
216- log .debug ("Ryuk is enabled" );
217- try {
218- //noinspection deprecation
219- ryukContainerId = ResourceReaper .start (client );
220- } catch (RuntimeException e ) {
221- cachedClientFailure = e ;
222- throw e ;
223- }
224- log .info ("Ryuk started - will monitor and terminate Testcontainers containers on JVM exit" );
225- } else {
226- log .debug ("Ryuk is disabled" );
227- ryukContainerId = null ;
228- // best-efforts cleanup at JVM shutdown, without using the Ryuk container
212+ final ResourceReaper resourceReaper ;
213+ try {
214+ resourceReaper = ResourceReaper .instance ();
229215 //noinspection deprecation
230- ResourceReaper .instance ().setHook ();
216+ resourceReaper .init ();
217+ } catch (RuntimeException e ) {
218+ cachedClientFailure = e ;
219+ throw e ;
231220 }
232221
233222 boolean checksEnabled = !TestcontainersConfiguration .getInstance ().isDisableChecks ();
@@ -237,6 +226,12 @@ public void close() {
237226 try {
238227 log .info ("Checking the system..." );
239228 checkDockerVersion (version .getVersion ());
229+
230+ //noinspection deprecation
231+ String ryukContainerId = resourceReaper instanceof RyukResourceReaper
232+ ? ((RyukResourceReaper ) resourceReaper ).getContainerId ()
233+ : null ;
234+
240235 if (ryukContainerId != null ) {
241236 checkDiskSpace (client , ryukContainerId );
242237 } else {
@@ -261,8 +256,7 @@ public void close() {
261256 log .debug ("Checks are disabled" );
262257 }
263258
264- dockerClient = client ;
265- return dockerClient ;
259+ return client ;
266260 }
267261
268262 private void checkDockerVersion (String dockerVersion ) {
@@ -378,8 +372,10 @@ private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd>
378372 final String tinyImage = ImageNameSubstitutor .instance ().apply (TINY_IMAGE ).asCanonicalNameString ();
379373
380374 checkAndPullImage (client , tinyImage );
375+ HashMap <String , String > labels = new HashMap <>(DEFAULT_LABELS );
376+ labels .putAll (ResourceReaper .instance ().getLabels ());
381377 CreateContainerCmd createContainerCmd = client .createContainerCmd (tinyImage )
382- .withLabels (DEFAULT_LABELS );
378+ .withLabels (labels );
383379 createContainerCmdConsumer .accept (createContainerCmd );
384380 String id = createContainerCmd .exec ().getId ();
385381
0 commit comments