try { InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); running = containerInfo.getState().getRunning(); } catch (NotFoundException e) { LOGGER.trace("Was going to stop container but it apparently no longer exists: {}", containerId);
/** * @return is the container created? */ default boolean isCreated() { if (getContainerId() == null) { return false; } try { String status = getCurrentContainerInfo().getState().getStatus(); return "created".equalsIgnoreCase(status) || isRunning(); } catch (DockerException e) { return false; } }
@Override public void invoke() { super.start(); this.followOutput(new Slf4jLogConsumer(logger())); // wait for the compose container to stop, which should only happen after it has spawned all the service containers logger().info("Docker Compose container is running for command: {}", Joiner.on(" ").join(this.getCommandParts())); while (this.isRunning()) { logger().trace("Compose container is still running"); Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } logger().info("Docker Compose has finished running"); AuditLogger.doComposeLog(this.getCommandParts(), this.getEnv()); final Integer exitCode = this.dockerClient.inspectContainerCmd(containerId) .exec() .getState() .getExitCode(); if (exitCode == null || exitCode != 0) { throw new ContainerLaunchException( "Containerised Docker Compose exited abnormally with code " + exitCode + " whilst running command: " + StringUtils.join(this.getCommandParts(), ' ')); } } }
try { InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); running = containerInfo.getState().getRunning(); } catch (NotFoundException e) { LOGGER.trace("Was going to stop container but it apparently no longer exists: {}", containerId);
dockerClient.startContainerCmd(exec.getId()).exec(); dockerClient.logContainerCmd(exec.getId()).withFollowStream(true).withStdErr(true).withStdOut(true).exec(callback); while (dockerClient.inspectContainerCmd(exec.getId()).exec().getState().getRunning()) { try { Thread.sleep(300); return dockerClient.inspectContainerCmd(exec.getId()).exec().getState().getExitCode(); } finally { dockerClient.removeContainerCmd(exec.getId()).exec();
return; if (!icr.getState().getStatus().equals("paused")) { return; if (!icr.getState().getRunning()) { return;
/** * gets {@link Container} status * * @param containerId - id of {@link Container} * @return {@link MicroserviceStatus} */ public MicroserviceStatus getMicroserviceStatus(String containerId) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); ContainerState containerState = inspectInfo.getState(); MicroserviceStatus result = new MicroserviceStatus(); if (containerState != null) { if (containerState.getStartedAt() != null) { result.setStartTime(getStartedTime(containerState.getStartedAt())); } if (containerState.getStatus() != null) { MicroserviceState microserviceState = MicroserviceState.fromText(containerState.getStatus()); result.setStatus(MicroserviceState.RESTARTING.equals(microserviceState) && RestartStuckChecker.isStuck(containerId) ? MicroserviceState.STUCK_IN_RESTART : microserviceState); } result.setContainerId(containerId); result.setUsage(containerId); } return result; }
/** * Based on this status, is this container running, and has it been doing so for the specified amount of time? * * @param state the state provided by InspectContainer * @param minimumRunningDuration minimum duration to consider this as "solidly" running, or null * @param now the time to consider as the current time * @return true if we can conclude that the container is running, false otherwise */ public static boolean isContainerRunning(InspectContainerResponse.ContainerState state, Duration minimumRunningDuration, Instant now) { if (state.getRunning()) { if (minimumRunningDuration == null) { return true; } Instant startedAt = DateTimeFormatter.ISO_INSTANT.parse( state.getStartedAt(), Instant::from); if (startedAt.isBefore(now.minus(minimumRunningDuration))) { return true; } } return false; }
@Override protected ComputerLauncher createLauncher(DockerAPI api, String workdir, InspectContainerResponse inspect, TaskListener listener) throws IOException, InterruptedException { if ("exited".equals(inspect.getState().getStatus())) { // Something went wrong // FIXME report error "somewhere" visible to end user. LOGGER.error("Failed to launch docker SSH agent :" + inspect.getState().getExitCode()); throw new IOException("Failed to launch docker SSH agent. Container exited with status " + inspect.getState().getExitCode()); } LOGGER.debug("container created {}", inspect); final InetSocketAddress address = getBindingForPort(api, inspect, port); // Wait until sshd has started // TODO we could (also) have a more generic mechanism relying on healthcheck (inspect State.Health.Status) final PortUtils.ConnectionCheck connectionCheck = PortUtils.connectionCheck( address ) .withRetries( maxNumRetries ) .withEveryRetryWaitFor( retryWaitTime, TimeUnit.SECONDS ); if (!connectionCheck.execute() || !connectionCheck.useSSH().execute()) { throw new IOException("SSH service didn't start after " + retryWaitTime*maxNumRetries + "s."); } return sshKeyStrategy.getSSHLauncher(address, this); }
@Override public void invoke() { super.start(); this.followOutput(new Slf4jLogConsumer(logger())); // wait for the compose container to stop, which should only happen after it has spawned all the service containers logger().info("Docker Compose container is running for command: {}", Joiner.on(" ").join(this.getCommandParts())); while (this.isRunning()) { logger().trace("Compose container is still running"); Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } logger().info("Docker Compose has finished running"); AuditLogger.doComposeLog(this.getCommandParts(), this.getEnv()); final Integer exitCode = this.dockerClient.inspectContainerCmd(containerId).exec().getState().getExitCode(); if (exitCode == null || exitCode != 0) { throw new ContainerLaunchException("Containerised Docker Compose exited abnormally with code " + exitCode + " whilst running command: " + StringUtils.join(this.getCommandParts(), ' ')); } } }
@Override public boolean call() { try { return client .inspectContainerCmd(cube.getId()) .exec() .getState() .getHealth() .getStatus() .equals("healthy"); } catch (NotFoundException e) { return false; } } }
@Override public boolean call() { try { return client .inspectContainerCmd(cube.getId()) .exec() .getState() .getHealth() .getStatus() .equals("healthy"); } catch (NotFoundException e) { return false; } } }
/** * @return has the container health state 'healthy'? */ default boolean isHealthy() { if (getContainerId() == null) { return false; } try { InspectContainerResponse inspectContainerResponse = getCurrentContainerInfo(); String healthStatus = inspectContainerResponse.getState().getHealth().getStatus(); return healthStatus.equals(STATE_HEALTHY); } catch (DockerException e) { return false; } }
private Stream<Container> asContainer(String container) { return inspectContainerCmd(container) .map(response -> new Container( response.getConfig().getHostName(), new DockerImage(response.getConfig().getImage()), containerResourcesFromHostConfig(response.getHostConfig()), new ContainerName(decode(response.getName())), Container.State.valueOf(response.getState().getStatus().toUpperCase()), response.getState().getPid() )) .map(Stream::of) .orElse(Stream.empty()); }
/** * @return is the container created? */ default boolean isCreated() { if (getContainerId() == null) { return false; } try { String status = getCurrentContainerInfo().getState().getStatus(); return "created".equalsIgnoreCase(status) || isRunning(); } catch (DockerException e) { return false; } }
/** * @return is the container currently running? */ default boolean isRunning() { if (getContainerId() == null) { return false; } try { Boolean running = getCurrentContainerInfo().getState().getRunning(); return Boolean.TRUE.equals(running); } catch (DockerException e) { return false; } }
/** * Based on this status, has the container halted? * * @param state the state provided by InspectContainer * @return true if we can conclude that the container has started but is now stopped, false otherwise. */ public static boolean isContainerStopped(InspectContainerResponse.ContainerState state) { // get some preconditions out of the way if (state.getRunning() || state.getPaused()) { return false; } // if the finished timestamp is non-empty, that means the container started and finished. boolean hasStarted = isDockerTimestampNonEmpty(state.getStartedAt()); boolean hasFinished = isDockerTimestampNonEmpty(state.getFinishedAt()); return hasStarted && hasFinished; }
public ContainerAssert hasStatus(String status) { ContainerStateAssert stateAssert = new ContainerStateAssert(getContainerState()); stateAssert.isNotNull(); if (!Objects.areEqual(getContainerState().getStatus(), status)) { failWithMessage("Expected container's status to be %s but was %s", status, getContainerState().getStatus()); } return this; }
public boolean isRunningContainer(String containerName) { boolean isRunning = inspectContainer(containerName).getState().getRunning(); log.trace("Container {} is running: {}", containerName, isRunning); return isRunning; }
public ContainersAssert areRunning() { for (InspectContainerResponse container : this.actual) { ContainerStateAssert stateAssert = new ContainerStateAssert(container.getState()); stateAssert.isNotNull(); if (!container.getState().getRunning()) { failWithMessage("Container %s is not running", container.getName()); } } return this; } }