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(); }
@JsonCreator static ContainerCreation create( @JsonProperty("Id") final String id, @JsonProperty("Warnings") final List<String> warnings) { return builder() .id(id) .warnings(warnings) .build(); } }
@Deprecated public List<String> getWarnings() { return warnings(); }
@Deprecated public List<String> getWarnings() { return warnings(); }
@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 + "'"); } } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof ContainerCreation) { ContainerCreation that = (ContainerCreation) o; return ((this.id == null) ? (that.id() == null) : this.id.equals(that.id())) && ((this.warnings == null) ? (that.warnings() == null) : this.warnings.equals(that.warnings())); } return false; }
@JsonCreator static ContainerCreation create( @JsonProperty("Id") final String id, @JsonProperty("Warnings") final List<String> warnings) { return builder() .id(id) .warnings(warnings) .build(); } }
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); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof ContainerCreation) { ContainerCreation that = (ContainerCreation) o; return ((this.id == null) ? (that.id() == null) : this.id.equals(that.id())) && ((this.warnings == null) ? (that.warnings() == null) : this.warnings.equals(that.warnings())); } return false; }
.build(); final ContainerCreation creation = docker.createContainer(config, testTag + "-probe"); final String containerId = creation.id(); docker.startContainer(containerId);
.build(); final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog"); final String syslogContainerId = creation.id(); docker.startContainer(syslogContainerId);
/** * Id of container (null if it is not yet been created or has been stopped). */ public String getContainerId() { return (container!=null ? container.id() : null); }
/** * Block until container exit. */ public void waitForExit() throws InterruptedException { try { dockerClient.waitContainer(container.id()); } catch (DockerException e) { throw new IllegalStateException(e); } }
@Override protected void after() { super.after(); try { dockerClient.killContainer(container.id()); dockerClient.removeContainer(container.id(), removeVolumes()); dockerClient.close(); } catch (DockerException | InterruptedException e) { throw new RuntimeException("Unable to stop/remove docker container " + container.id(), e); } }
private String createContainer() throws InterruptedException { final long startTime = System.currentTimeMillis(); // equivalent to "docker run" final ContainerCreation container; try { if (name == null) { container = client.createContainer(containerConfig); } else { container = client.createContainer(containerConfig, name); } return container.id(); } catch (final DockerException e) { throw new IllegalStateException("Unable to create container using " + containerConfig, e); } finally { final long elapsedMillis = System.currentTimeMillis() - startTime; LOGGER.info("Created container {} in {}ms", containerConfig.image(), elapsedMillis); } }
/** * Container log. */ public String getLog() { try (LogStream stream = dockerClient.logs(container.id(), LogsParam.stdout(), LogsParam.stderr());) { String fullLog = stream.readFully(); if (log.isTraceEnabled()) { log.trace("{} full log: {}", containerShortId, StringUtils.replace(fullLog, "\n", "|")); } return fullLog; } catch (DockerException | InterruptedException e) { throw new IllegalStateException(e); } }