io.github.bonigarcia.seljup.DockerService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-jupiter Show documentation
Show all versions of selenium-jupiter Show documentation
JUnit 5 extension for Selenium WebDriver
/*
* (C) Copyright 2017 Boni Garcia (http://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.seljup;
import static com.spotify.docker.client.DockerClient.RemoveContainerParam.forceKill;
import static com.spotify.docker.client.DockerClient.Signal.SIGKILL;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang.SystemUtils.IS_OS_LINUX;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import com.google.common.collect.ImmutableMap;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DefaultDockerClient.Builder;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.ProgressHandler;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.HostConfig;
import com.spotify.docker.client.messages.PortBinding;
import com.spotify.docker.client.messages.ProgressMessage;
import io.github.bonigarcia.seljup.config.Config;
/**
* Docker Service.
*
* @author Boni Garcia ([email protected])
* @since 1.1.2
*/
public class DockerService {
final Logger log = getLogger(lookup().lookupClass());
private Config config;
private String dockerDefaultSocket;
private int dockerWaitTimeoutSec;
private int dockerPollTimeMs;
private DockerClient dockerClient;
private InternalPreferences preferences;
private boolean localDaemon = true;
public DockerService(Config config, InternalPreferences preferences) {
this.config = config;
this.preferences = preferences;
dockerDefaultSocket = getConfig().getDockerDefaultSocket();
dockerWaitTimeoutSec = getConfig().getDockerWaitTimeoutSec();
dockerPollTimeMs = getConfig().getDockerPollTimeMs();
String dockerServerUrl = getConfig().getDockerServerUrl();
Builder dockerClientBuilder = null;
if (dockerServerUrl.isEmpty()) {
try {
dockerClientBuilder = DefaultDockerClient.fromEnv();
} catch (DockerCertificateException e) {
throw new SeleniumJupiterException(e);
}
} else {
log.debug("Using Docker server URL {}", dockerServerUrl);
dockerClientBuilder = DefaultDockerClient.builder()
.uri(dockerServerUrl);
}
dockerClient = dockerClientBuilder.build();
}
public String getHost(String containerId, String network)
throws DockerException, InterruptedException {
String dockerHost = getConfig().getDockerHost();
if( !dockerHost.isEmpty() ) {
return dockerHost;
}
return IS_OS_LINUX
? dockerClient.inspectContainer(containerId).networkSettings()
.networks().get(network).gateway()
: dockerClient.getHost();
}
public synchronized String startContainer(DockerContainer dockerContainer)
throws DockerException, InterruptedException {
String imageId = dockerContainer.getImageId();
log.info("Starting Docker container {}", imageId);
com.spotify.docker.client.messages.HostConfig.Builder hostConfigBuilder = HostConfig
.builder();
com.spotify.docker.client.messages.ContainerConfig.Builder containerConfigBuilder = ContainerConfig
.builder();
boolean privileged = dockerContainer.isPrivileged();
if (privileged) {
log.trace("Using privileged mode");
hostConfigBuilder.privileged(true);
hostConfigBuilder.capAdd("NET_ADMIN", "NET_RAW");
}
Optional network = dockerContainer.getNetwork();
if (network.isPresent()) {
log.trace("Using network: {}", network.get());
hostConfigBuilder.networkMode(network.get());
}
Optional