Java Code Examples for com.github.dockerjava.api.model.Ports#bind()

The following examples show how to use com.github.dockerjava.api.model.Ports#bind() . 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: DockerClientJavaApi.java    From doclipser with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void defaultRunCommand(String eclipseProjectName, String dockerBuildContext) {
    ExposedPort tcp8080 = ExposedPort.tcp(8080);

    CreateContainerResponse container = dockerClient.createContainerCmd("mariolet/my-tomcat")
       .withCmd("true")
       .withExposedPorts(tcp8080)
       .exec();

    Ports portBindings = new Ports();
    portBindings.bind(tcp8080, Ports.Binding(80));

    dockerClient.startContainerCmd(container.getId())
       .withPortBindings(portBindings)
       .exec();
}
 
Example 2
Source File: MesosMasterContainer.java    From minimesos with Apache License 2.0 6 votes vote down vote up
@Override
protected CreateContainerCmd dockerCommand() {
    int port = getServicePort();
    ExposedPort exposedPort = ExposedPort.tcp(port);

    Ports portBindings = new Ports();
    if (getCluster().isMapPortsToHost()) {
        portBindings.bind(exposedPort, Ports.Binding.bindPort(port));
    }

    CreateContainerCmd cmd = DockerClientFactory.build().createContainerCmd(getImageName() + ":" + getImageTag())
        .withName(getName())
        .withExposedPorts(new ExposedPort(getServicePort()))
        .withEnv(newEnvironment()
            .withValues(getMesosMasterEnvVars())
            .withValues(getSharedEnvVars())
            .createEnvironment())
        .withPortBindings(portBindings);

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

    return cmd;
}
 
Example 3
Source File: ConsulContainer.java    From minimesos with Apache License 2.0 6 votes vote down vote up
@Override
protected CreateContainerCmd dockerCommand() {
    int port = getServicePort();
    ExposedPort exposedPort = ExposedPort.tcp(port);

    Ports portBindings = new Ports();
    if (getCluster().isMapPortsToHost()) {
        portBindings.bind(exposedPort, Ports.Binding.bindPort(port));
    }

    ExposedPort consulHTTPPort = ExposedPort.tcp(ConsulConfig.CONSUL_HTTP_PORT);
    ExposedPort consulDNSPort = ExposedPort.udp(ConsulConfig.CONSUL_DNS_PORT);

    return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag())
            .withName(getName())
            .withCmd("agent", "-server", "-bootstrap", "-client", "0.0.0.0")
            .withExposedPorts(consulHTTPPort, consulDNSPort)
            .withPortBindings(portBindings);
}
 
Example 4
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void startContainerWithConflictingPortBindings() throws DockerException {

    ExposedPort tcp22 = ExposedPort.tcp(22);
    ExposedPort tcp23 = ExposedPort.tcp(23);

    Ports portBindings = new Ports();
    portBindings.bind(tcp22, Binding.bindPort(11022));
    portBindings.bind(tcp23, Binding.bindPort(11022));

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true")
            .withExposedPorts(tcp22, tcp23).withHostConfig(newHostConfig()
                    .withPortBindings(portBindings))
            .exec();

    LOG.info("Created container {}", container.toString());

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

    dockerRule.getClient().startContainerCmd(container.getId()).exec();
}
 
Example 5
Source File: DefaultDockerClient.java    From junit5-docker with Apache License 2.0 5 votes vote down vote up
private Ports createPortBindings(PortBinding... portBinding) {
    Ports bindings = new Ports();
    for (PortBinding binding : portBinding) {
        ExposedPort inner = tcp(binding.inner);
        bindings.bind(inner, bindPort(binding.exposed));
    }
    return bindings;
}
 
Example 6
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Override
protected CreateContainerCmd dockerCommand() {
    ExposedPort exposedPort = ExposedPort.tcp(MARATHON_PORT);
    Ports portBindings = new Ports();
    if (getCluster().isMapPortsToHost()) {
        portBindings.bind(exposedPort, Ports.Binding.bindPort(MARATHON_PORT));
    }
    return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag())
            .withName(getName())
            .withExtraHosts("minimesos-zookeeper:" + this.zooKeeper.getIpAddress())
            .withCmd(CollectionsUtils.splitCmd(config.getCmd()))
            .withExposedPorts(exposedPort)
            .withPortBindings(portBindings);
}
 
Example 7
Source File: ZooKeeperContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Override
protected CreateContainerCmd dockerCommand() {
    int port = getServicePort();
    ExposedPort exposedPort = ExposedPort.tcp(port);

    Ports portBindings = new Ports();
    if (getCluster().isMapPortsToHost()) {
        portBindings.bind(exposedPort, Ports.Binding.bindPort(port));
    }

    return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag())
        .withName(getName())
        .withExposedPorts(new ExposedPort(ZooKeeperConfig.DEFAULT_ZOOKEEPER_PORT), new ExposedPort(2888), new ExposedPort(3888))
        .withPortBindings(portBindings);
}
 
Example 8
Source File: KuduTestResource.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> start() {
    LOG.info(TestcontainersConfiguration.getInstance().toString());

    try {
        Network kuduNetwork = Network.newNetwork();

        // Setup the Kudu master server container
        masterContainer = new GenericContainer(KUDU_IMAGE).withCommand("master")
                .withExposedPorts(KUDU_MASTER_RPC_PORT, KUDU_MASTER_HTTP_PORT).withNetwork(kuduNetwork)
                .withNetworkAliases(KUDU_MASTER_NETWORK_ALIAS);
        masterContainer = masterContainer.withLogConsumer(new Slf4jLogConsumer(LOG)).waitingFor(Wait.forListeningPort());
        masterContainer.start();

        // Force host name and port, so that the tablet container is accessible from KuduResource, KuduTest and KuduIT.
        Consumer<CreateContainerCmd> consumer = cmd -> {
            Ports portBindings = new Ports();
            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_RPC_PORT), Ports.Binding.bindPort(KUDU_TABLET_RPC_PORT));
            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_HTTP_PORT), Ports.Binding.bindPort(KUDU_TABLET_HTTP_PORT));
            HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(portBindings)
                    .withNetworkMode(kuduNetwork.getId());
            cmd.withHostName(KUDU_TABLET_NETWORK_ALIAS).withHostConfig(hostConfig);
        };

        // Setup the Kudu tablet server container
        tabletContainer = new GenericContainer(KUDU_IMAGE).withCommand("tserver")
                .withEnv("KUDU_MASTERS", KUDU_MASTER_NETWORK_ALIAS)
                .withExposedPorts(KUDU_TABLET_RPC_PORT, KUDU_TABLET_HTTP_PORT).withNetwork(kuduNetwork)
                .withNetworkAliases(KUDU_TABLET_NETWORK_ALIAS).withCreateContainerCmdModifier(consumer);
        tabletContainer = tabletContainer.withLogConsumer(new Slf4jLogConsumer(LOG)).waitingFor(Wait.forListeningPort());
        tabletContainer.start();

        // Print interesting Kudu servers connectivity information
        final String masterRpcAuthority = masterContainer.getContainerIpAddress() + ":"
                + masterContainer.getMappedPort(KUDU_MASTER_RPC_PORT);
        LOG.info("Kudu master RPC accessible at " + masterRpcAuthority);
        final String masterHttpAuthority = masterContainer.getContainerIpAddress() + ":"
                + masterContainer.getMappedPort(KUDU_MASTER_HTTP_PORT);
        LOG.info("Kudu master HTTP accessible at " + masterHttpAuthority);
        final String tServerRpcAuthority = tabletContainer.getContainerIpAddress() + ":"
                + tabletContainer.getMappedPort(KUDU_TABLET_RPC_PORT);
        LOG.info("Kudu tablet server RPC accessible at " + tServerRpcAuthority);
        final String tServerHttpAuthority = tabletContainer.getContainerIpAddress() + ":"
                + tabletContainer.getMappedPort(KUDU_TABLET_HTTP_PORT);
        LOG.info("Kudu tablet server HTTP accessible at " + tServerHttpAuthority);

        return CollectionHelper.mapOf(KUDU_AUTHORITY_CONFIG_KEY, masterRpcAuthority);
    } catch (Exception ex) {
        LOG.error("Issue starting KuduTestResource, please have a look at KuduInfrastructureTestHelper", ex);
        return CollectionHelper.mapOf(KUDU_AUTHORITY_CONFIG_KEY,
                "Please_have_a_look_at_KuduInfrastructureTestHelper");
    }
}
 
Example 9
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 3 votes vote down vote up
@Test
public void startContainerWithRandomPortBindings() throws DockerException {

    ExposedPort tcp22 = ExposedPort.tcp(22);
    ExposedPort tcp23 = ExposedPort.tcp(23);

    Ports portBindings = new Ports();
    portBindings.bind(tcp22, Binding.empty());
    portBindings.bind(tcp23, Binding.empty());

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999")
            .withExposedPorts(tcp22, tcp23).withHostConfig(newHostConfig()
                    .withPortBindings(portBindings)
                    .withPublishAllPorts(true))
            .exec();

    LOG.info("Created container {}", container.toString());

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

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

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

    assertThat(Arrays.asList(inspectContainerResponse.getConfig().getExposedPorts()), contains(tcp22, tcp23));

    assertThat(inspectContainerResponse.getNetworkSettings().getPorts().getBindings().get(tcp22)[0].getHostPortSpec(),
            is(not(equalTo(String.valueOf(tcp22.getPort())))));

    assertThat(inspectContainerResponse.getNetworkSettings().getPorts().getBindings().get(tcp23)[0].getHostPortSpec(),
            is(not(equalTo(String.valueOf(tcp23.getPort())))));

}