Skip to content

Commit

Permalink
Use default gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Oct 26, 2023
1 parent 8a5b728 commit 86e58bb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/github/bonigarcia/wdm/docker/DockerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class DockerService {
final Logger log = getLogger(lookup().lookupClass());

public static final String NETWORK_HOST = "host";
private static final String DEFAULT_GATEWAY = "172.17.0.1";
private static final String BETA = "beta";
private static final String DEV = "dev";
private static final String LATEST_MINUS = "latest-";
Expand Down Expand Up @@ -141,7 +142,7 @@ private DockerClient getDockerClient(String dockerHost) {

public String getHost(String containerId, String network) {
String host = getDefaultHost();
if (IS_OS_LINUX && isRunningInsideDocker()) {
if (IS_OS_LINUX && isRunningInsideDocker() || isHost(network)) {
host = getGateway(containerId, network);
log.debug(
"WebDriverManager running inside a Docker container. Using gateway address: {}",
Expand All @@ -163,8 +164,12 @@ public String getDefaultHost() {
}

public String getGateway(String containerId, String network) {
return dockerClient.inspectContainerCmd(containerId).exec()
String gateway = dockerClient.inspectContainerCmd(containerId).exec()
.getNetworkSettings().getNetworks().get(network).getGateway();
if (isNullOrEmpty(gateway)) {
return DEFAULT_GATEWAY;
}
return gateway;
}

public String getAddress(String containerId, String network) {
Expand Down Expand Up @@ -672,8 +677,7 @@ public DockerContainer startBrowserContainer(String dockerImage,
browserContainer.setContainerId(containerId);
String gateway = getGateway(containerId, network);
browserContainer.setGateway(gateway);
String browserHost = isHost(network) ? gateway
: getHost(containerId, network);
String browserHost = getHost(containerId, network);
String browserPort = isHost(network) ? dockerBrowserPort
: getBindPort(containerId, dockerBrowserPort + "/tcp");
String browserUrlFormat = "http://%s:%s/";
Expand Down

0 comments on commit 86e58bb

Please sign in to comment.