docker.inspectImage(BUSYBOX); } catch (ImageNotFoundException e) { docker.pull(BUSYBOX); docker.inspectImage(ALPINE); } catch (ImageNotFoundException e) { docker.pull(ALPINE); .hostConfig(hostConfig) .build(); final ContainerCreation creation = docker.createContainer(config, testTag + "-probe"); final String containerId = creation.id(); docker.startContainer(containerId); docker.killContainer(containerId);
@Override public Object call() throws Exception { try { dockerClient.killContainer(containerId); } catch (DockerRequestException e) { if (e.getResponseBody().contains("is not running")) { // Container already isn't running. So we continue. } else { throw e; } } try { dockerClient.removeContainer(containerId); return true; } catch (ContainerNotFoundException e) { // We're done here return true; } catch (DockerException e) { if ((e instanceof DockerRequestException) && ((DockerRequestException) e).getResponseBody().contains( "Driver btrfs failed to remove root filesystem")) { // Workaround btrfs issue where removing containers throws an exception, // but succeeds anyway. return true; } else { return null; } } } });
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; } }
/** * Stops this container. */ public void stop() throws InterruptedException { // Tell docker to stop or eventually kill the container final String container = containerId.or(containerName); // Interrupt the thread blocking on waitContainer stopAsync().awaitTerminated(); if (System.getenv("CONTAINER_STATS") != null) { try { log.info("container {} stats: {}", containerName, docker.stats(containerName)); } catch (DockerException e) { log.warn("Could not log container stats. Exception was {}", e); } } try { docker.stopContainer(container, secondsToWaitBeforeKill); } catch (DockerException e) { if ((e instanceof ContainerNotFoundException) && !containerId.isPresent()) { // we tried to stop the container by name but no container of the given name existed. // this isn't surprising or exceptional, just means the container wasn't started yet. } else { log.warn("Stopping container {} failed", container, e); } } }
docker.pull(image); listener.pulled(); log.info("Pulled image {} in {}s", image, pullTime.elapsed(SECONDS)); docker.inspectImage(image); } catch (ImageNotFoundException e) {
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) { List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(image)); if (images.size() == 0) { dockerClient.pull(imageToPull, new AnsiProgressHandler()); final ContainerCreation container = dockerClient.createContainer(containerConfig, containerName); dockerClient.startContainer(container.id()); return new ContainerCreationStatus(true, containerName, container.id(), nodePort); } catch (DockerRequestException e) {
final String expose = port + "/udp"; docker.pull(ALPINE); .hostConfig(hostConfig) .build(); final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog"); final String syslogContainerId = creation.id(); docker.startContainer(syslogContainerId); final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId); assertThat(containerInfo.state().running(), equalTo(true)); logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr()); final String log = logs.readFully(); final String logType = docker.inspectContainer(taskStatus.getContainerId()) .hostConfig().logConfig().logType(); assertEquals("syslog", logType); try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) { log = logs.readFully();
private void startContainer(final String name) throws DockerException, InterruptedException { docker.pull(BUSYBOX); final HostConfig hostConfig = HostConfig.builder().build(); final ContainerConfig config = ContainerConfig.builder() .image(BUSYBOX) .cmd(IDLE_COMMAND) .hostConfig(hostConfig) .build(); final ContainerCreation creation = docker.createContainer(config, name); final String containerId = creation.id(); docker.startContainer(containerId); } }
@Before public void setup() throws Exception { try (final DockerClient docker = getNewDockerClient()) { // Build an image with an ENTRYPOINT and CMD prespecified final String dockerDirectory = Resources.getResource("syslog-test-image").getPath(); docker.build(Paths.get(dockerDirectory), testImage); // Figure out the host IP from the container's point of view (needed for syslog) final ContainerConfig config = ContainerConfig.builder() .image(BUSYBOX) .cmd(asList("ip", "route", "show")) .build(); final ContainerCreation creation = docker.createContainer(config); final String containerId = creation.id(); docker.startContainer(containerId); // Wait for the container to exit. // If we don't wait, docker.logs() might return an epmty string because the container // cmd hasn't run yet. docker.waitContainer(containerId); final String log; try (LogStream logs = docker.logs(containerId, stdout(), stderr())) { log = logs.readFully(); } final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log); if (m.find()) { syslogHost = m.group("gateway"); } else { fail("couldn't determine the host address from '" + log + "'"); } } }
public static void before() throws Throwable { if (existContainer()) { logger.info("Container exists, removing"); if (elasticsearchRule.getDockerClient().inspectContainer(CONTAINER_NAME).state().running()) { elasticsearchRule.getDockerClient().killContainer(CONTAINER_NAME); } elasticsearchRule.getDockerClient().removeContainer(CONTAINER_NAME); } else { logger.info("Container does not exist"); } logger.info("Creating container"); elasticsearchRule.before(); }
/** * Stops PostgreSql. * * @throws Exception If fails to stop PostgreSql. */ public void stopPostgreSql() throws Exception { // Close all the connections for (Connection connection : this.connections) { try { connection.close(); } catch (SQLException ex) { // ignore failure } } // Stop PostgresSQL System.out.println("Stopping PostgreSQL"); this.docker.killContainer(this.postgresContainerId); this.docker.removeContainer(this.postgresContainerId); this.docker.close(); }
/** * Deploy and start docker container, expose ports, connect to a network * * @return docker container id */ private String deployContainer() { ContainerConfig containerConfig = buildContainerConfig(); String id; try { ContainerCreation container = docker.createContainer(containerConfig, params.getName()); id = container.id(); dockerManager.addShutdownHook(params.getName()); docker.disconnectFromNetwork(id, "bridge"); docker.connectToNetwork(id, docker.inspectNetwork(universeParams.getNetworkName()).id()); docker.startContainer(id); String ipAddr = docker.inspectContainer(id) .networkSettings().networks() .values().asList().get(0) .ipAddress(); if (StringUtils.isEmpty(ipAddr)) { throw new NodeException("Empty Ip address for container: " + params.getName()); } ipAddress.set(ipAddr); } catch (InterruptedException | DockerException e) { throw new NodeException("Can't start a container", e); } return id; }
DockerRule(DockerRuleParams params) { this.params = params; dockerClient = createDockerClient(); ContainerConfig containerConfig = createContainerConfig(params.imageName, params.ports, params.cmd); try { if (!params.isLocalImage) { dockerClient.pull(params.imageName); } container = dockerClient.createContainer(containerConfig); } catch (DockerException | InterruptedException e) { throw new IllegalStateException(e); } }
public String runContainer(ContainerConfig.Builder containerConfigBuilder, String containerName) throws DockerException { try { final ContainerConfig containerConfig = containerConfigBuilder.build(); final ContainerCreation creation; creation = dockerClient.createContainer(containerConfig, containerName); final String id = creation.id(); dockerClient.startContainer(id); return id; } catch (InterruptedException e) { logger.error("", e); } return null; }
private String startContainer(final String image, final Optional<String> dockerVersion) throws InterruptedException, DockerException { // Get container image info final ImageInfo imageInfo = docker.inspectImage(image); if (imageInfo == null) { throw new HeliosRuntimeException("docker inspect image returned null on image " + image); } // Create container final HostConfig hostConfig = config.hostConfig(dockerVersion); final ContainerConfig containerConfig = config.containerConfig(imageInfo, dockerVersion) .toBuilder() .hostConfig(hostConfig) .build(); listener.creating(); final ContainerCreation container = docker.createContainer(containerConfig, containerName); log.info("created container: {}: {}, {}", config, container, containerConfig); listener.created(container.id()); // Start container log.info("starting container: {}: {} {}", config, container.id(), hostConfig); listener.starting(); docker.startContainer(container.id()); log.info("started container: {}: {}", config, container.id()); listener.started(); return container.id(); }
public void runContainer() { try { buildImageConfig(); try { createImage(); } catch (ImageNotFoundException e) { dockerClient.pull(dockerHost.getImage(), new ProcessHandlerLogger()); createImage(); } dockerClient.startContainer(containerId); final ContainerInfo info = dockerClient.inspectContainer(containerId); portMappings = info.networkSettings().ports(); } catch (InterruptedException | DockerException e) { logger.error("Error while setting up docker container", e); throw new RuntimeException("Error while setting up docker container", e); } }
public void stopAndRemove(String containerId) { try { dockerClient.stopContainer(containerId, 10); dockerClient.removeContainer(containerId); } catch (DockerException | InterruptedException e) { logger.error("", e); } }
/** * Start a docker container * * @throws NodeException this exception will be thrown if the container can not be started */ public void start(String containerName) { log.info("Starting docker container: {}", containerName); try { ContainerInfo container = docker.inspectContainer(containerName); if (container.state().running() || container.state().paused()) { log.warn("The container `{}` already running, should stop before start", container.name()); return; } docker.startContainer(containerName); } catch (DockerException | InterruptedException ex) { throw new NodeException("Can't start container " + containerName, ex); } }
private void startContainer(DockerClient docker, String containerId) { LOG.info("Starting Couchbase container '{}", CONTAINER_NAME); try { docker.startContainer(containerId); LOG.debug("Couchbase container '{}' started", CONTAINER_NAME); } catch (DockerException | InterruptedException e) { LOG.error("Unable to start Couchbase container '{}'. Is there something running on the same ports?", CONTAINER_NAME); throw new EnvironmentBuilderException("Unable to start Couchbase container", e); } }
LONG_WAIT_SECONDS, SECONDS); assertJobEquals(job, firstTaskStatus.getJob()); assertNotNull(dockerClient.inspectContainer(firstTaskStatus.getContainerId())); dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running()); dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running()); dockerClient.killContainer(firstTaskStatus.getContainerId()); assertFalse( dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running()); dockerClient.killContainer(firstRestartedContainerId); assertFalse(dockerClient.inspectContainer(firstRestartedContainerId).state().running()); assertTrue(dockerClient.inspectContainer(secondRestartedContainerId).state().running());