Search in sources :

Example 51 with HostConfig

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);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Arrays(java.util.Arrays) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Environment(de.zalando.ep.zalenium.util.Environment) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) URL(java.net.URL) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) LoggerFactory(org.slf4j.LoggerFactory) GoogleAnalyticsApi(de.zalando.ep.zalenium.util.GoogleAnalyticsApi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) ArrayList(java.util.ArrayList) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Map(java.util.Map) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerException(com.spotify.docker.client.exceptions.DockerException) Logger(org.slf4j.Logger) DockerSeleniumStarterRemoteProxy(de.zalando.ep.zalenium.proxy.DockerSeleniumStarterRemoteProxy) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) Container(com.spotify.docker.client.messages.Container) List(java.util.List) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Image(com.spotify.docker.client.messages.Image) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HostConfig(com.spotify.docker.client.messages.HostConfig) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Image(com.spotify.docker.client.messages.Image) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) HostConfig(com.spotify.docker.client.messages.HostConfig)

Example 52 with HostConfig

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();
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HostConfig(com.spotify.docker.client.messages.HostConfig) ArrayList(java.util.ArrayList) List(java.util.List) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull)

Example 53 with HostConfig

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("");
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Map(java.util.Map) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerException(com.spotify.docker.client.exceptions.DockerException) Logger(org.slf4j.Logger) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Assert.assertNotNull(org.junit.Assert.assertNotNull) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PortBinding(com.spotify.docker.client.messages.PortBinding) HostConfig(com.spotify.docker.client.messages.HostConfig) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HashMap(java.util.HashMap) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) HostConfig(com.spotify.docker.client.messages.HostConfig) List(java.util.List) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Aggregations

HostConfig (com.spotify.docker.client.messages.HostConfig)53 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)30 Test (org.junit.Test)29 Matchers.containsString (org.hamcrest.Matchers.containsString)22 Long.toHexString (java.lang.Long.toHexString)19 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)19 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)16 DockerClient (com.spotify.docker.client.DockerClient)12 List (java.util.List)11 DockerException (com.spotify.docker.client.exceptions.DockerException)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 LogStream (com.spotify.docker.client.LogStream)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)4 PortBinding (com.spotify.docker.client.messages.PortBinding)4 IOException (java.io.IOException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableList (com.google.common.collect.ImmutableList)3