use of org.guvnor.ala.docker.model.DockerRuntimeEndpoint in project kie-wb-common by kiegroup.
the class DockerRuntimeExecExecutor method create.
private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
if (runtimeConfig.isPull()) {
try {
LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
}
}
final String[] ports = { runtimeConfig.getPort() };
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
if (!_dockerProvider.isPresent()) {
return Optional.empty();
}
final DockerProvider dockerProvider = _dockerProvider.get();
final List<PortBinding> randomPort = new ArrayList<>();
final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
randomPort.add(randomPortBinding);
portBindings.put(runtimeConfig.getPort(), randomPort);
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
final ContainerCreation creation;
try {
creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
}
final String id = creation.id();
String shortId = id.substring(0, 12);
String host = "";
try {
docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
} catch (DockerException | InterruptedException ex) {
throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
}
DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
dockerRuntimeEndpoint.setHost(host);
dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
dockerRuntimeEndpoint.setContext("");
return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
Aggregations