com.github.dockerjava.api.command.CreateNetworkResponse Java Examples

The following examples show how to use com.github.dockerjava.api.command.CreateNetworkResponse. 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: RemoveNetworkCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void removeNetwork() throws DockerException {
    assumeNotSwarm("Swarm has no network", dockerRule);

    CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd()
            .withName("test-network")
            .exec();

    LOG.info("Removing network: {}", network.getId());
    dockerRule.getClient().removeNetworkCmd(network.getId()).exec();

    List<Network> networks = dockerRule.getClient().listNetworksCmd().exec();

    Matcher matcher = not(hasItem(hasField("id", startsWith(network.getId()))));
    assertThat(networks, matcher);

}
 
Example #2
Source File: AbstractDockerBasedEnvironment.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
/**
 * Create network which is used as default in docker-compose.yml
 * This should be run before {@link DockerComposeExecutor#run(TestEnvironment)}
 */
@Override
public void createNetwork() {
    removeNetwork();

    String apmNetwork = apmBindAddress.substring(0, apmBindAddress.lastIndexOf(".")) + ".0/24";

    log.info(String.format("Creating network %s:", apmNetwork));

    Network.Ipam ipam = new Network.Ipam()
            .withConfig(new Network.Ipam.Config()
                    .withSubnet(apmNetwork)
                    .withGateway(apmBindAddress));

    CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd()
            .withName(Constants.HOST_ADDED_TO_ETC_HOSTS)
            .withIpam(ipam)
            .exec();

    try {
        network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
    } catch (DockerException ex) {
        log.severe(String.format("Could not create network: %s", createNetworkResponse));
        throw new EnvironmentException("Could not create network: " + createNetworkResponse, ex);
    }
}
 
Example #3
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createContainerWithNetworkID() {
    assumeThat("API version should be >= 1.23", dockerRule, isGreaterOrEqual(VERSION_1_24));

    String networkName = "net-" + UUID.randomUUID().toString();
    Map<String, String> labels = new HashMap<>();
    labels.put("com.example.label", "test");
    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName)
            .withLabels(labels).withAttachable(true).exec();
    String networkId = createNetworkResponse.getId();
    CreateContainerResponse createContainerResponse = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withLabels(labels).withCmd("true").exec();
    String containerId = createContainerResponse.getId();
    dockerRule.getClient().connectToNetworkCmd().withContainerId(containerId).withNetworkId(networkId).exec();
    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(containerId).exec();
    ContainerNetwork containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
    if (containerNetwork == null) {
        // swarm node used network id
        containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkId);
    }
    assertThat(containerNetwork, notNullValue());
}
 
Example #4
Source File: CreateNetworkCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createNetworkWithIpamConfig() throws DockerException {
    assumeNotSwarm("no network in swarm", dockerRule);

    String networkName = "networkIpam" + dockerRule.getKind();
    String subnet = "10.67." + (79 + getFactoryType().ordinal()) + ".0/24";

    Network.Ipam ipam = new Network.Ipam().withConfig(new Network.Ipam.Config().withSubnet(subnet));
    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName).withIpam(ipam).exec();

    assertNotNull(createNetworkResponse.getId());

    Network network = dockerRule.getClient().inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
    assertEquals(network.getName(), networkName);
    assertEquals("bridge", network.getDriver());
    assertEquals(subnet, network.getIpam().getConfig().iterator().next().getSubnet());
}
 
Example #5
Source File: DisconnectFromNetworkCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void forceDisconnectFromNetwork() throws InterruptedException {
    assumeNotSwarm("no network in swarm", dockerRule);

    CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("testNetwork2" + dockerRule.getKind()).exec();

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
            .withHostConfig(newHostConfig()
                    .withNetworkMode("testNetwork2" + dockerRule.getKind()))
            .withCmd("sleep", "9999")
            .exec();

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

    dockerRule.getClient().disconnectFromNetworkCmd()
            .withNetworkId(network.getId())
            .withContainerId(container.getId())
            .withForce(true)
            .exec();

    Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();
    assertFalse(updatedNetwork.getContainers().containsKey(container.getId()));
}
 
Example #6
Source File: NetworkLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCreatingNetwork_thenRemove() throws InterruptedException {

    // when
    CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungDefault").withDriver("bridge").exec();

    // then
    Thread.sleep(4000);
    dockerClient.removeNetworkCmd(networkResponse.getId()).exec();
}
 
Example #7
Source File: NetworkLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCreatingAdvanceNetwork_thenRetrieveResponse() {

    // when
    CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungAdvanced").withIpam(new Ipam().withConfig(new Ipam.Config().withSubnet("172.36.0.0/16").withIpRange("172.36.5.0/24"))).withDriver("bridge").exec();

    // then
    assertThat(networkResponse, is(not(null)));
}
 
Example #8
Source File: NetworkLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCreatingNetwork_thenRetrieveResponse() {

    // when
    CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungDefault").withDriver("bridge").exec();

    // then
    assertThat(networkResponse, is(not(null)));
}
 
Example #9
Source File: CreateNetworkCmdExec.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
protected CreateNetworkResponse execute(CreateNetworkCmd command) {
    WebTarget webResource = getBaseResource().path("/networks/create");

    LOGGER.trace("POST: {}", webResource);
    return webResource.request().accept(MediaType.APPLICATION_JSON)
            .post(command, new TypeReference<CreateNetworkResponse>() {
            });
}
 
Example #10
Source File: CreateNetworkCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createNetworkWithLabel() throws DockerException {
    assumeNotSwarm("no network in swarm?", dockerRule);
    assumeThat("API version should be >= 1.21", dockerRule, isGreaterOrEqual(VERSION_1_21));

    String networkName = "createNetworkWithLabel" + dockerRule.getKind();
    Map<String, String> labels = new HashMap<>();
    labels.put("com.example.usage" + dockerRule.getKind(), "test");
    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName).withLabels(labels).exec();
    assertNotNull(createNetworkResponse.getId());
    Network network = dockerRule.getClient().inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
    assertEquals(network.getLabels(), labels);
}
 
Example #11
Source File: CreateNetworkCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createAttachableNetwork() throws DockerException {
    assumeThat("API version should be > 1.24", dockerRule, isGreaterOrEqual(VERSION_1_25));

    String networkName = "createAttachableNetwork" + dockerRule.getKind();
    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd()
            .withName(networkName)
            .withAttachable(true)
            .exec();
    assertNotNull(createNetworkResponse.getId());
    Network network = dockerRule.getClient().inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
    assertThat(network, notNullValue());
    assertTrue(network.isAttachable());
}
 
Example #12
Source File: CreateNetworkCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createNetwork() throws DockerException {
    assumeNotSwarm("no network in swarm", dockerRule);

    String networkName = "createNetwork" + dockerRule.getKind();

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

    assertNotNull(createNetworkResponse.getId());

    Network network = dockerRule.getClient().inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
    assertThat(network.getName(), is(networkName));
    assertThat(network.getDriver(), is("bridge"));
}
 
Example #13
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createContainerWithAlias() throws DockerException {
    String containerName1 = "containerAlias_" + dockerRule.getKind();
    String networkName = "aliasNet" + dockerRule.getKind();

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

    assertNotNull(createNetworkResponse.getId());

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(newHostConfig()
                    .withNetworkMode(networkName))
            .withCmd("sleep", "9999")
            .withName(containerName1)
            .withAliases("server" + dockerRule.getKind())
            .exec();

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

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

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId())
            .exec();

    ContainerNetwork aliasNet = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
    assertThat(aliasNet.getAliases(), hasItem("server" + dockerRule.getKind()));
}
 
Example #14
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createContainerWithCustomIp() throws DockerException {
    String containerName1 = "containerCustomIplink_" + dockerRule.getKind();
    String networkName = "customIpNet" + dockerRule.getKind();
    String subnetPrefix = getFactoryType().getSubnetPrefix() + "101";

    CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd()
            .withIpam(new Network.Ipam()
                    .withConfig(new Network.Ipam.Config()
                            .withSubnet(subnetPrefix + ".0/24")))
            .withDriver("bridge")
            .withName(networkName)
            .exec();

    assertNotNull(createNetworkResponse.getId());

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(newHostConfig()
                    .withNetworkMode(networkName))
            .withCmd("sleep", "9999")
            .withName(containerName1)
            .withIpv4Address(subnetPrefix + ".100")
            .exec();

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

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

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient()
            .inspectContainerCmd(container.getId())
            .exec();

    ContainerNetwork customIpNet = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
    assertNotNull(customIpNet);
    assertThat(customIpNet.getGateway(), is(subnetPrefix + ".1"));
    assertThat(customIpNet.getIpAddress(), is(subnetPrefix + ".100"));
}
 
Example #15
Source File: ConnectToNetworkCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void connectToNetwork() throws InterruptedException {
    assumeNotSwarm("no network in swarm", dockerRule);
    String networkName = "connectToNetwork" + dockerRule.getKind();

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999").exec();
    dockerRule.getClient().startContainerCmd(container.getId()).exec();

    CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName(networkName).exec();

    dockerRule.getClient().connectToNetworkCmd().withNetworkId(network.getId()).withContainerId(container.getId()).exec();

    Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();

    assertTrue(updatedNetwork.getContainers().containsKey(container.getId()));

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();

    assertNotNull(inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName));
}
 
Example #16
Source File: ConnectToNetworkCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void connectToNetworkWithContainerNetwork() throws InterruptedException {
    assumeNotSwarm("no network in swarm", dockerRule);

    final String subnetPrefix = getFactoryType().getSubnetPrefix() + "100";
    final String networkName = "ContainerWithNetwork" + dockerRule.getKind();
    final String containerIp = subnetPrefix + ".100";

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

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

    try {
        dockerRule.getClient().removeNetworkCmd(networkName).exec();
    } catch (DockerException ignore) {
    }

    CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd()
        .withName(networkName)
        .withIpam(new Network.Ipam()
            .withConfig(new Network.Ipam.Config()
                .withSubnet(subnetPrefix + ".0/24")))
        .exec();

    dockerRule.getClient().connectToNetworkCmd()
        .withNetworkId(network.getId())
        .withContainerId(container.getId())
        .withContainerNetwork(new ContainerNetwork()
            .withAliases("aliasName" + dockerRule.getKind())
            .withIpamConfig(new ContainerNetwork.Ipam()
                .withIpv4Address(containerIp)))
        .exec();

    Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();

    Network.ContainerNetworkConfig containerNetworkConfig = updatedNetwork.getContainers().get(container.getId());
    assertNotNull(containerNetworkConfig);
    assertThat(containerNetworkConfig.getIpv4Address(), is(containerIp + "/24"));

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();

    ContainerNetwork testNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
    assertNotNull(testNetwork);
    assertThat(testNetwork.getAliases(), hasItem("aliasName" + dockerRule.getKind()));
    assertThat(testNetwork.getGateway(), is(subnetPrefix + ".1"));
    assertThat(testNetwork.getIpAddress(), is(containerIp));
}
 
Example #17
Source File: DisconnectFromNetworkCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void disconnectFromNetwork() throws InterruptedException {
    assumeNotSwarm("no network in swarm", dockerRule);

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
    dockerRule.getClient().startContainerCmd(container.getId()).exec();

    CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("disconnectNetwork" + dockerRule.getKind()).exec();

    dockerRule.getClient().connectToNetworkCmd().withNetworkId(network.getId()).withContainerId(container.getId()).exec();

    Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();

    assertTrue(updatedNetwork.getContainers().containsKey(container.getId()));

    dockerRule.getClient().disconnectFromNetworkCmd().withNetworkId(network.getId()).withContainerId(container.getId()).exec();

    updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();

    assertFalse(updatedNetwork.getContainers().containsKey(container.getId()));
}
 
Example #18
Source File: CreateNetworkCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public CreateNetworkCmdImpl(DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> execution) {
    super(execution);
}
 
Example #19
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")}));
}