Search in sources :

Example 1 with Ports

use of com.github.dockerjava.api.model.Ports in project dockerunit by qzagarese.

the class ConsulDescriptor method setup.

@ContainerBuilder
public CreateContainerCmd setup(CreateContainerCmd cmd) {
    List<ExposedPort> ports = new ArrayList<>(Arrays.asList(cmd.getExposedPorts()));
    ExposedPort dnsPort = ExposedPort.udp(CONSUL_DNS_PORT);
    ports.add(dnsPort);
    ExposedPort consulPort = ExposedPort.tcp(CONSUL_PORT);
    ports.add(consulPort);
    Ports bindings = cmd.getPortBindings();
    if (bindings == null) {
        bindings = new Ports();
    }
    bindings.bind(dnsPort, Binding.bindPort(8600));
    bindings.bind(consulPort, Binding.bindPort(8500));
    return cmd.withExposedPorts(ports).withPortBindings(bindings).withCmd("agent", "-dev", "-client=0.0.0.0", "-enable-script-checks");
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports) ContainerBuilder(com.github.qzagarese.dockerunit.annotation.ContainerBuilder)

Example 2 with Ports

use of com.github.dockerjava.api.model.Ports in project dockerunit by qzagarese.

the class PortBindingExtensionInterpreter method build.

@Override
public CreateContainerCmd build(TestDescriptor td, CreateContainerCmd cmd, PortBinding pb) {
    List<ExposedPort> ports = new ArrayList<>(Arrays.asList(cmd.getExposedPorts()));
    ExposedPort containerPort = pb.protocol().equals(Protocol.TCP) ? ExposedPort.tcp(pb.exposedPort()) : ExposedPort.udp(pb.exposedPort());
    ports.add(containerPort);
    Ports bindings = cmd.getPortBindings();
    if (bindings == null) {
        bindings = new Ports();
    }
    bindings.bind(containerPort, pb.hostIp().isEmpty() ? Binding.bindPort(pb.hostPort()) : Binding.bindIpAndPort(pb.hostIp(), pb.hostPort()));
    return cmd.withExposedPorts(ports).withPortBindings(bindings);
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports)

Example 3 with Ports

use of com.github.dockerjava.api.model.Ports in project testcontainers-java by testcontainers.

the class ContainerState method getPortBindings.

/**
 * @return the port bindings
 */
default List<String> getPortBindings() {
    List<String> portBindings = new ArrayList<>();
    final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings();
    for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) {
        for (Ports.Binding portBinding : binding.getValue()) {
            portBindings.add(String.format("%s:%s", portBinding.toString(), binding.getKey()));
        }
    }
    return portBindings;
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports) Map(java.util.Map)

Example 4 with Ports

use of com.github.dockerjava.api.model.Ports in project elastest-torm by elastest.

the class DockerService2 method bindingPort.

public SocatBindedPort bindingPort(String containerIp, String port, String networkName) throws Exception {
    DockerClient dockerClient = this.getDockerClient();
    int listenPort = 37000;
    String bindedPort = null;
    try {
        listenPort = utilTools.findRandomOpenPort();
        List<String> envVariables = new ArrayList<>();
        envVariables.add("LISTEN_PORT=" + listenPort);
        envVariables.add("FORWARD_PORT=" + port);
        envVariables.add("TARGET_SERVICE_IP=" + containerIp);
        Ports portBindings = new Ports();
        ExposedPort exposedListenPort = ExposedPort.tcp(listenPort);
        portBindings.bind(exposedListenPort, Ports.Binding.bindPort(listenPort));
        bindedPort = this.runDockerContainer(dockerClient, etSocatImage, envVariables, "container" + listenPort, networkName, portBindings, listenPort);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());
    }
    SocatBindedPort bindedPortObj = new SocatBindedPort(Integer.toString(listenPort), bindedPort);
    return bindedPortObj;
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) DockerClient(com.github.dockerjava.api.DockerClient) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports) SocatBindedPort(io.elastest.etm.model.SocatBindedPort) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ExposedPort (com.github.dockerjava.api.model.ExposedPort)4 Ports (com.github.dockerjava.api.model.Ports)4 ArrayList (java.util.ArrayList)4 DockerClient (com.github.dockerjava.api.DockerClient)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1 ContainerBuilder (com.github.qzagarese.dockerunit.annotation.ContainerBuilder)1 SocatBindedPort (io.elastest.etm.model.SocatBindedPort)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXException (org.xml.sax.SAXException)1