com.spotify.docker.client.exceptions.ContainerNotFoundException Java Examples

The following examples show how to use com.spotify.docker.client.exceptions.ContainerNotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerContainers.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
private DockerContainers unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
    Period period = settings.getAutoRegisterPeriod();
    DockerContainers unregisteredContainers = new DockerContainers();

    for (String containerName : instances.keySet()) {
        if (knownAgents.containsAgentWithId(containerName)) {
            continue;
        }

        ContainerInfo containerInfo;
        try {
            containerInfo = docker(settings).inspectContainer(containerName);
        } catch (ContainerNotFoundException e) {
            LOG.warn("The container " + containerName + " could not be found.");
            continue;
        }
        DateTime dateTimeCreated = new DateTime(containerInfo.created());

        if (clock.now().isAfter(dateTimeCreated.plus(period))) {
            unregisteredContainers.register(DockerContainer.fromContainerInfo(containerInfo));
        }
    }
    return unregisteredContainers;
}
 
Example #2
Source File: PushPullIT.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private static void awaitStopped(final DockerClient client,
                                 final String containerId)
    throws Exception {
  Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      boolean containerRemoved = false;
      try {
        client.inspectContainer(containerId);
      } catch (ContainerNotFoundException e) {
        containerRemoved = true;
      }

      return containerRemoved ? true : null;
    }
  });
}
 
Example #3
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void resizeTty(final String containerId, final Integer height, final Integer width)
    throws DockerException, InterruptedException {
  checkTtyParams(height, width);

  WebTarget resource = resource().path("containers").path(containerId).path("resize");
  if (height != null && height > 0) {
    resource = resource.queryParam("h", height);
  }
  if (width != null && width > 0) {
    resource = resource.queryParam("w", width);
  }

  try {
    request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #4
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private void containerAction(final String containerId, final String action,
                             final MultivaluedMap<String, String> queryParameters)
        throws DockerException, InterruptedException {
  try {
    WebTarget resource = resource()
            .path("containers").path(containerId).path(action);

    for (Map.Entry<String, List<String>> queryParameter : queryParameters.entrySet()) {
      for (String parameterValue : queryParameter.getValue()) {
        resource = resource.queryParam(queryParameter.getKey(), parameterValue);
      }
    }
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #5
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void stopContainer(final String containerId, final int secondsToWaitBeforeKilling)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = noTimeoutResource()
        .path("containers").path(containerId).path("stop")
        .queryParam("t", String.valueOf(secondsToWaitBeforeKilling));
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 304: // already stopped, so we're cool
        return;
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #6
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ContainerExit waitContainer(final String containerId)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = noTimeoutResource()
        .path("containers").path(containerId).path("wait");
    // Wait forever
    return request(POST, ContainerExit.class, resource,
                   resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #7
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void removeContainer(final String containerId, final RemoveContainerParam... params)
    throws DockerException, InterruptedException {
  try {
    WebTarget resource = resource().path("containers").path(containerId);

    for (final RemoveContainerParam param : params) {
      resource = resource.queryParam(param.name(), param.value());
    }

    request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new BadParamException(getQueryParamMap(resource()), e);
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #8
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream exportContainer(String containerId)
    throws DockerException, InterruptedException {
  final WebTarget resource = resource()
      .path("containers").path(containerId).path("export");
  try {
    return request(GET, InputStream.class, resource,
                   resource.request(APPLICATION_OCTET_STREAM_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #9
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ContainerStats stats(final String containerId)
    throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("containers").path(containerId).path("stats")
      .queryParam("stream", "0");

  try {
    return request(GET, ContainerStats.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #10
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private LogStream getLogStream(final String method, final WebTarget resource,
                               final String containerId)
    throws DockerException, InterruptedException {
  try {
    final Invocation.Builder request = resource.request("application/vnd.docker.raw-stream");
    return request(method, LogStream.class, resource, request);
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new BadParamException(getQueryParamMap(resource), e);
      case 404:
        throw new ContainerNotFoundException(containerId);
      default:
        throw e;
    }
  }
}
 
Example #11
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public TopResults topContainer(final String containerId, final String psArgs)
    throws DockerException, InterruptedException {
  try {
    WebTarget resource = resource().path("containers").path(containerId).path("top");
    if (!Strings.isNullOrEmpty(psArgs)) {
      resource = resource.queryParam("ps_args", psArgs);
    }
    return request(GET, TopResults.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #12
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<ContainerChange> inspectContainerChanges(final String containerId)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("containers").path(containerId).path("changes");
    return request(GET, CONTAINER_CHANGE_LIST, resource,
                   resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #13
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ContainerUpdate updateContainer(final String containerId, final HostConfig config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.22");
  try {
    WebTarget resource = resource().path("containers").path(containerId).path("update");
    return request(POST, ContainerUpdate.class, resource, resource.request(APPLICATION_JSON_TYPE),
            Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId);
      default:
        throw e;
    }
  }
}
 
Example #14
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
private ContainerInfo getContainerInfo(final String existingContainerId)
    throws DockerException, InterruptedException {
  if (existingContainerId == null) {
    return null;
  }
  log.info("inspecting container: {}: {}", config, existingContainerId);
  try {
    return docker.inspectContainer(existingContainerId);
  } catch (ContainerNotFoundException e) {
    return null;
  }
}
 
Example #15
Source File: GCloudEmulatorManager.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void terminateAndDiscardAnyExistingContainers(boolean warnAboutExisting) throws DockerException, InterruptedException {
	ContainerInfo containerInfo;
	try {
		containerInfo = docker.inspectContainer(CONTAINER_NAME_JUNIT);
		// Already have this container running.

		assertNotNull("We should either we get containerInfo or we get an exception", containerInfo);

		LOG.info("");
		LOG.info("/===========================================");
		if (warnAboutExisting) {
			LOG.warn("|    >>> FOUND OLD EMULATOR INSTANCE RUNNING <<< ");
			LOG.warn("| Destroying that one to keep tests running smoothly.");
		}
		LOG.info("| Cleanup of GCloud Emulator");

		// We REQUIRE 100% accurate side effect free unit tests
		// So we completely discard this one.

		String id = containerInfo.id();
		// Kill container
		if (containerInfo.state().running()) {
			docker.killContainer(id);
			LOG.info("| - Killed");
		}

		// Remove container
		docker.removeContainer(id);

		LOG.info("| - Removed");
		LOG.info("\\===========================================");
		LOG.info("");

	} catch (ContainerNotFoundException cnfe) {
		// No such container. Good !
	}
}
 
Example #16
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public void renameContainer(final String containerId, final String name)
    throws DockerException, InterruptedException {
  WebTarget resource = resource()
      .path("containers").path(containerId).path("rename");

  if (name == null) {
    throw new IllegalArgumentException("Cannot rename container to null");
  }

  checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(),
                "Invalid container name: \"%s\"", name);
  resource = resource.queryParam("name", name);

  log.info("Renaming container with id {}. New name {}.", containerId, name);

  try {
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      case 409:
        throw new ContainerRenameConflictException(containerId, name, e);
      default:
        throw e;
    }
  }
}
 
Example #17
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerInfo inspectContainer(final String containerId)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("containers").path(containerId).path("json");
    return request(GET, ContainerInfo.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #18
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream archiveContainer(String containerId, String path)
    throws DockerException, InterruptedException {
  final String apiVersion = version().apiVersion();
  final int versionComparison = compareVersion(apiVersion, "1.20");

  // Version below 1.20
  if (versionComparison < 0) {
    throw new UnsupportedApiVersionException(apiVersion);
  }

  final WebTarget resource = resource()
      .path("containers").path(containerId).path("archive")
      .queryParam("path", path);

  try {
    return request(GET, InputStream.class, resource,
                   resource.request(APPLICATION_OCTET_STREAM_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #19
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
public InputStream copyContainer(String containerId, String path)
    throws DockerException, InterruptedException {
  final String apiVersion = version().apiVersion();
  final int versionComparison = compareVersion(apiVersion, "1.24");

  // Version above 1.24
  if (versionComparison >= 0) {
    throw new UnsupportedApiVersionException(apiVersion);
  }

  final WebTarget resource = resource()
      .path("containers").path(containerId).path("copy");

  // Internal JSON object; not worth it to create class for this
  final JsonNodeFactory nf = JsonNodeFactory.instance;
  final JsonNode params = nf.objectNode().set("Resource", nf.textNode(path));

  try {
    return request(POST, InputStream.class, resource,
                   resource.request(APPLICATION_OCTET_STREAM_TYPE),
                   Entity.json(params));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #20
Source File: BaseTest.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
protected void assertContainerDoesNotExist(String id) throws DockerException, InterruptedException {
    try {
        docker.inspectContainer(id);
        fail("Expected ContainerNotFoundException");
    } catch (ContainerNotFoundException expected) {

    }
}
 
Example #21
Source File: BaseTest.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void afterClass() throws Exception {
    for (String container : containers) {
        try {
            docker.inspectContainer(container);
            docker.stopContainer(container, 2);
            docker.removeContainer(container);
        } catch (ContainerNotFoundException ignore) {

        }
    }
}
 
Example #22
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
public void terminate(DockerClient docker) throws DockerException, InterruptedException {
    try {
        LOG.debug("Terminating instance " + this.name());
        docker.stopContainer(name, 2);
        docker.removeContainer(name);
    } catch (ContainerNotFoundException ignore) {
        LOG.warn("Cannot terminate a container that does not exist " + name);
    }
}
 
Example #23
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public ContainerCreation commitContainer(final String containerId,
                                         final String repo,
                                         final String tag,
                                         final ContainerConfig config,
                                         final String comment,
                                         final String author)
    throws DockerException, InterruptedException {

  checkNotNull(containerId, "containerId");
  checkNotNull(repo, "repo");
  checkNotNull(config, "containerConfig");

  WebTarget resource = resource()
      .path("commit")
      .queryParam("container", containerId)
      .queryParam("repo", repo);

  if (!isNullOrEmpty(author)) {
    resource = resource.queryParam("author", author);
  }
  if (!isNullOrEmpty(comment)) {
    resource = resource.queryParam("comment", comment);
  }
  if (!isNullOrEmpty(tag)) {
    resource = resource.queryParam("tag", tag);
  }

  log.debug("Committing container id: {} to repository: {} with ContainerConfig: {}", containerId,
           repo, config);

  try {
    return request(POST, ContainerCreation.class, resource, resource
        .request(APPLICATION_JSON_TYPE), Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}