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); } }
public static DockerContainer create( CreateAgentRequest request, PluginSettings settings, DockerClient docker) throws InterruptedException, DockerException, IOException { String containerName = UUID.randomUUID().toString(); HashMap<String, String> labels = labelsFrom(request); String imageName = image(request.properties()); List<String> env = environmentFrom(request, settings, containerName); try { docker.inspectImage(imageName); } catch (ImageNotFoundException ex) { LOG.info("Image " + imageName + " not found, attempting to download."); docker.pull(imageName); } ContainerConfig.Builder containerConfigBuilder = ContainerConfig.builder(); if (request.properties().containsKey("Command")) { containerConfigBuilder.cmd( splitIntoLinesAndTrimSpaces(request.properties().get("Command")) .toArray(new String[] {})); } ContainerConfig containerConfig = containerConfigBuilder.image(imageName).labels(labels).env(env).build(); ContainerCreation container = docker.createContainer(containerConfig, containerName); String id = container.id(); ContainerInfo containerInfo = docker.inspectContainer(id); LOG.debug("Created container " + containerName); docker.startContainer(containerName); return new DockerContainer( containerName, containerInfo.created(), request.properties(), request.environment()); }