Search in sources :

Example 6 with ContainerConfig

use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.

the class HeliosSoloDeploymentTest method testConfig.

@Test
public void testConfig() throws Exception {
    final String image = "helios-test";
    final String ns = "namespace";
    final String env = "stuff";
    final Config config = ConfigFactory.empty().withValue("helios.solo.profile", ConfigValueFactory.fromAnyRef("test")).withValue("helios.solo.profiles.test.image", ConfigValueFactory.fromAnyRef(image)).withValue("helios.solo.profiles.test.namespace", ConfigValueFactory.fromAnyRef(ns)).withValue("helios.solo.profiles.test.env.TEST", ConfigValueFactory.fromAnyRef(env));
    final HeliosSoloDeployment deployment = buildHeliosSoloDeployment(new HeliosSoloDeployment.Builder(null, config));
    assertEquals(ns + ".solo.local", deployment.agentName());
    boolean foundSolo = false;
    for (final ContainerConfig cc : containerConfig.getAllValues()) {
        if (cc.image().contains(image)) {
            foundSolo = true;
            assertThat(cc.env(), hasItem("TEST=" + env));
            assertThat(cc.env(), hasItem("HELIOS_NAME=" + ns + ".solo.local"));
        }
    }
    assertTrue("Could not find helios-solo container creation", foundSolo);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Config(com.typesafe.config.Config) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with ContainerConfig

use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.

the class SyslogRedirectingContainerDecorator method decorateContainerConfig.

@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion, ContainerConfig.Builder containerConfigBuilder) {
    if (!useSyslogRedirector(dockerVersion)) {
        return;
    }
    final ContainerConfig imageConfig = imageInfo.config();
    // Inject syslog-redirector in the entrypoint to capture std out/err
    final String syslogRedirectorPath = Optional.fromNullable(job.getEnv().get("SYSLOG_REDIRECTOR")).or("/helios/syslog-redirector");
    final List<String> entrypoint = Lists.newArrayList(syslogRedirectorPath, "-h", syslogHostPort, "-n", job.getId().toString(), "--");
    if (imageConfig.entrypoint() != null) {
        entrypoint.addAll(imageConfig.entrypoint());
    }
    containerConfigBuilder.entrypoint(entrypoint);
    final ContainerConfig containerConfig = containerConfigBuilder.build();
    // Only setting the entrypoint causes dockerd to not use the image cmd.
    if ((containerConfig.cmd() == null || containerConfig.cmd().isEmpty()) && imageConfig.cmd() != null) {
        containerConfigBuilder.cmd(imageConfig.cmd());
    }
    final ImmutableMap.Builder<String, Map> volumesBuilder = ImmutableMap.builder();
    if (containerConfig.volumes() != null) {
        volumesBuilder.putAll(containerConfig.volumes());
    }
    volumesBuilder.put("/helios", new HashMap<>());
    containerConfigBuilder.volumes(volumesBuilder.build());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with ContainerConfig

use of com.spotify.docker.client.messages.ContainerConfig 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();
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) HostConfig(com.spotify.docker.client.messages.HostConfig) ImageInfo(com.spotify.docker.client.messages.ImageInfo)

Example 9 with ContainerConfig

use of com.spotify.docker.client.messages.ContainerConfig 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);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Integer.toHexString(java.lang.Integer.toHexString)

Example 10 with ContainerConfig

use of com.spotify.docker.client.messages.ContainerConfig 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);
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) IOException(java.io.IOException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)14 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)8 HostConfig (com.spotify.docker.client.messages.HostConfig)8 Test (org.junit.Test)7 DockerClient (com.spotify.docker.client.DockerClient)3 DockerException (com.spotify.docker.client.exceptions.DockerException)3 ContainerExit (com.spotify.docker.client.messages.ContainerExit)3 ImageInfo (com.spotify.docker.client.messages.ImageInfo)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 LogStream (com.spotify.docker.client.LogStream)2 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 Integer.toHexString (java.lang.Integer.toHexString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 LogConfig (com.spotify.docker.client.messages.LogConfig)1 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)1