com.github.dockerjava.api.model.Link Java Examples

The following examples show how to use com.github.dockerjava.api.model.Link. 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: MesosAgentContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
private CreateContainerCmd getBaseCommand() {
    String hostDir = MesosCluster.getClusterHostDir().getAbsolutePath();
    List<Bind> binds = new ArrayList<>();
    binds.add(Bind.parse("/var/run/docker.sock:/var/run/docker.sock:rw"));
    binds.add(Bind.parse("/sys/fs/cgroup:/sys/fs/cgroup"));
    binds.add(Bind.parse(hostDir + ":" + hostDir));
    if (getCluster().getMapAgentSandboxVolume()) {
        binds.add(Bind.parse(String.format("%s:%s:rw", hostDir + "/.minimesos/sandbox-" + getClusterId() + "/" + hostName, MESOS_AGENT_WORK_DIR + hostName + "/slaves")));
    }
    CreateContainerCmd cmd = DockerClientFactory.build().createContainerCmd(getImageName() + ":" + getImageTag())
        .withName(getName())
        .withHostName(hostName)
        .withPrivileged(true)
        .withVolumes(new Volume(MESOS_AGENT_WORK_DIR + hostName))
        .withEnv(newEnvironment()
            .withValues(getMesosAgentEnvVars())
            .withValues(getSharedEnvVars())
            .createEnvironment())
        .withPidMode("host")
        .withLinks(new Link(getZooKeeper().getContainerId(), "minimesos-zookeeper"))
        .withBinds(binds.stream().toArray(Bind[]::new));

    MesosDns mesosDns = getCluster().getMesosDns();
    if (mesosDns != null) {
        cmd.withDns(mesosDns.getIpAddress());
    }

    return cmd;
}
 
Example #2
Source File: MesosClusterTest.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Test
public void testMasterLinkedToAgents() throws UnirestException {
    List<MesosAgent> containers = CLUSTER.getAgents();
    for (MesosAgent container : containers) {
        InspectContainerResponse exec = DockerClientFactory.build().inspectContainerCmd(container.getContainerId()).exec();

        List<Link> links = Arrays.asList(exec.getHostConfig().getLinks());

        Assert.assertNotNull(links);
        Assert.assertEquals("link to zookeeper is expected", 1, links.size());
        Assert.assertEquals("minimesos-zookeeper", links.get(0).getAlias());
    }
}
 
Example #3
Source File: GenericContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
private Set<Link> findLinksFromThisContainer(String alias, LinkableContainer linkableContainer) {
    return dockerClient.listContainersCmd()
            .withStatusFilter(Arrays.asList("running"))
            .exec().stream()
            .flatMap(container -> Stream.of(container.getNames()))
            .filter(name -> name.endsWith(linkableContainer.getContainerName()))
            .map(name -> new Link(name, alias))
            .collect(Collectors.toSet());
}
 
Example #4
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createContainerWithLink() throws DockerException {
    String containerName1 = "containerWithlink_" + dockerRule.getKind();
    String containerName2 = "container2Withlink_" + dockerRule.getKind();

    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999")
            .withName(containerName1).exec();
    LOG.info("Created container1 {}", container1.toString());
    assertThat(container1.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();
    LOG.info("Container1 Inspect: {}", inspectContainerResponse1.toString());
    assertThat(inspectContainerResponse1.getState().getRunning(), is(true));

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withName(containerName2)
            .withCmd("env")
            .withHostConfig(newHostConfig()
                    .withLinks(new Link(containerName1, "container1Link")))
            .exec();
    LOG.info("Created container {}", container2.toString());
    assertThat(container2.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(containerName1,
            "container1Link")}));
}
 
Example #5
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
@CheckForNull
@JsonIgnore
default Link[] getLinks() {
    return getHostConfig().getLinks();
}
 
Example #6
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 5 votes vote down vote up
/**
 * Add link to another container.
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
default CreateContainerCmd withLinks(Link... links) {
    requireNonNull(links, "links was not specified");
    getHostConfig().setLinks(links);
    return this;
}
 
Example #7
Source File: DeploymentSystemTest.java    From logstash with Apache License 2.0 4 votes vote down vote up
@Test
public void willForwardDataToElasticsearchInDockerMode() throws Exception {
    final ElasticsearchContainer elasticsearchInstance = new ElasticsearchContainer(dockerClient);
    cluster.addAndStartContainer(elasticsearchInstance);

    Client elasticsearchClient = elasticsearchInstance.createClient();

    deployScheduler("logstash", elasticsearchInstance.getIpAddress() + ":9200", true, null, true);

    final String sysLogPort = "514";
    final String randomLogLine = "Hello " + RandomStringUtils.randomAlphanumeric(32);

    dockerClient.pullImageCmd("ubuntu:15.10").exec(new PullImageResultCallback()).awaitSuccess();
    final String logstashSlave = dockerClient.listContainersCmd().withSince(cluster.getAgents().get(0).getContainerId()).exec().stream().filter(container -> container.getImage().endsWith("/logstash-executor:latest")).findFirst().map(Container::getId).orElseThrow(() -> new AssertionError("Unable to find logstash container"));

    assertTrue("logstash slave is expected to be running", dockerClient.inspectContainerCmd(logstashSlave).exec().getState().isRunning());

    final CreateContainerResponse loggerContainer = dockerClient.createContainerCmd("ubuntu:15.10").withLinks(new Link(logstashSlave, "logstash")).withCmd("logger", "--server=logstash", "--port=" + sysLogPort, "--udp", "--rfc3164", randomLogLine).exec();
    dockerClient.startContainerCmd(loggerContainer.getId()).exec();

    await().atMost(5, SECONDS).pollDelay(1, SECONDS).until(() -> {
        final String finishedAt = dockerClient.inspectContainerCmd(loggerContainer.getId()).exec().getState().getFinishedAt();
        assertNotEquals("", finishedAt.trim());
        assertNotEquals("0001-01-01T00:00:00Z", finishedAt);
    });

    final int exitCode = dockerClient.inspectContainerCmd(loggerContainer.getId()).exec().getState().getExitCode();
    dockerClient.removeContainerCmd(loggerContainer.getId()).exec();
    assertEquals(0, exitCode);

    await().atMost(10, SECONDS).pollDelay(1, SECONDS).until(() -> {
        final SearchHits hits = elasticsearchClient.prepareSearch("logstash-*").setQuery(QueryBuilders.simpleQueryStringQuery("Hello")).addField("message").addField("mesos_agent_id").execute().actionGet().getHits();
        assertEquals(1, hits.totalHits());
        Map<String, SearchHitField> fields = hits.getAt(0).fields();

        String esMessage = fields.get("message").getValue();
        assertEquals(randomLogLine, esMessage.trim());

        String esMesosSlaveId = fields.get("mesos_agent_id").getValue();

        String trueSlaveId;
        try {
            trueSlaveId = cluster.getClusterStateInfo().getJSONArray("slaves").getJSONObject(0).getString("id");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        assertEquals(trueSlaveId, esMesosSlaveId.trim());
    });
}
 
Example #8
Source File: DeploymentSystemTest.java    From logstash with Apache License 2.0 4 votes vote down vote up
@Test
public void willForwardDataToElasticsearchInJarMode() throws Exception {
    final ElasticsearchContainer elasticsearchInstance = new ElasticsearchContainer(dockerClient);
    cluster.addAndStartContainer(elasticsearchInstance);

    Client elasticsearchClient = elasticsearchInstance.createClient();

    deployScheduler("logstash", elasticsearchInstance.getIpAddress() + ":9200", false, null, true);

    final String sysLogPort = "514";
    final String randomLogLine = "Hello " + RandomStringUtils.randomAlphanumeric(32);

    dockerClient.pullImageCmd("ubuntu:15.10").exec(new PullImageResultCallback()).awaitSuccess();
    final String logstashSlave = cluster.getAgents().get(0).getContainerId();

    assertTrue(dockerClient.inspectContainerCmd(logstashSlave).exec().getState().isRunning());

    final CreateContainerResponse loggerContainer = dockerClient.createContainerCmd("ubuntu:15.10").withLinks(new Link(logstashSlave, "logstash")).withCmd("logger", "--server=logstash", "--port=" + sysLogPort, "--udp", "--rfc3164", randomLogLine).exec();
    dockerClient.startContainerCmd(loggerContainer.getId()).exec();

    await().atMost(5, SECONDS).pollDelay(1, SECONDS).until(() -> {
        final String finishedAt = dockerClient.inspectContainerCmd(loggerContainer.getId()).exec().getState().getFinishedAt();
        assertNotEquals("", finishedAt.trim());
        assertNotEquals("0001-01-01T00:00:00Z", finishedAt);
    });


    final int exitCode = dockerClient.inspectContainerCmd(loggerContainer.getId()).exec().getState().getExitCode();
    dockerClient.removeContainerCmd(loggerContainer.getId()).exec();
    assertEquals(0, exitCode);

    await().atMost(10, SECONDS).pollDelay(1, SECONDS).until(() -> {
        final SearchHits hits = elasticsearchClient.prepareSearch("logstash-*").setQuery(QueryBuilders.simpleQueryStringQuery("Hello")).addField("message").addField("mesos_agent_id").execute().actionGet().getHits();
        assertEquals(1, hits.totalHits());
        Map<String, SearchHitField> fields = hits.getAt(0).fields();

        String esMessage = fields.get("message").getValue();
        assertEquals(randomLogLine, esMessage.trim());

        String esMesosSlaveId = fields.get("mesos_agent_id").getValue();

        String trueSlaveId;
        try {
            trueSlaveId = cluster.getClusterStateInfo().getJSONArray("slaves").getJSONObject(0).getString("id");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        assertEquals(trueSlaveId, esMesosSlaveId.trim());
    });
}
 
Example #9
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void startContainerWithLinkingDeprecated() throws DockerException {
    String container1Name = "containerWithLink1" + dockerRule.getKind();
    String container2Name = "containerWithLink2" + dockerRule.getKind();
    dockerRule.ensureContainerRemoved(container1Name);
    dockerRule.ensureContainerRemoved(container2Name);

    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd("busybox")
            .withCmd("sleep", "9999")
            .withName(container1Name)
            .exec();

    LOG.info("Created container1 {}", container1.toString());
    assertThat(container1.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();
    LOG.info("Container1 Inspect: {}", inspectContainerResponse1.toString());

    assertThat(inspectContainerResponse1.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getId(), startsWith(container1.getId()));
    assertThat(inspectContainerResponse1.getName(), equalTo("/" + container1Name));
    assertThat(inspectContainerResponse1.getImageId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getState(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getState().getRunning(), is(true));

    if (!inspectContainerResponse1.getState().getRunning()) {
        assertThat(inspectContainerResponse1.getState().getExitCode(), is(equalTo(0)));
    }

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999")
            .withName(container2Name).withHostConfig(newHostConfig()
                    .withLinks(new Link(container1Name, container1Name + "Link")))
            .exec();

    LOG.info("Created container2 {}", container2.toString());
    assertThat(container2.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container2.getId()).exec();

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();
    LOG.info("Container2 Inspect: {}", inspectContainerResponse2.toString());

    assertThat(inspectContainerResponse2.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse2.getHostConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(container1Name,
            container1Name + "Link")}));
    assertThat(inspectContainerResponse2.getId(), startsWith(container2.getId()));
    assertThat(inspectContainerResponse2.getName(), equalTo("/" + container2Name));
    assertThat(inspectContainerResponse2.getImageId(), not(is(emptyString())));
    assertThat(inspectContainerResponse2.getState(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getState().getRunning(), is(true));

}
 
Example #10
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void startContainerWithLinking() throws DockerException {
    String container1Name = "containerWithLinking1" + dockerRule.getKind();
    String container2Name = "containerWithLinking2" + dockerRule.getKind();
    dockerRule.ensureContainerRemoved(container1Name);
    dockerRule.ensureContainerRemoved(container2Name);

    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd("busybox")
            .withCmd("sleep", "9999")
            .withName(container1Name)
            .exec();

    LOG.info("Created container1 {}", container1.toString());
    assertThat(container1.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();
    LOG.info("Container1 Inspect: {}", inspectContainerResponse1.toString());

    assertThat(inspectContainerResponse1.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getId(), startsWith(container1.getId()));
    assertThat(inspectContainerResponse1.getName(), equalTo("/" + container1Name));
    assertThat(inspectContainerResponse1.getImageId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getState(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getState().getRunning(), is(true));

    if (!inspectContainerResponse1.getState().getRunning()) {
        assertThat(inspectContainerResponse1.getState().getExitCode(), is(equalTo(0)));
    }

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999")
            .withName(container2Name).withHostConfig(newHostConfig()
                    .withLinks(new Link(container1Name, container1Name + "Link")))
            .exec();

    LOG.info("Created container2 {}", container2.toString());
    assertThat(container2.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container2.getId()).exec();

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();
    LOG.info("Container2 Inspect: {}", inspectContainerResponse2.toString());

    assertThat(inspectContainerResponse2.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse2.getHostConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(container1Name,
            container1Name + "Link")}));
    assertThat(inspectContainerResponse2.getId(), startsWith(container2.getId()));
    assertThat(inspectContainerResponse2.getName(), equalTo("/" + container2Name));
    assertThat(inspectContainerResponse2.getImageId(), not(is(emptyString())));
    assertThat(inspectContainerResponse2.getState(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getState().getRunning(), is(true));

}
 
Example #11
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void createContainerWithLinkInCustomNetwork() throws DockerException {
    String containerName1 = "containerCustomlink_" + dockerRule.getKind();
    String containerName2 = "containerCustom2link_" + dockerRule.getKind();
    String networkName = "linkNetcustom" + dockerRule.getKind();

    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd()
            .withName(networkName)
            .withDriver("bridge")
            .exec();

    assertNotNull(createNetworkResponse.getId());

    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(newHostConfig()
                    .withNetworkMode(networkName))
            .withCmd("sleep", "9999")
            .withName(containerName1)
            .exec();

    assertThat(container1.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();
    LOG.info("Container1 Inspect: {}", inspectContainerResponse1.toString());
    assertThat(inspectContainerResponse1.getState().getRunning(), is(true));

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(newHostConfig()
                    .withLinks(new Link(containerName1, containerName1 + "Link"))
                    .withNetworkMode(networkName))
            .withName(containerName2)
            .withCmd("env")
            .exec();

    LOG.info("Created container {}", container2.toString());
    assertThat(container2.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();

    ContainerNetwork linkNet = inspectContainerResponse2.getNetworkSettings().getNetworks().get(networkName);
    assertNotNull(linkNet);
    assertThat(linkNet.getLinks(), equalTo(new Link[]{new Link(containerName1, containerName1 + "Link")}));
}
 
Example #12
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void createContainerWithLinking() throws DockerException {
    String containerName1 = "containerWithlinking_" + dockerRule.getKind();
    String containerName2 = "container2Withlinking_" + dockerRule.getKind();

    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withCmd("sleep", "9999")
            .withName(containerName1).exec();

    LOG.info("Created container1 {}", container1.toString());
    assertThat(container1.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();
    LOG.info("Container1 Inspect: {}", inspectContainerResponse1.toString());

    assertThat(inspectContainerResponse1.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getId(), startsWith(container1.getId()));
    assertThat(inspectContainerResponse1.getName(), equalTo("/" + containerName1));
    assertThat(inspectContainerResponse1.getImageId(), not(is(emptyString())));
    assertThat(inspectContainerResponse1.getState(), is(notNullValue()));
    assertThat(inspectContainerResponse1.getState().getRunning(), is(true));

    if (!inspectContainerResponse1.getState().getRunning()) {
        assertThat(inspectContainerResponse1.getState().getExitCode(), is(equalTo(0)));
    }

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999")
            .withName(containerName2)
            .withHostConfig(newHostConfig()
                    .withLinks(new Link(containerName1, containerName1 + "Link")))
            .exec();

    LOG.info("Created container2 {}", container2.toString());
    assertThat(container2.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();
    LOG.info("Container2 Inspect: {}", inspectContainerResponse2.toString());

    assertThat(inspectContainerResponse2.getConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getId(), not(is(emptyString())));
    assertThat(inspectContainerResponse2.getHostConfig(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), is(notNullValue()));
    assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(containerName1,
            containerName1 + "Link")}));
    assertThat(inspectContainerResponse2.getId(), startsWith(container2.getId()));
    assertThat(inspectContainerResponse2.getName(), equalTo("/" + containerName2));
    assertThat(inspectContainerResponse2.getImageId(), not(is(emptyString())));

}
 
Example #13
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 4 votes vote down vote up
/**
 * Add link to another container.
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
default CreateContainerCmd withLinks(List<Link> links) {
    requireNonNull(links, "links was not specified");
    return withLinks(links.toArray(new Link[links.size()]));
}