Source:
private WebDriver createContainerWebDriver(TestCleaner cleaner, String image, MutableCapabilities capabilities) throws IOException { try { Path log = Files.createTempFile("ath-docker-browser", "log"); LOGGER.info("Starting selenium container. Logs in " + log); if (!IOUtil.isTcpPortFree(4444)) throw new IllegalStateException("Port 4444 is occupied"); Docker.cmd("pull", image).popen().verifyOrDieWith("Failed to pull image " + image); String[] args = {"run", "-d", "--shm-size=2g", "--network=host", image}; ProcessInputStream popen = Docker.cmd(args).popen(); popen.waitFor(); String cid = popen.verifyOrDieWith("Failed to run selenium container").trim(); new ProcessBuilder(Docker.cmd("logs", "-f", cid).toCommandArray()).redirectErrorStream(true).redirectOutput(log.toFile()).start(); Closeable cleanContainer = new Closeable() { @Override public void close() { try { Docker.cmd("kill", cid).popen().verifyOrDieWith("Failed to kill " + cid); Docker.cmd("rm", cid).popen().verifyOrDieWith("Failed to rm " + cid); } catch (IOException | InterruptedException e) { throw new Error("Failed removing container", e); } } @Override public String toString() { return "Kill and remove selenium container"; } }; Thread.sleep(3000); try { RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("<LINK_0>4444/wd/hub"), capabilities); cleaner.addTask(cleanContainer); return remoteWebDriver; } catch (RuntimeException e) { cleanContainer.close(); throw e; } catch (Throwable e) { cleanContainer.close(); throw new Error(e); } } catch (InterruptedException e) { throw new Error(e); } }

Target:
private WebDriver createContainerWebDriver(TestCleaner cleaner, String image, MutableCapabilities capabilities) throws IOException { try { final int controlPort = IOUtil.randomTcpPort(); final int vncPort = IOUtil.randomTcpPort(5900, 6000); final int displayNumber = vncPort - 5900; Path log = Files.createTempFile("ath-docker-browser", "log"); LOGGER.info("Starting selenium container. Logs in " + log); Docker.cmd("pull", image).popen().verifyOrDieWith("Failed to pull image " + image); String[] args = { "run", "-d", "--shm-size=2g", "--network=host", "-e", "SE_OPTS=-port " + controlPort, "-e", "DISPLAY=:" + displayNumber, image }; ProcessInputStream popen = Docker.cmd(args).popen(); popen.waitFor(); String cid = popen.verifyOrDieWith("Failed to run selenium container").trim(); new ProcessBuilder(Docker.cmd("logs", "-f", cid).toCommandArray()).redirectErrorStream(true).redirectOutput(log.toFile()).start(); Closeable cleanContainer = new Closeable() { @Override public void close() { try { Docker.cmd("kill", cid).popen().verifyOrDieWith("Failed to kill " + cid); Docker.cmd("rm", cid).popen().verifyOrDieWith("Failed to rm " + cid); } catch (IOException | InterruptedException e) { throw new Error("Failed removing container", e); } } @Override public String toString() { return "Kill and remove selenium container"; } }; Thread.sleep(3000); try { RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("<LINK_0>" + controlPort + "/wd/hub"), capabilities); cleaner.addTask(cleanContainer); return remoteWebDriver; } catch (RuntimeException e) { cleanContainer.close(); throw e; } catch (Throwable e) { cleanContainer.close(); throw new Error(e); } } catch (InterruptedException e) { throw new Error(e); } }