use of com.spotify.docker.client.messages.HostConfig 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);
}
}
use of com.spotify.docker.client.messages.HostConfig in project azure-tools-for-java by Microsoft.
the class DockerUtil method createContainer.
/**
* create container with specified ImageName:TagName.
*/
@NotNull
public static String createContainer(@NotNull DockerClient docker, @NotNull String imageNameWithTag, @NotNull String containerServerPort) throws DockerException, InterruptedException {
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
List<PortBinding> randomPort = new ArrayList<>();
PortBinding randomBinding = PortBinding.randomPort("0.0.0.0");
randomPort.add(randomBinding);
portBindings.put(containerServerPort, randomPort);
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image(imageNameWithTag).exposedPorts(containerServerPort).build();
final ContainerCreation container = docker.createContainer(config);
return container.id();
}
use of com.spotify.docker.client.messages.HostConfig in project flink by apache.
the class GCloudEmulatorManager method launchDocker.
public static void launchDocker() throws DockerException, InterruptedException, DockerCertificateException {
// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
docker = DefaultDockerClient.fromEnv().build();
terminateAndDiscardAnyExistingContainers(true);
LOG.info("");
LOG.info("/===========================================");
LOG.info("| GCloud Emulator");
ContainerInfo containerInfo;
String id;
try {
docker.inspectImage(DOCKER_IMAGE_NAME);
} catch (ImageNotFoundException e) {
// No such image so we must download it first.
LOG.info("| - Getting docker image \"{}\"", DOCKER_IMAGE_NAME);
docker.pull(DOCKER_IMAGE_NAME, message -> {
if (message.id() != null && message.progress() != null) {
LOG.info("| - Downloading > {} : {}", message.id(), message.progress());
}
});
}
// No such container. Good, we create one!
LOG.info("| - Creating new container");
// Bind container ports to host ports
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
portBindings.put(INTERNAL_PUBSUB_PORT, Collections.singletonList(PortBinding.randomPort("0.0.0.0")));
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
// Create new container with exposed ports
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).exposedPorts(INTERNAL_PUBSUB_PORT).image(DOCKER_IMAGE_NAME).cmd("sh", "-c", "mkdir -p /opt/data/pubsub ; gcloud beta emulators pubsub start --data-dir=/opt/data/pubsub --host-port=0.0.0.0:" + INTERNAL_PUBSUB_PORT).build();
LOG.debug("Launching container with configuration {}", containerConfig);
final ContainerCreation creation = docker.createContainer(containerConfig, CONTAINER_NAME_JUNIT);
id = creation.id();
containerInfo = docker.inspectContainer(id);
if (!containerInfo.state().running()) {
LOG.warn("| - Starting it up ....");
docker.startContainer(id);
Thread.sleep(1000);
}
containerInfo = docker.inspectContainer(id);
dockerIpAddress = "127.0.0.1";
Map<String, List<PortBinding>> ports = containerInfo.networkSettings().ports();
assertNotNull("Unable to retrieve the ports where to connect to the emulators", ports);
assertEquals("We expect 1 port to be mapped", 1, ports.size());
pubsubPort = getPort(ports, INTERNAL_PUBSUB_PORT, "PubSub");
LOG.info("| Waiting for the emulators to be running");
// PubSub exposes an "Ok" at the root url when running.
if (!waitForOkStatus("PubSub", pubsubPort)) {
// Oops, we did not get an "Ok" within 10 seconds
startHasFailedKillEverything();
}
LOG.info("\\===========================================");
LOG.info("");
}
Aggregations