25
25
import org .openqa .selenium .TimeoutException ;
26
26
import org .openqa .selenium .WebDriverException ;
27
27
import org .openqa .selenium .docker .Container ;
28
+ import org .openqa .selenium .docker .ContainerConfig ;
28
29
import org .openqa .selenium .docker .ContainerInfo ;
29
30
import org .openqa .selenium .docker .Docker ;
30
31
import org .openqa .selenium .docker .Image ;
@@ -94,6 +95,7 @@ public class DockerSessionFactory implements SessionFactory {
94
95
private final Image videoImage ;
95
96
private final DockerAssetsPath assetsPath ;
96
97
private final String networkName ;
98
+ private final boolean runningInDocker ;
97
99
98
100
public DockerSessionFactory (
99
101
Tracer tracer ,
@@ -104,7 +106,8 @@ public DockerSessionFactory(
104
106
Capabilities stereotype ,
105
107
Image videoImage ,
106
108
DockerAssetsPath assetsPath ,
107
- String networkName ) {
109
+ String networkName ,
110
+ boolean runningInDocker ) {
108
111
this .tracer = Require .nonNull ("Tracer" , tracer );
109
112
this .clientFactory = Require .nonNull ("HTTP client" , clientFactory );
110
113
this .docker = Require .nonNull ("Docker command" , docker );
@@ -115,6 +118,7 @@ public DockerSessionFactory(
115
118
Require .nonNull ("Stereotype" , stereotype ));
116
119
this .videoImage = videoImage ;
117
120
this .assetsPath = assetsPath ;
121
+ this .runningInDocker = runningInDocker ;
118
122
}
119
123
120
124
@ Override
@@ -130,23 +134,27 @@ public boolean test(Capabilities capabilities) {
130
134
@ Override
131
135
public Either <WebDriverException , ActiveSession > apply (CreateSessionRequest sessionRequest ) {
132
136
LOG .info ("Starting session for " + sessionRequest .getCapabilities ());
133
- int port = PortProber .findFreePort ();
134
- URL remoteAddress = getUrl (port );
135
137
136
- HttpClient client = clientFactory . createClient ( remoteAddress );
138
+ int port = runningInDocker ? 4444 : PortProber . findFreePort ( );
137
139
try (Span span = tracer .getCurrentContext ().createSpan ("docker_session_factory.apply" )) {
138
140
Map <String , EventAttributeValue > attributeMap = new HashMap <>();
139
141
attributeMap .put (AttributeKey .LOGGER_CLASS .getKey (),
140
142
EventAttribute .setValue (this .getClass ().getName ()));
141
- LOG .info ("Creating container, mapping container port 4444 to " + port );
143
+ String logMessage = runningInDocker ? "Creating container..." :
144
+ "Creating container, mapping container port 4444 to " + port ;
145
+ LOG .info (logMessage );
142
146
Container container = createBrowserContainer (port , sessionRequest .getCapabilities ());
143
147
container .start ();
144
148
ContainerInfo containerInfo = container .inspect ();
145
149
150
+ String containerIp = containerInfo .getIp ();
151
+ URL remoteAddress = getUrl (port , containerIp );
152
+ HttpClient client = clientFactory .createClient (remoteAddress );
153
+
146
154
attributeMap .put ("docker.browser.image" , EventAttribute .setValue (browserImage .toString ()));
147
155
attributeMap .put ("container.port" , EventAttribute .setValue (port ));
148
156
attributeMap .put ("container.id" , EventAttribute .setValue (container .getId ().toString ()));
149
- attributeMap .put ("container.ip" , EventAttribute .setValue (containerInfo . getIp () ));
157
+ attributeMap .put ("container.ip" , EventAttribute .setValue (containerIp ));
150
158
attributeMap .put ("docker.server.url" , EventAttribute .setValue (remoteAddress .toString ()));
151
159
152
160
LOG .info (
@@ -212,7 +220,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
212
220
String containerPath = path .get ().getContainerPath (id );
213
221
saveSessionCapabilities (mergedCapabilities , containerPath );
214
222
String hostPath = path .get ().getHostPath (id );
215
- videoContainer = startVideoContainer (mergedCapabilities , containerInfo . getIp () , hostPath );
223
+ videoContainer = startVideoContainer (mergedCapabilities , containerIp , hostPath );
216
224
}
217
225
218
226
Dialect downstream = sessionRequest .getDownstreamDialects ().contains (result .getDialect ()) ?
@@ -249,11 +257,14 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
249
257
private Container createBrowserContainer (int port , Capabilities sessionCapabilities ) {
250
258
Map <String , String > browserContainerEnvVars = getBrowserContainerEnvVars (sessionCapabilities );
251
259
Map <String , String > devShmMount = Collections .singletonMap ("/dev/shm" , "/dev/shm" );
252
- return docker .create (image (browserImage )
253
- .env (browserContainerEnvVars )
254
- .bind (devShmMount )
255
- .map (Port .tcp (4444 ), Port .tcp (port ))
256
- .network (networkName ));
260
+ ContainerConfig containerConfig = image (browserImage )
261
+ .env (browserContainerEnvVars )
262
+ .bind (devShmMount )
263
+ .network (networkName );
264
+ if (!runningInDocker ) {
265
+ containerConfig = containerConfig .map (Port .tcp (4444 ), Port .tcp (port ));
266
+ }
267
+ return docker .create (containerConfig );
257
268
}
258
269
259
270
private Map <String , String > getBrowserContainerEnvVars (Capabilities sessionRequestCapabilities ) {
@@ -365,11 +376,15 @@ private void waitForServerToStart(HttpClient client, Duration duration) {
365
376
});
366
377
}
367
378
368
- private URL getUrl (int port ) {
379
+ private URL getUrl (int port , String containerIp ) {
369
380
try {
370
381
String host = "localhost" ;
371
- if (dockerUri .getScheme ().startsWith ("tcp" ) || dockerUri .getScheme ().startsWith ("http" )) {
372
- host = dockerUri .getHost ();
382
+ if (runningInDocker ) {
383
+ host = containerIp ;
384
+ } else {
385
+ if (dockerUri .getScheme ().startsWith ("tcp" ) || dockerUri .getScheme ().startsWith ("http" )) {
386
+ host = dockerUri .getHost ();
387
+ }
373
388
}
374
389
return new URL (String .format ("http://%s:%s/wd/hub" , host , port ));
375
390
} catch (MalformedURLException e ) {
0 commit comments