use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class TaskRunner method startContainer.
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();
}
use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class SyslogRedirectingContainerDecorator method decorateHostConfig.
@Override
public void decorateHostConfig(Job job, Optional<String> dockerVersion, HostConfig.Builder hostConfigBuilder) {
final HostConfig hostConfig = hostConfigBuilder.build();
if (useSyslogRedirector(dockerVersion)) {
final List<String> binds = Lists.newArrayList();
if (hostConfig.binds() != null) {
binds.addAll(hostConfig.binds());
}
binds.add("/usr/lib/helios:/helios:ro");
hostConfigBuilder.binds(binds);
} else {
final ImmutableMap.Builder<String, String> logOpts = ImmutableMap.builder();
logOpts.put("syslog-address", "udp://" + syslogHostPort);
// match the behavior of syslog-redirector
logOpts.put("syslog-facility", "local0");
logOpts.put("tag", job.getId().toString());
hostConfigBuilder.logConfig(LogConfig.create("syslog", logOpts.build()));
}
}
use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class ReapingTest method startContainer.
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);
}
use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class SystemTestBase method assertDockerReachable.
private void assertDockerReachable(final int probePort) throws Exception {
try (final DockerClient docker = getNewDockerClient()) {
// Pull our base images
try {
docker.inspectImage(BUSYBOX);
} catch (ImageNotFoundException e) {
docker.pull(BUSYBOX);
}
try {
docker.inspectImage(ALPINE);
} catch (ImageNotFoundException e) {
docker.pull(ALPINE);
}
// Start a container with an exposed port
final HostConfig hostConfig = HostConfig.builder().portBindings(ImmutableMap.of("4711/tcp", singletonList(PortBinding.of("0.0.0.0", probePort)))).build();
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd("nc", "-p", "4711", "-lle", "cat").exposedPorts(ImmutableSet.of("4711/tcp")).hostConfig(hostConfig).build();
final ContainerCreation creation = docker.createContainer(config, testTag + "-probe");
final String containerId = creation.id();
docker.startContainer(containerId);
// Wait for container to come up
Polling.await(5, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
final ContainerInfo info = docker.inspectContainer(containerId);
return info.state().running() ? true : null;
}
});
log.info("Verifying that docker containers are reachable");
try {
Polling.awaitUnchecked(5, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
log.info("Probing: {}:{}", DOCKER_HOST.address(), probePort);
try (final Socket ignored = new Socket(DOCKER_HOST.address(), probePort)) {
return true;
} catch (IOException e) {
return false;
}
}
});
} catch (TimeoutException e) {
fail("Please ensure that DOCKER_HOST is set to an address that where containers can " + "be reached. If docker is running in a local VM, DOCKER_HOST must be set to the " + "address of that VM. If docker can only be reached on a limited port range, " + "set the environment variable DOCKER_PORT_RANGE=start:end");
}
docker.killContainer(containerId);
}
}
use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class SyslogRedirectingContainerDecoratorTest method testWithDockerVersionPost1_9.
@Test
public void testWithDockerVersionPost1_9() {
final Optional<String> dockerVersion = Optional.of("1.12.1");
final SyslogRedirectingContainerDecorator decorator = new SyslogRedirectingContainerDecorator(SYSLOG_HOST_PORT);
final HostConfig.Builder hostBuilder = HostConfig.builder();
decorator.decorateHostConfig(JOB, dockerVersion, hostBuilder);
final ContainerConfig.Builder containerBuilder = ContainerConfig.builder();
decorator.decorateContainerConfig(JOB, imageInfo, dockerVersion, containerBuilder);
final ContainerConfig containerConfig = containerBuilder.build();
assertThat(containerConfig.entrypoint(), not(hasItem("/helios/syslog-redirector")));
final HostConfig hostConfig = hostBuilder.build();
final LogConfig logConfig = hostConfig.logConfig();
assertEquals("syslog", logConfig.logType());
assertEquals(JOB.getId().toString(), logConfig.logOptions().get("tag"));
assertEquals("udp://" + SYSLOG_HOST_PORT, logConfig.logOptions().get("syslog-address"));
}
Aggregations