use of com.spotify.docker.client.AnsiProgressHandler in project zalenium by zalando.
the class DockerContainerClient method createContainer.
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
String containerName = generateContainerName(zaleniumContainerName, nodePort);
loadMountedFolders(zaleniumContainerName);
// In some environments the created containers need to be labeled so the platform can handle them. E.g. Rancher.
loadSeleniumContainerLabels();
loadPullSeleniumImageFlag();
loadIsZaleniumPrivileged(zaleniumContainerName);
loadStorageOpts(zaleniumContainerName);
List<String> binds = generateMountedFolderBinds();
binds.add("/dev/shm:/dev/shm");
String noVncPort = envVars.get("NOVNC_PORT");
String networkMode = getZaleniumNetwork(zaleniumContainerName);
List<String> extraHosts = new ArrayList<>();
extraHosts.add(String.format("%s:%s", DOCKER_FOR_MAC_LOCALHOST_NAME, DOCKER_FOR_MAC_LOCALHOST_IP));
// Allows "--net=host" work. Only supported for Linux.
if (DOCKER_NETWORK_HOST_MODE_NAME.equalsIgnoreCase(networkMode)) {
envVars.put("SELENIUM_HUB_HOST", "127.0.0.1");
envVars.put("SELENIUM_NODE_HOST", "127.0.0.1");
envVars.put("PICK_ALL_RANDOM_PORTS", "true");
try {
String hostName = dockerClient.info().name();
extraHosts.add(String.format("%s:%s", hostName, "127.0.1.0"));
} catch (DockerException | InterruptedException e) {
logger.debug(nodeId + " Error while getting host name", e);
}
}
// Reflect extra hosts of the hub container
final List<String> hubExtraHosts = getContainerExtraHosts(zaleniumContainerName);
extraHosts.addAll(hubExtraHosts);
HostConfig hostConfig = HostConfig.builder().appendBinds(binds).networkMode(networkMode).extraHosts(extraHosts).autoRemove(true).storageOpt(storageOpt).privileged(isZaleniumPrivileged).build();
List<String> flattenedEnvVars = envVars.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList());
flattenedEnvVars.addAll(zaleniumHttpEnvVars);
final String[] exposedPorts = { nodePort, noVncPort };
ContainerConfig.Builder builder = ContainerConfig.builder().image(image).env(flattenedEnvVars).exposedPorts(exposedPorts).hostConfig(hostConfig);
if (seleniumContainerLabels.size() > 0) {
builder.labels(seleniumContainerLabels);
}
final ContainerConfig containerConfig = builder.build();
try {
if (pullSeleniumImage) {
List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(image));
if (images.size() == 0) {
// If the image has no tag, we add latest, otherwise we end up pulling all the images with that name.
String imageToPull = image.lastIndexOf(':') > 0 ? image : image.concat(":latest");
dockerClient.pull(imageToPull, new AnsiProgressHandler());
}
}
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while checking (and pulling) if the image is present", e);
ga.trackException(e);
}
try {
final ContainerCreation container = dockerClient.createContainer(containerConfig, containerName);
dockerClient.startContainer(container.id());
return new ContainerCreationStatus(true, containerName, nodePort);
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while starting a new container", e);
ga.trackException(e);
return new ContainerCreationStatus(false);
}
}
Aggregations