Search in sources :

Example 6 with PortBinding

use of com.spotify.docker.client.messages.PortBinding 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 7 with PortBinding

use of com.spotify.docker.client.messages.PortBinding 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)

Example 8 with PortBinding

use of com.spotify.docker.client.messages.PortBinding in project shinyproxy by openanalytics.

the class DockerEngineBackend method prepareProxy.

@Override
protected void prepareProxy(DockerContainerProxy proxy, ContainerProxyRequest request) throws Exception {
    Builder hostConfigBuilder = HostConfig.builder();
    List<PortBinding> portBindings = Collections.emptyList();
    if (proxy.getPort() > 0)
        portBindings = Collections.singletonList(PortBinding.of("0.0.0.0", proxy.getPort()));
    hostConfigBuilder.portBindings(Collections.singletonMap(String.valueOf(getAppPort(proxy)), portBindings));
    Optional.ofNullable(Utils.memoryToBytes(request.app.getDockerMemory())).ifPresent(l -> hostConfigBuilder.memory(l));
    Optional.ofNullable(request.app.getDockerNetwork()).ifPresent(n -> hostConfigBuilder.networkMode(request.app.getDockerNetwork()));
    hostConfigBuilder.dns(request.app.getDockerDns());
    hostConfigBuilder.binds(getBindVolumes(request.app));
    hostConfigBuilder.privileged(Boolean.valueOf(getProperty(PROPERTY_PRIVILEGED, request.app, DEFAULT_PRIVILEGED)));
    ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfigBuilder.build()).image(request.app.getDockerImage()).exposedPorts(String.valueOf(getAppPort(proxy))).cmd(request.app.getDockerCmd()).env(buildEnv(request.userId, request.app)).build();
    ContainerCreation container = dockerClient.createContainer(containerConfig);
    proxy.setContainerId(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) Builder(com.spotify.docker.client.messages.HostConfig.Builder)

Example 9 with PortBinding

use of com.spotify.docker.client.messages.PortBinding in project zeppelin by apache.

the class DockerInterpreterProcess method start.

@Override
public void start(String userName) throws IOException {
    docker = DefaultDockerClient.builder().uri(URI.create(DOCKER_HOST)).build();
    removeExistContainer(containerName);
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    // Bind container ports to host ports
    int intpServicePort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
    this.dockerIntpServicePort = String.valueOf(intpServicePort);
    final String[] ports = { dockerIntpServicePort };
    for (String port : ports) {
        List<PortBinding> hostPorts = new ArrayList<>();
        hostPorts.add(PortBinding.of("0.0.0.0", port));
        portBindings.put(port, hostPorts);
    }
    final HostConfig hostConfig = HostConfig.builder().networkMode("host").portBindings(portBindings).build();
    DockerSpecTemplate specTemplate = new DockerSpecTemplate();
    specTemplate.loadProperties(getTemplateBindings());
    URL urlTemplate = this.getClass().getResource(DOCKER_INTP_JINJA);
    String template = Resources.toString(urlTemplate, StandardCharsets.UTF_8);
    String dockerCommand = specTemplate.render(template);
    int firstLineIsNewline = dockerCommand.indexOf("\n");
    if (firstLineIsNewline == 0) {
        dockerCommand = dockerCommand.replaceFirst("\n", "");
    }
    LOGGER.info("dockerCommand = {}", dockerCommand);
    List<String> listEnv = getListEnvs();
    LOGGER.info("docker listEnv = {}", listEnv);
    // check if the interpreter process exit script
    // if interpreter process exit, then container need exit
    StringBuilder sbStartCmd = new StringBuilder();
    sbStartCmd.append("sleep 10; ");
    sbStartCmd.append("process=RemoteInterpreterServer; ");
    sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
    sbStartCmd.append("while [ ! -z \"$RUNNING_PIDS\" ]; ");
    sbStartCmd.append("do sleep 1; ");
    sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
    sbStartCmd.append("done");
    // Create container with exposed ports
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).hostname(this.intpEventServerHost).image(containerImage).workingDir("/").env(listEnv).cmd("sh", "-c", sbStartCmd.toString()).build();
    try {
        LOGGER.info("wait docker pull image {} ...", containerImage);
        docker.pull(containerImage, new ProgressHandler() {

            @Override
            public void progress(ProgressMessage message) throws DockerException {
                if (null != message.error()) {
                    LOGGER.error(message.toString());
                }
            }
        });
        final ContainerCreation containerCreation = docker.createContainer(containerConfig, containerName);
        this.containerId = containerCreation.id();
        // Start container
        docker.startContainer(containerId);
        copyRunFileToContainer(containerId);
        execInContainer(containerId, dockerCommand, false);
    } catch (DockerException e) {
        LOGGER.error(e.getMessage(), e);
        throw new IOException(e.getMessage());
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
        throw new IOException(e.getMessage());
    }
    long startTime = System.currentTimeMillis();
    // wait until interpreter send dockerStarted message through thrift rpc
    synchronized (dockerStarted) {
        if (!dockerStarted.get()) {
            try {
                dockerStarted.wait(getConnectTimeout());
            } catch (InterruptedException e) {
                LOGGER.error("Remote interpreter is not accessible");
                throw new IOException(e.getMessage());
            }
        }
    }
    if (!dockerStarted.get()) {
        LOGGER.info("Interpreter docker creation is time out in {} seconds", getConnectTimeout() / 1000);
    }
    // waits for interpreter thrift rpc server ready
    while (System.currentTimeMillis() - startTime < getConnectTimeout()) {
        if (RemoteInterpreterUtils.checkIfRemoteEndpointAccessible(getHost(), getPort())) {
            break;
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) HashMap(java.util.HashMap) ProgressHandler(com.spotify.docker.client.ProgressHandler) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) HostConfig(com.spotify.docker.client.messages.HostConfig) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

PortBinding (com.spotify.docker.client.messages.PortBinding)9 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)8 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)8 List (java.util.List)7 HashMap (java.util.HashMap)6 HostConfig (com.spotify.docker.client.messages.HostConfig)5 ArrayList (java.util.ArrayList)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)3 DockerClient (com.spotify.docker.client.DockerClient)2 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)2 DockerException (com.spotify.docker.client.exceptions.DockerException)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Map (java.util.Map)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 NotNull (com.microsoft.azuretools.azurecommons.helpers.NotNull)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 DockerException (com.spotify.docker.client.DockerException)1 LogStream (com.spotify.docker.client.LogStream)1