@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; }
Builder(ContainerCreation source) { this.id = source.id(); this.warnings = source.warnings(); } @Override
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(); }
@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 + "'"); } } }
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); } }
.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); }
Builder(ContainerCreation source) { this.id = source.id(); this.warnings = source.warnings(); } @Override
/** * Block until container exit. */ public void waitForExit() throws InterruptedException { try { dockerClient.waitContainer(container.id()); } catch (DockerException e) { throw new IllegalStateException(e); } }
@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; }
@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); } }
protected void waitForLog(String messageToMatch) throws DockerException, InterruptedException, UnsupportedEncodingException { LogStream logs = dockerClient.logs(container.id(), follow(), stdout()); String log; do { LogMessage logMessage = logs.next(); ByteBuffer buffer = logMessage.content(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); log = new String(bytes); } while (!log.contains(messageToMatch)); } }
public void setUpDocker() { LOG.info("Setting up Docker container '{}'", containerName); pullContainer(); Optional<Container> existingContainer = findExistingContainer(); if (existingContainer.isPresent()) { LOG.warn("Container '{}' already exists", containerName); killAndRemoveContainer(existingContainer.get()); } ContainerCreation container = createContainer(); containerId = container.id(); waitFor(this::startContainer); waitForContainerToStart(); }
private String createContainer(DockerTask task) throws Exception { final ContainerConfig containerConfig = configureContainer(task); final ContainerCreation creation = docker.createContainer(containerConfig, task.getName()); return creation.id(); }
@SneakyThrows public RunningDockerInstance run(AliveStrategy aliveStrategyCheck) { try { return startContainerIfNecessary(dockerClient.inspectContainer(containerName), aliveStrategyCheck); } catch (DockerException de) { ensureImageExists(); ContainerCreation container = createContainer(); return startContainer(container.id(), aliveStrategyCheck); } }
@Override protected void before() throws Throwable { super.before(); dockerClient.startContainer(container.id()); ContainerInfo info = dockerClient.inspectContainer(container.id()); ports = info.networkSettings().ports(); if (params.portToWaitOn != null) { waitForPort(getHostPort(params.portToWaitOn), params.waitTimeout); } if (params.logToWait != null) { waitForLog(params.logToWait); } }