com.spotify.docker.client.messages.ContainerConfig Java Examples

The following examples show how to use com.spotify.docker.client.messages.ContainerConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerContainerManager.java    From simpleci with MIT License 6 votes vote down vote up
public String runContainer(ContainerConfig.Builder containerConfigBuilder, String containerName) throws DockerException {
    try {
        final ContainerConfig containerConfig = containerConfigBuilder.build();
        final ContainerCreation creation;

        creation = dockerClient.createContainer(containerConfig, containerName);

        final String id = creation.id();
        dockerClient.startContainer(id);
        return id;
    } catch (InterruptedException e) {
        logger.error("", e);
    }

    return null;
}
 
Example #2
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testBufferedRequestEntityProcessing() throws Exception {
  builder.useRequestEntityProcessing(RequestEntityProcessing.BUFFERED);
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
  
  final HostConfig hostConfig = HostConfig.builder().build();

  final ContainerConfig containerConfig = ContainerConfig.builder()
      .hostConfig(hostConfig)
      .build();

  server.enqueue(new MockResponse());

  dockerClient.createContainer(containerConfig);

  final RecordedRequest recordedRequest = takeRequestImmediately();

  assertThat(recordedRequest.getHeader("Content-Length"), notNullValue());
  assertThat(recordedRequest.getHeader("Transfer-Encoding"), nullValue());
}
 
Example #3
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testChunkedRequestEntityProcessing() throws Exception {
  builder.useRequestEntityProcessing(RequestEntityProcessing.CHUNKED);
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
  
  final HostConfig hostConfig = HostConfig.builder().build();

  final ContainerConfig containerConfig = ContainerConfig.builder()
      .hostConfig(hostConfig)
      .build();

  server.enqueue(new MockResponse());

  dockerClient.createContainer(containerConfig);

  final RecordedRequest recordedRequest = takeRequestImmediately();

  assertThat(recordedRequest.getHeader("Content-Length"), nullValue());
  assertThat(recordedRequest.getHeader("Transfer-Encoding"), is("chunked"));
}
 
Example #4
Source File: TaskConfig.java    From helios with Apache License 2.0 6 votes vote down vote up
/**
 * Create docker container configuration for a job.
 *
 * @param imageInfo The ImageInfo object.
 *
 * @return The ContainerConfig object.
 */
public ContainerConfig containerConfig(final ImageInfo imageInfo,
                                       final Optional<String> dockerVersion) {
  final ContainerConfig.Builder builder = ContainerConfig.builder();

  builder.image(job.getImage());
  builder.cmd(job.getCommand());
  builder.hostname(job.getHostname());
  builder.env(containerEnvStrings());
  builder.exposedPorts(containerExposedPorts());
  builder.volumes(volumes().keySet());
  builder.labels(job.getLabels());

  for (final ContainerDecorator decorator : containerDecorators) {
    decorator.decorateContainerConfig(job, imageInfo, dockerVersion, builder);
  }

  return builder.build();
}
 
Example #5
Source File: SyslogRedirectingContainerDecoratorTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDockerVersionPre1_9() {
  final Optional<String> dockerVersion = Optional.of("1.6.0");

  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(), hasItem("/helios/syslog-redirector"));

  final HostConfig hostConfig = hostBuilder.build();
  assertNull(hostConfig.logConfig());
  assertFalse(hostConfig.binds().isEmpty());
}
 
Example #6
Source File: SyslogRedirectingContainerDecoratorTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #7
Source File: RedisContainer.java    From pay-publicapi with MIT License 6 votes vote down vote up
RedisContainer(DockerClient docker, String host) throws DockerException, InterruptedException {

        this.docker = docker;
        this.host = host;

        failsafeDockerPull(docker);
        docker.listImages(DockerClient.ListImagesParam.create("name", REDIS_IMAGE));

        final HostConfig hostConfig = HostConfig.builder().logConfig(LogConfig.create("json-file")).publishAllPorts(true).build();
        ContainerConfig containerConfig = ContainerConfig.builder()
                .image(REDIS_IMAGE)
                .hostConfig(hostConfig)
                .build();
        containerId = docker.createContainer(containerConfig).id();
        docker.startContainer(containerId);
        port = hostPortNumber(docker.inspectContainer(containerId));
        registerShutdownHook();
        waitForRedisToStart();
    }
 
Example #8
Source File: ZookeeperDockerService.java    From pravega with Apache License 2.0 6 votes vote down vote up
private ServiceSpec setServiceSpec() {

        Map<String, String> labels = new HashMap<>();
        labels.put("com.docker.swarm.service.name", serviceName);

        final TaskSpec taskSpec = TaskSpec
                .builder()
                .containerSpec(ContainerSpec.builder().image(IMAGE_PATH + IMAGE_PREFIX + ZOOKEEPER_IMAGE_NAME + ZOOKEEPER_IMAGE_VERSION)
                        .hostname(serviceName)
                        .labels(labels)
                        .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(ZKSERVICE_ZKPORT)).build()).build())
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .resources(ResourceRequirements.builder()
                        .limits(Resources.builder().memoryBytes(mem).nanoCpus((long) cpu).build())
                        .build())
                .build();
        ServiceSpec spec = ServiceSpec.builder().name(serviceName).mode(ServiceMode.withReplicas(instances))
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(ZKSERVICE_ZKPORT).targetPort(ZKSERVICE_ZKPORT)
                        .publishMode(PortConfig.PortConfigPublishMode.HOST).build()).build())
                .taskTemplate(taskSpec)
                .build();
        return spec;
    }
 
Example #9
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #10
Source File: DockerBasedTestExecutor.java    From pravega with Apache License 2.0 6 votes vote down vote up
private ContainerConfig setContainerConfig(String methodName, String className) {

        Map<String, String> labels = new HashMap<>(2);
        labels.put("testMethodName", methodName);
        labels.put("testClassName", className);

        HostConfig hostConfig = HostConfig.builder()
                .portBindings(ImmutableMap.of(DOCKER_CLIENT_PORT + "/tcp", Arrays.asList(PortBinding.
                        of(LoginClient.MESOS_MASTER, DOCKER_CLIENT_PORT)))).networkMode("docker_gwbridge").build();

        ContainerConfig containerConfig = ContainerConfig.builder()
                .hostConfig(hostConfig)
                .image(IMAGE)
                .user("root")
                .workingDir("/data")
                .cmd("sh", "-c", "java -DmasterIP=" + LoginClient.MESOS_MASTER + " -DexecType=" + getConfig("execType",
                        "LOCAL") + " -Dlog.level=" + LOG_LEVEL +  " -cp /data/build/libs/test-docker-collection.jar io.pravega.test.system." +
                        "SingleJUnitTestRunner " + className + "#" + methodName + " > server.log 2>&1")
                .labels(labels)
                .build();

        return containerConfig;
    }
 
Example #11
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testGoogleContainerRegistryCredentialsDoesntExist() throws Exception {
  final String image = "helios-test";

  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.google-container-registry.credentials",
          ConfigValueFactory.fromAnyRef("/dev/null/foo/bar")
      );

  buildHeliosSoloDeployment(new HeliosSoloDeployment.Builder(null, config));

  ContainerConfig soloContainerConfig = null;
  for (final ContainerConfig cc : containerConfig.getAllValues()) {
    if (cc.image().contains(image)) {
      soloContainerConfig = cc;
    }
  }

  assertNotNull("Could not find helios-solo container creation", soloContainerConfig);

  assertThat(soloContainerConfig.env(), not(hasItem(startsWith("HELIOS_AGENT_OPTS="))));
  assertThat(soloContainerConfig.hostConfig().binds(), not(hasItem(startsWith("/dev/null"))));
}
 
Example #12
Source File: SupervisorTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySupervisorRestartsExitedContainer() throws Exception {
  final String containerId1 = "deadbeef1";
  final String containerId2 = "deadbeef2";

  final ContainerCreation createResponse1 = ContainerCreation.builder().id(containerId1).build();
  final ContainerCreation createResponse2 = ContainerCreation.builder().id(containerId2).build();

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse1);

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  when(docker.inspectContainer(eq(containerId1))).thenReturn(runningResponse);

  final SettableFuture<ContainerExit> waitFuture1 = SettableFuture.create();
  final SettableFuture<ContainerExit> waitFuture2 = SettableFuture.create();
  when(docker.waitContainer(containerId1)).thenAnswer(futureAnswer(waitFuture1));
  when(docker.waitContainer(containerId2)).thenAnswer(futureAnswer(waitFuture2));

  // Start the job
  sut.setGoal(START);
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId1));
  verify(docker, timeout(30000)).waitContainer(containerId1);

  // Indicate that the container exited
  when(docker.inspectContainer(eq(containerId1))).thenReturn(stoppedResponse);
  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse2);
  when(docker.inspectContainer(eq(containerId2))).thenReturn(runningResponse);
  waitFuture1.set(ContainerExit.create(1L));

  // Verify that the container was restarted
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId2));
  verify(docker, timeout(30000)).waitContainer(containerId2);
}
 
Example #13
Source File: TaskConfigTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainerConfig() throws Exception {
  final TaskConfig taskConfig = TaskConfig.builder()
      .namespace("test")
      .host(HOST)
      .job(JOB)
      .build();

  final ImageInfo imageInfo = mock(ImageInfo.class);
  final ContainerConfig containerConfig = taskConfig.containerConfig(
      imageInfo, Optional.absent());
  assertThat(ImmutableMap.copyOf(containerConfig.labels()), equalTo(LABELS));
}
 
Example #14
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  this.dockerClient = mock(DockerClient.class);
  this.heliosClient = mock(HeliosClient.class);

  // the anonymous classes to override a method are to workaround the docker-client "messages"
  // having no mutators, fun
  final Info info = mock(Info.class);
  when(info.operatingSystem()).thenReturn("foo");
  when(this.dockerClient.info()).thenReturn(info);

  // mock the call to dockerClient.createContainer so we can test the arguments passed to it
  this.containerConfig = ArgumentCaptor.forClass(ContainerConfig.class);

  final ContainerCreation creation = mock(ContainerCreation.class);
  when(creation.id()).thenReturn(CONTAINER_ID);

  when(this.dockerClient.createContainer(
      this.containerConfig.capture(), anyString())).thenReturn(creation);

  // we have to mock out several other calls to get the HeliosSoloDeployment ctor
  // to return non-exceptionally. the anonymous classes to override a method are to workaround
  // the docker-client "messages" having no mutators, fun
  when(this.dockerClient.info()).thenReturn(info);

  final PortBinding binding = PortBinding.of("192.168.1.1", 5801);
  final ImmutableMap<String, List<PortBinding>> ports =
      ImmutableMap.<String, List<PortBinding>>of("5801/tcp", ImmutableList.of(binding));
  final ContainerInfo containerInfo = mock(ContainerInfo.class);
  final NetworkSettings networkSettings = mock(NetworkSettings.class);
  when(networkSettings.gateway()).thenReturn("a-gate-way");
  when(networkSettings.ports()).thenReturn(ports);
  when(containerInfo.networkSettings()).thenReturn(networkSettings);
  when(this.dockerClient.inspectContainer(CONTAINER_ID)).thenReturn(containerInfo);

  when(this.dockerClient.waitContainer(CONTAINER_ID)).thenReturn(ContainerExit.create(0L));
}
 
Example #15
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private static String startAuthedRegistry(final DockerClient client) throws Exception {
  final Map<String, List<PortBinding>> ports = singletonMap(
      "5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", 5000)));
  final HostConfig hostConfig = HostConfig.builder().portBindings(ports)
      .binds(ImmutableList.of(
          Resources.getResource("dockerRegistry/auth").getPath() + ":/auth",
          Resources.getResource("dockerRegistry/certs").getPath() + ":/certs"
      ))
      /*
       *  Mounting volumes requires special permissions on Docker >= 1.10.
       *  Until a proper Seccomp profile is in place, run container privileged.
       */
      .privileged(true)
      .build();

  final ContainerConfig containerConfig = ContainerConfig.builder()
      .image(REGISTRY_IMAGE)
      .hostConfig(hostConfig)
      .env(ImmutableList.of(
          "REGISTRY_AUTH=htpasswd",
          "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm",
          "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd",
          "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt",
          "REGISTRY_HTTP_TLS_KEY=/certs/domain.key",
          "REGISTRY_HTTP_SECRET=super-secret"
      ))
      .build();

  return startAndAwaitContainer(client, containerConfig, REGISTRY_NAME);
}
 
Example #16
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testDockerHostContainsLocalhost() throws Exception {
  buildHeliosSoloDeployment();

  boolean foundSolo = false;
  for (final ContainerConfig cc : containerConfig.getAllValues()) {
    if (cc.image().contains("helios-solo")) {
      assertThat(cc.hostConfig().binds(), hasItem("/var/run/docker.sock:/var/run/docker.sock"));
      foundSolo = true;
    }
  }
  assertTrue("Could not find helios-solo container creation", foundSolo);
}
 
Example #17
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #18
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigHasGoogleContainerRegistryCredentials() throws Exception {
  // generate a file that we will pretend contains GCR credentials, in order to verify that
  // HeliosSoloDeployment sets up the expected environment variables and volume binds
  // when this config value exists (and is a real file)
  final File credentialsFile = temporaryFolder.newFile("fake-credentials");
  final String credentialsPath = credentialsFile.getPath();

  final String image = "helios-test";

  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.google-container-registry.credentials",
          ConfigValueFactory.fromAnyRef(credentialsPath)
      );

  buildHeliosSoloDeployment(new HeliosSoloDeployment.Builder(null, config));

  ContainerConfig soloContainerConfig = null;
  for (final ContainerConfig cc : containerConfig.getAllValues()) {
    if (cc.image().contains(image)) {
      soloContainerConfig = cc;
    }
  }

  assertNotNull("Could not find helios-solo container creation", soloContainerConfig);

  assertThat(soloContainerConfig.env(),
      hasItem("HELIOS_AGENT_OPTS=--docker-gcp-account-credentials=" + credentialsPath));

  final String credentialsParentPath = credentialsFile.getParent();
  assertThat(soloContainerConfig.hostConfig().binds(),
      hasItem(credentialsParentPath + ":" + credentialsParentPath + ":ro"));
}
 
Example #19
Source File: SyslogRedirectingContainerDecorator.java    From helios with Apache License 2.0 5 votes vote down vote up
@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();

  // If there's no explicit container cmd specified, copy over the one from the image.
  // 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 ImmutableSet.Builder<String> volumesBuilder = ImmutableSet.builder();
  if (containerConfig.volumes() != null) {
    volumesBuilder.addAll(containerConfig.volumes());
  }
  volumesBuilder.add("/helios");
  containerConfigBuilder.volumes(volumesBuilder.build());
}
 
Example #20
Source File: ReapingTest.java    From helios with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #21
Source File: SyslogRedirectionTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@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 + "'");
    }
  }
}
 
Example #22
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private static String startAndAwaitContainer(final DockerClient client,
                                             final ContainerConfig containerConfig,
                                             final String containerName)
    throws Exception {
  final ContainerCreation creation = client.createContainer(containerConfig, containerName);
  final String containerId = creation.id();
  client.startContainer(containerId);
  awaitRunning(client, containerId);
  return containerId;
}
 
Example #23
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private static String startUnauthedRegistry(final DockerClient client) throws Exception {
  final Map<String, List<PortBinding>> ports = singletonMap(
      "5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", 5000)));
  final HostConfig hostConfig = HostConfig.builder().portBindings(ports)
      .build();

  final ContainerConfig containerConfig = ContainerConfig.builder()
      .image(REGISTRY_IMAGE)
      .hostConfig(hostConfig)
      .build();

  return startAndAwaitContainer(client, containerConfig, REGISTRY_NAME);
}
 
Example #24
Source File: BookkeeperDockerService.java    From pravega with Apache License 2.0 5 votes vote down vote up
private ServiceSpec setServiceSpec() {

        Map<String, String> labels = new HashMap<>();
        labels.put("com.docker.swarm.service.name", serviceName);

        String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
        List<String> stringList = new ArrayList<>();
        String env1 = "ZK_URL=" + zk;
        String env2 = "ZK=" + zk;
        String env3 = "bookiePort=" + String.valueOf(BK_PORT);
        String env4 = "DLOG_EXTRA_OPTS=-Xms512m";
        String env5 = "BK_useHostNameAsBookieID=false";
        stringList.add(env1);
        stringList.add(env2);
        stringList.add(env3);
        stringList.add(env4);
        stringList.add(env5);

        final TaskSpec taskSpec = TaskSpec
                .builder().restartPolicy(RestartPolicy.builder().maxAttempts(3).condition(RESTART_POLICY_ANY).build())
                .containerSpec(ContainerSpec.builder()
                        .hostname(serviceName)
                        .labels(labels)
                        .image(IMAGE_PATH + IMAGE_PREFIX + BOOKKEEPER_IMAGE_NAME + PRAVEGA_VERSION)
                        .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(BK_PORT)).build())
                        .env(stringList).build())
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .resources(ResourceRequirements.builder()
                        .reservations(Resources.builder()
                                .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                        .build())
                .build();
        ServiceSpec spec = ServiceSpec.builder().name(serviceName).mode(ServiceMode.withReplicas(instances))
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(BK_PORT).build()).build())
                .taskTemplate(taskSpec)
                .build();
        return spec;
    }
 
Example #25
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerCreation createContainer(final ContainerConfig config, final String name)
    throws DockerException, InterruptedException {
  WebTarget resource = resource()
      .path("containers").path("create");

  if (name != null) {
    checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(),
                  "Invalid container name: \"%s\"", name);
    resource = resource.queryParam("name", name);
  }

  log.debug("Creating container with ContainerConfig: {}", config);

  try {
    return request(POST, ContainerCreation.class, resource, resource
        .request(APPLICATION_JSON_TYPE), Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(config.image(), e);
      case 406:
        throw new DockerException("Impossible to attach. Container not running.", e);
      default:
        throw e;
    }
  }
}
 
Example #26
Source File: Docker.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Docker(String imageName)  {
    containerConfig = ContainerConfig.builder()
            .image(imageName)
            .networkDisabled(false)
            .exposedPorts(ImmutableSet.of(EXPOSED_IMAP_PORT))
            .hostConfig(ALL_PORTS_HOST_CONFIG)
            .build();
    
    try {
        dockerClient = DefaultDockerClient.fromEnv().build();
        dockerClient.pull(imageName);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #27
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testCapAddAndDrop() throws Exception {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  final HostConfig hostConfig = HostConfig.builder()
      .capAdd(ImmutableList.of("foo", "bar"))
      .capAdd(ImmutableList.of("baz", "qux"))
      .build();

  final ContainerConfig containerConfig = ContainerConfig.builder()
      .hostConfig(hostConfig)
      .build();

  server.enqueue(new MockResponse());

  dockerClient.createContainer(containerConfig);

  final RecordedRequest recordedRequest = takeRequestImmediately();

  assertThat(recordedRequest.getMethod(), is("POST"));
  assertThat(recordedRequest.getPath(), is("/containers/create"));

  assertThat(recordedRequest.getHeader("Content-Type"), is("application/json"));

  final JsonNode requestJson = toJson(recordedRequest.getBody());
  assertThat(requestJson, is(jsonObject()
      .where("HostConfig", is(jsonObject()
          .where("CapAdd", is(jsonArray(
              containsInAnyOrder(jsonText("baz"), jsonText("qux")))))))));
}
 
Example #28
Source File: ContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
ContainerConfig.Builder containerConfig);
 
Example #29
Source File: DockerService.java    From selenium-jupiter with Apache License 2.0 4 votes vote down vote up
public synchronized String startContainer(DockerContainer dockerContainer)
        throws DockerException, InterruptedException {
    String imageId = dockerContainer.getImageId();
    log.info("Starting Docker container {}", imageId);
    com.spotify.docker.client.messages.HostConfig.Builder hostConfigBuilder = HostConfig
            .builder();
    com.spotify.docker.client.messages.ContainerConfig.Builder containerConfigBuilder = ContainerConfig
            .builder();

    boolean privileged = dockerContainer.isPrivileged();
    if (privileged) {
        log.trace("Using privileged mode");
        hostConfigBuilder.privileged(true);
        hostConfigBuilder.capAdd("NET_ADMIN", "NET_RAW");
    }
    Optional<String> network = dockerContainer.getNetwork();
    if (network.isPresent()) {
        log.trace("Using network: {}", network.get());
        hostConfigBuilder.networkMode(network.get());
    }
    Optional<Map<String, List<PortBinding>>> portBindings = dockerContainer
            .getPortBindings();
    if (portBindings.isPresent()) {
        log.trace("Using port bindings: {}", portBindings.get());
        hostConfigBuilder.portBindings(portBindings.get());
        containerConfigBuilder.exposedPorts(portBindings.get().keySet());
    }
    Optional<List<String>> binds = dockerContainer.getBinds();
    if (binds.isPresent()) {
        log.trace("Using binds: {}", binds.get());
        hostConfigBuilder.binds(binds.get());
    }
    Optional<List<String>> envs = dockerContainer.getEnvs();
    if (envs.isPresent()) {
        log.trace("Using envs: {}", envs.get());
        containerConfigBuilder.env(envs.get());
    }
    Optional<List<String>> cmd = dockerContainer.getCmd();
    if (cmd.isPresent()) {
        log.trace("Using cmd: {}", cmd.get());
        containerConfigBuilder.cmd(cmd.get());
    }
    Optional<List<String>> entryPoint = dockerContainer.getEntryPoint();
    if (entryPoint.isPresent()) {
        log.trace("Using entryPoint: {}", entryPoint.get());
        containerConfigBuilder.entrypoint(entryPoint.get());
    }

    ContainerConfig createContainer = containerConfigBuilder.image(imageId)
            .hostConfig(hostConfigBuilder.build()).build();
    String containerId = dockerClient.createContainer(createContainer).id();
    dockerClient.startContainer(containerId);

    return containerId;
}
 
Example #30
Source File: HDFSDockerService.java    From pravega with Apache License 2.0 4 votes vote down vote up
private ServiceSpec setServiceSpec() {
    Map<String, String> labels = new HashMap<>();
    labels.put("com.docker.swarm.task.name", serviceName);
    Mount mount = Mount.builder().type("volume").source("hadoop-logs").target("/opt/hadoop/logs").build();
    String env1 = "SSH_PORT=2222";
    String env2 = "HDFS_HOST=" + serviceName;

    final TaskSpec taskSpec = TaskSpec
            .builder()
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
            .containerSpec(ContainerSpec.builder().image(hdfsimage).env(Arrays.asList(env1, env2))
                    .healthcheck(ContainerConfig.Healthcheck.builder().test(customHealthCheck("ss -l | grep " + HDFS_PORT + " || exit 1")).build())
                    .mounts(mount)
                    .labels(labels)
                    .hostname(serviceName)
                    .build())
            .resources(ResourceRequirements.builder()
                    .reservations(Resources.builder()
                            .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                    .build())
            .build();
    List<PortConfig> portConfigs = new ArrayList<>();
    PortConfig port1 = PortConfig.builder().publishedPort(8020).targetPort(8020).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs").build();
    PortConfig port2 = PortConfig.builder().publishedPort(50090).targetPort(50090).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-secondary").build();
    PortConfig port3 = PortConfig.builder().publishedPort(50010).targetPort(50010).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode").build();
    PortConfig port4 = PortConfig.builder().publishedPort(50020).targetPort(50020).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode-ipc").build();
    PortConfig port5 = PortConfig.builder().publishedPort(50075).targetPort(50075).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode-http").build();
    PortConfig port6 = PortConfig.builder().publishedPort(50070).targetPort(50070).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-web").build();
    PortConfig port7 = PortConfig.builder().publishedPort(2222).targetPort(2222).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-ssh").build();
    portConfigs.add(port1);
    portConfigs.add(port2);
    portConfigs.add(port3);
    portConfigs.add(port4);
    portConfigs.add(port5);
    portConfigs.add(port6);
    portConfigs.add(port7);

    ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(instances))
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
            .endpointSpec(EndpointSpec.builder().ports(portConfigs)
                    .build()).build();
    return spec;
}