use of com.spotify.docker.client.messages.Container.PortMapping in project azure-tools-for-java by Microsoft.
the class DockerRunDialog method execute.
private void execute() {
Operation operation = TelemetryManager.createOperation(WEBAPP, DEPLOY_WEBAPP_DOCKERLOCAL);
Observable.fromCallable(() -> {
operation.start();
ConsoleLogger.info("Starting job ... ");
if (basePath == null) {
ConsoleLogger.error("Project base path is null.");
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
String targetFilePath = dataModel.getTargetPath();
ConsoleLogger.info(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
ConsoleLogger.info(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
String imageNameWithTag = String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName());
ConsoleLogger.info(String.format("Building image ... [%s]", imageNameWithTag));
DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
DockerUtil.ping(docker);
DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler());
// create a container
final String containerServerPort = StringUtils.firstNonEmpty(getPortFromDockerfile(content), getPortByArtifact(targetFilePath));
ConsoleLogger.info(Constant.MESSAGE_CREATING_CONTAINER);
String containerId = DockerUtil.createContainer(docker, String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName()), containerServerPort);
ConsoleLogger.info(String.format(Constant.MESSAGE_CONTAINER_INFO, containerId));
// start container
ConsoleLogger.info(Constant.MESSAGE_STARTING_CONTAINER);
Container container = DockerUtil.runContainer(docker, containerId);
DockerRuntime.getInstance().setRunningContainerId(basePath, container.id(), dataModel);
// props
String hostname = new URI(dataModel.getDockerHost()).getHost();
ImmutableList<PortMapping> ports = container.ports();
String publicPort = null;
if (ports != null) {
for (Container.PortMapping portMapping : ports) {
if (StringUtils.equalsIgnoreCase(containerServerPort, String.valueOf(portMapping.privatePort()))) {
publicPort = String.valueOf(portMapping.publicPort());
}
}
}
ConsoleLogger.info(String.format(Constant.MESSAGE_CONTAINER_STARTED, (hostname != null ? hostname : "localhost") + (publicPort != null ? ":" + publicPort : "")));
return null;
}).subscribeOn(SchedulerProviderFactory.getInstance().getSchedulerProvider().io()).subscribe(ret -> {
ConsoleLogger.info("Container started.");
sendTelemetry(true, null);
operation.complete();
}, e -> {
e.printStackTrace();
ConsoleLogger.error(e.getMessage());
sendTelemetry(false, e.getMessage());
EventUtil.logError(operation, ErrorType.systemError, new Exception(e), null, null);
operation.complete();
});
}
Aggregations