Search in sources :

Example 6 with InspectContainerResponse

use of com.github.dockerjava.api.command.InspectContainerResponse in project testcontainers-java by testcontainers.

the class ContainerState method getMappedPort.

/**
 * Get the actual mapped port for a given port exposed by the container.
 *
 * @param originalPort the original TCP port that is exposed
 * @return the port that the exposed port is mapped to, or null if it is not exposed
 */
default Integer getMappedPort(int originalPort) {
    Preconditions.checkState(this.getContainerId() != null, "Mapped port can only be obtained after the container is started");
    Ports.Binding[] binding = new Ports.Binding[0];
    final InspectContainerResponse containerInfo = this.getContainerInfo();
    if (containerInfo != null) {
        binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort));
    }
    if (binding != null && binding.length > 0 && binding[0] != null) {
        return Integer.valueOf(binding[0].getHostPortSpec());
    } else {
        throw new IllegalArgumentException("Requested port (" + originalPort + ") is not mapped");
    }
}
Also used : PortBinding(com.github.dockerjava.api.model.PortBinding) InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) ExposedPort(com.github.dockerjava.api.model.ExposedPort)

Example 7 with InspectContainerResponse

use of com.github.dockerjava.api.command.InspectContainerResponse in project testcontainers-java by testcontainers.

the class ContainerState method isHealthy.

/**
 * @return has the container health state 'healthy'?
 */
default boolean isHealthy() {
    if (getContainerId() == null) {
        return false;
    }
    try {
        InspectContainerResponse inspectContainerResponse = getCurrentContainerInfo();
        String healthStatus = inspectContainerResponse.getState().getHealth().getStatus();
        return healthStatus.equals(STATE_HEALTHY);
    } catch (DockerException e) {
        return false;
    }
}
Also used : InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) DockerException(com.github.dockerjava.api.exception.DockerException)

Aggregations

InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)7 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)3 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)3 IOException (java.io.IOException)3 DockerClient (com.github.dockerjava.api.DockerClient)2 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)2 DockerException (com.github.dockerjava.api.exception.DockerException)2 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)2 ExposedPort (com.github.dockerjava.api.model.ExposedPort)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 Bind (com.github.dockerjava.api.model.Bind)1 HostConfig (com.github.dockerjava.api.model.HostConfig)1 PortBinding (com.github.dockerjava.api.model.PortBinding)1 Volume (com.github.dockerjava.api.model.Volume)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1