9
9
import com .github .dockerjava .api .model .Frame ;
10
10
import com .github .dockerjava .api .model .HostConfig ;
11
11
import com .github .dockerjava .api .model .Network ;
12
- import com .github .dockerjava .api .model .Ports ;
13
12
import com .github .dockerjava .api .model .Volume ;
14
13
import com .google .common .annotations .VisibleForTesting ;
15
14
import com .google .common .base .Throwables ;
21
20
import org .slf4j .Logger ;
22
21
import org .slf4j .LoggerFactory ;
23
22
import org .testcontainers .DockerClientFactory ;
23
+ import org .testcontainers .containers .ContainerState ;
24
24
25
25
import java .io .BufferedReader ;
26
26
import java .io .IOException ;
@@ -70,8 +70,23 @@ private ResourceReaper() {
70
70
dockerClient = DockerClientFactory .instance ().client ();
71
71
}
72
72
73
- @ SneakyThrows (InterruptedException .class )
73
+
74
+ /**
75
+ *
76
+ * @deprecated internal API
77
+ */
78
+ @ Deprecated
74
79
public static String start (String hostIpAddress , DockerClient client ) {
80
+ return start (client );
81
+ }
82
+
83
+ /**
84
+ *
85
+ * @deprecated internal API
86
+ */
87
+ @ Deprecated
88
+ @ SneakyThrows (InterruptedException .class )
89
+ public static String start (DockerClient client ) {
75
90
String ryukImage = ImageNameSubstitutor .instance ()
76
91
.apply (DockerImageName .parse ("testcontainers/ryuk:0.3.0" ))
77
92
.asCanonicalNameString ();
@@ -107,14 +122,22 @@ public void onNext(Frame frame) {
107
122
}
108
123
});
109
124
110
- InspectContainerResponse inspectedContainer = client . inspectContainerCmd ( ryukContainerId ). exec ();
125
+ ContainerState containerState = new ContainerState () {
111
126
112
- Integer ryukPort = inspectedContainer .getNetworkSettings ().getPorts ().getBindings ().values ().stream ()
113
- .flatMap (Stream ::of )
114
- .findFirst ()
115
- .map (Ports .Binding ::getHostPortSpec )
116
- .map (Integer ::parseInt )
117
- .get ();
127
+ final InspectContainerResponse inspectedContainer = client .inspectContainerCmd (ryukContainerId ).exec ();
128
+
129
+ @ Override
130
+ public List <Integer > getExposedPorts () {
131
+ return Stream .of (getContainerInfo ().getConfig ().getExposedPorts ())
132
+ .map (ExposedPort ::getPort )
133
+ .collect (Collectors .toList ());
134
+ }
135
+
136
+ @ Override
137
+ public InspectContainerResponse getContainerInfo () {
138
+ return inspectedContainer ;
139
+ }
140
+ };
118
141
119
142
CountDownLatch ryukScheduledLatch = new CountDownLatch (1 );
120
143
@@ -126,13 +149,15 @@ public void onNext(Frame frame) {
126
149
);
127
150
}
128
151
152
+ String host = containerState .getHost ();
153
+ Integer ryukPort = containerState .getFirstMappedPort ();
129
154
Thread kiraThread = new Thread (
130
155
DockerClientFactory .TESTCONTAINERS_THREAD_GROUP ,
131
156
() -> {
132
157
while (true ) {
133
158
RYUK_ACK_RATE_LIMITER .doWhenReady (() -> {
134
159
int index = 0 ;
135
- try (Socket clientSocket = new Socket (hostIpAddress , ryukPort )) {
160
+ try (Socket clientSocket = new Socket (host , ryukPort )) {
136
161
FilterRegistry registry = new FilterRegistry (clientSocket .getInputStream (), clientSocket .getOutputStream ());
137
162
138
163
synchronized (DEATH_NOTE ) {
@@ -157,7 +182,7 @@ public void onNext(Frame frame) {
157
182
}
158
183
}
159
184
} catch (IOException e ) {
160
- log .warn ("Can not connect to Ryuk at {}:{}" , hostIpAddress , ryukPort , e );
185
+ log .warn ("Can not connect to Ryuk at {}:{}" , host , ryukPort , e );
161
186
}
162
187
});
163
188
}
@@ -170,7 +195,7 @@ public void onNext(Frame frame) {
170
195
// We need to wait before we can start any containers to make sure that we delete them
171
196
if (!ryukScheduledLatch .await (TestcontainersConfiguration .getInstance ().getRyukTimeout (), TimeUnit .SECONDS )) {
172
197
log .error ("Timed out waiting for Ryuk container to start. Ryuk's logs:\n {}" , ryukLog );
173
- throw new IllegalStateException (String .format ("Could not connect to Ryuk at %s:%s" , hostIpAddress , ryukPort ));
198
+ throw new IllegalStateException (String .format ("Could not connect to Ryuk at %s:%s" , host , ryukPort ));
174
199
}
175
200
} finally {
176
201
try {
0 commit comments