protected InspectContainerResponse.ContainerState getCurrentState(DockerClient dockerClient, String containerId) { return dockerClient.inspectContainerCmd(containerId).exec().getState(); }
default InspectContainerResponse getCurrentContainerInfo() { return DockerClientFactory.instance().client().inspectContainerCmd(getContainerId()).exec(); }
private void stopContainer(String containerId, String imageName) { boolean running; try { InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); running = containerInfo.getState().getRunning(); } catch (NotFoundException e) { dockerClient.inspectContainerCmd(containerId).exec(); } catch (NotFoundException e) { LOGGER.trace("Was going to remove container but it apparently no longer exists: {}", containerId);
@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(), ' ')); } } }
containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); containerName = containerInfo.getName(); containerIsStarting(containerInfo);
@Test public void sharedMemorySetTest() { try (GenericContainer containerWithSharedMemory = new GenericContainer("busybox:1.29") .withSharedMemorySize(1024L * FileUtils.ONE_MB)) { containerWithSharedMemory.start(); HostConfig hostConfig = containerWithSharedMemory.getDockerClient().inspectContainerCmd(containerWithSharedMemory.getContainerId()) .exec().getHostConfig(); assertEquals("Shared memory not set on container", hostConfig.getShmSize(), 1024 * FileUtils.ONE_MB); } } }
public InspectContainerResponse inspectContainer(String containerId) { this.readWriteLock.readLock().lock(); try { return this.dockerClient.inspectContainerCmd(containerId).exec(); } finally { this.readWriteLock.readLock().unlock(); } }
/** * return container last start epoch time * * @param id container id * @return long epoch time */ public long getContainerStartedAt(String id) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(id).exec(); String startedAt = inspectInfo.getState().getStartedAt(); return startedAt != null ? DateTimeFormatter.ISO_INSTANT.parse(startedAt, Instant::from).toEpochMilli() : Instant.now().toEpochMilli(); }
private void logNetworks(String containerId) { Map<String, ContainerNetwork> networks = getClient().inspectContainerCmd(containerId).exec().getNetworkSettings().getNetworks(); int networksSize = networks.size(); log.debug("There are {} network(s) in the container {}", networksSize, containerId); if (networksSize == 0) { return; } int i = 0; for (Entry<String, ContainerNetwork> network : networks.entrySet()) { log.debug("{}) {} -> {}", ++i, network.getKey(), network.getValue()); } }
private void logNetworks(String containerId) { Map<String, ContainerNetwork> networks = getClient().inspectContainerCmd(containerId).exec().getNetworkSettings().getNetworks(); int networksSize = networks.size(); log.debug("There are {} network(s) in the container {}", networksSize, containerId); if (networksSize == 0) { return; } int i = 0; for (Entry<String, ContainerNetwork> network : networks.entrySet()) { log.debug("{}) {} -> {}", ++i, network.getKey(), network.getValue()); } }
public Optional<String> getContainerStatus(String containerId) { Optional<String> result = Optional.empty(); try { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); ContainerState status = inspectInfo.getState(); result = Optional.ofNullable(status.getStatus()); } catch (Exception exp) { logWarning(MODULE_NAME, exp.getMessage()); } return result; }
private String lookupImageIdFromContainer(String containerId) { try { InspectContainerResponse containerInspectResponse = docker.inspectContainerCmd(containerId).exec(); return containerInspectResponse.getImageId(); } catch (DockerException e) { logger.error("Unable to inspect container " + containerId, e); throw new OrchestrationException(e); } }
private void logMounts(String containerId) { InspectContainerResponse exec = getClient().inspectContainerCmd(containerId).exec(); List<Mount> mounts = exec.getMounts(); log.debug("There are {} mount(s) in the container {}:", mounts.size(), containerId); for (int i = 0; i < mounts.size(); i++) { Mount mount = mounts.get(i); log.debug("{}) {} -> {} ({})", i + 1, mount.getSource(), mount.getDestination(), mount.getMode()); } }
public List<Container> getRunningContainers() { return getContainers().stream() .filter(container -> { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(container.getId()).exec(); ContainerState containerState = inspectInfo.getState(); return containerState != null && containerState.getStatus() != null && MicroserviceState.fromText(containerState.getStatus()) == MicroserviceState.RUNNING; }) .collect(Collectors.toList()); }
/** * compares if microservice's and container's settings are equal * * @param containerId container id * @param microservice microservice * @return boolean */ public boolean areMicroserviceAndContainerEqual(String containerId, Microservice microservice) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); return isPortMappingEqual(inspectInfo, microservice) && isNetworkModeEqual(inspectInfo, microservice); }
/** * @param containerId id of the container to inspect * @return IP Address of the container */ public static String getIpAddress(String containerId) { InspectContainerResponse response = DockerClientFactory.build().inspectContainerCmd(containerId).exec(); return response.getNetworkSettings().getIpAddress(); }
/** * @return IP Address of the container's gateway (which would be docker0) */ public static String getGatewayIpAddress(String containerId) { InspectContainerResponse response = DockerClientFactory.build().inspectContainerCmd(containerId).exec(); return response.getNetworkSettings().getGateway(); }
public final ComputerLauncher createLauncher(final DockerAPI api, @Nonnull final String containerId, String workdir, TaskListener listener) throws IOException, InterruptedException { final InspectContainerResponse inspect; try(final DockerClient client = api.getClient()) { inspect = client.inspectContainerCmd(containerId).exec(); } final ComputerLauncher launcher = createLauncher(api, workdir, inspect, listener); final Boolean running = inspect.getState().getRunning(); if (Boolean.FALSE.equals(running)) { listener.error("Container {} is not running. {}", containerId, inspect.getState().getStatus()); throw new IOException("Container is not running."); } return new DockerDelegatingComputerLauncher(launcher, api, containerId); }
public void updateStarted() { containersStartedByExtension = getContainers(); containersInspect = containersStartedByExtension.stream() .map(c -> dockerClient.inspectContainerCmd(c.getId()).exec()) .collect(Collectors.toList()); logs = containersStartedByExtension.stream() .map(c -> dockerClient.logContainerCmd(c.getId()) .withStdOut(true) .withStdErr(true) .exec(new LogCallback())) .collect(Collectors.toList()); }
protected void createContainer(DockerClient dockerClient) throws InterruptedException { pullImage(dockerClient); CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(properties.getDockerImage()) // NOSONAR .withLabels(getAllLabels()) .withEnv(getEnvs()) .withHostConfig(buildHostConfig()); adjustCreateCommand(createContainerCmd); String containerId = createContainerCmd.exec().getId(); dockerClient.startContainerCmd(containerId).exec(); containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); }