Search in sources :

Example 11 with ImageNotFoundException

use of com.spotify.docker.client.exceptions.ImageNotFoundException in project helios by spotify.

the class SystemTestBase method assertDockerReachable.

private void assertDockerReachable(final int probePort) throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Pull our base images
        try {
            docker.inspectImage(BUSYBOX);
        } catch (ImageNotFoundException e) {
            docker.pull(BUSYBOX);
        }
        try {
            docker.inspectImage(ALPINE);
        } catch (ImageNotFoundException e) {
            docker.pull(ALPINE);
        }
        // Start a container with an exposed port
        final HostConfig hostConfig = HostConfig.builder().portBindings(ImmutableMap.of("4711/tcp", singletonList(PortBinding.of("0.0.0.0", probePort)))).build();
        final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd("nc", "-p", "4711", "-lle", "cat").exposedPorts(ImmutableSet.of("4711/tcp")).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "-probe");
        final String containerId = creation.id();
        docker.startContainer(containerId);
        // Wait for container to come up
        Polling.await(5, SECONDS, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                final ContainerInfo info = docker.inspectContainer(containerId);
                return info.state().running() ? true : null;
            }
        });
        log.info("Verifying that docker containers are reachable");
        try {
            Polling.awaitUnchecked(5, SECONDS, new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    log.info("Probing: {}:{}", DOCKER_HOST.address(), probePort);
                    try (final Socket ignored = new Socket(DOCKER_HOST.address(), probePort)) {
                        return true;
                    } catch (IOException e) {
                        return false;
                    }
                }
            });
        } catch (TimeoutException e) {
            fail("Please ensure that DOCKER_HOST is set to an address that where containers can " + "be reached. If docker is running in a local VM, DOCKER_HOST must be set to the " + "address of that VM. If docker can only be reached on a limited port range, " + "set the environment variable DOCKER_PORT_RANGE=start:end");
        }
        docker.killContainer(containerId);
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) IOException(java.io.IOException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with ImageNotFoundException

use of com.spotify.docker.client.exceptions.ImageNotFoundException in project helios by spotify.

the class TaskRunner method pullImage.

private void pullImage(final String image) throws DockerException, InterruptedException {
    listener.pulling();
    DockerTimeoutException wasTimeout = null;
    final Stopwatch pullTime = Stopwatch.createStarted();
    // Attempt to pull.  Failure, while less than ideal, is ok.
    try {
        docker.pull(image);
        listener.pulled();
        log.info("Pulled image {} in {}s", image, pullTime.elapsed(SECONDS));
    } catch (DockerTimeoutException e) {
        log.warn("Pulling image {} failed with timeout after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
        wasTimeout = e;
    } catch (DockerException e) {
        log.warn("Pulling image {} failed after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
    }
    try {
        // If we don't have the image by now, fail.
        docker.inspectImage(image);
    } catch (ImageNotFoundException e) {
        // to know, as the pull should have fixed the not found-ness.
        if (wasTimeout != null) {
            throw new ImagePullFailedException("Failed pulling image " + image + " because of timeout", wasTimeout);
        }
        throw e;
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ImagePullFailedException(com.spotify.docker.client.exceptions.ImagePullFailedException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) Stopwatch(com.google.common.base.Stopwatch) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 13 with ImageNotFoundException

use of com.spotify.docker.client.exceptions.ImageNotFoundException in project helios by spotify.

the class TaskMonitorTest method verifyImageMissingTrumpsFlappingState.

@Test
public void verifyImageMissingTrumpsFlappingState() throws Exception {
    when(flapController.isFlapping()).thenReturn(true);
    sut.failed(new ImageNotFoundException("foobar", "not found"), "container error");
    verify(statusUpdater).setThrottleState(IMAGE_MISSING);
    verify(statusUpdater).setState(FAILED);
    verify(statusUpdater).setContainerError("container error");
    verify(statusUpdater).update();
}
Also used : ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Test(org.junit.Test)

Example 14 with ImageNotFoundException

use of com.spotify.docker.client.exceptions.ImageNotFoundException 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

ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)14 DockerException (com.spotify.docker.client.exceptions.DockerException)7 Test (org.junit.Test)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)5 IOException (java.io.IOException)4 WebTarget (javax.ws.rs.client.WebTarget)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)2 DockerClient (com.spotify.docker.client.DockerClient)2 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)2 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)2 ImagePullFailedException (com.spotify.docker.client.exceptions.ImagePullFailedException)2 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 HostConfig (com.spotify.docker.client.messages.HostConfig)2 InterruptedIOException (java.io.InterruptedIOException)2 Long.toHexString (java.lang.Long.toHexString)2 ExecutionException (java.util.concurrent.ExecutionException)2 Stopwatch (com.google.common.base.Stopwatch)1