3737import java .nio .file .Paths ;
3838import java .text .DateFormat ;
3939import java .text .SimpleDateFormat ;
40+ import java .util .Arrays ;
4041import java .util .Date ;
4142import java .util .HashMap ;
4243import java .util .Map ;
44+ import java .util .Optional ;
45+ import java .util .regex .Matcher ;
46+ import java .util .regex .Pattern ;
4347
4448import org .apache .commons .io .FilenameUtils ;
49+ import org .apache .commons .lang3 .StringUtils ;
4550import org .eclipse .jface .dialogs .IDialogConstants ;
4651import org .eclipse .jface .dialogs .MessageDialog ;
4752import org .eclipse .swt .SWT ;
5661import org .eclipse .swt .widgets .Text ;
5762
5863import com .microsoft .azuretools .azurecommons .exceptions .InvalidFormDataException ;
64+ import com .microsoft .azuretools .azurecommons .helpers .NotNull ;
5965import com .microsoft .azuretools .azurecommons .helpers .Nullable ;
6066import com .microsoft .azuretools .azurecommons .util .Utils ;
6167import com .microsoft .azuretools .container .ConsoleLogger ;
@@ -104,6 +110,11 @@ public class DockerRunDialog extends AzureTitleAreaDialogWrapper {
104110 private static final String IMAGE_NAME_PREFIX = "localimage" ;
105111 private static final String DEFAULT_TAG_NAME = "latest" ;
106112 private static final String SELECT_DOCKER_FILE = "Browse..." ;
113+ private static final String WAR = "war" ;
114+ private static final String DEFAULT_TOMCAT_SERVICE_PORT = "80" ;
115+ private static final String DEFAULT_SPRING_BOOT_SERVICE_PORT = "8080" ;
116+
117+ private static final Pattern PORT_PATTERN = Pattern .compile ("EXPOSE\\ s+(\\ d+).*" );
107118
108119 private DockerHostRunSetting dataModel ;
109120 private Text txtDockerHost ;
@@ -350,9 +361,10 @@ private void execute() {
350361 targetDockerfile .getFileName ().toString (), new DockerProgressHandler ());
351362
352363 // create a container
364+ final String containerServerPort = StringUtils .firstNonEmpty (getPortFromDockerfile (content ), getPortByArtifact (targetFilePath ));
353365 ConsoleLogger .info (Constant .MESSAGE_CREATING_CONTAINER );
354366 String containerId = DockerUtil .createContainer (docker ,
355- String .format ("%s:%s" , dataModel .getImageName (), dataModel .getTagName ()));
367+ String .format ("%s:%s" , dataModel .getImageName (), dataModel .getTagName ()), containerServerPort );
356368 ConsoleLogger .info (String .format (Constant .MESSAGE_CONTAINER_INFO , containerId ));
357369
358370 // start container
@@ -366,7 +378,7 @@ private void execute() {
366378 String publicPort = null ;
367379 if (ports != null ) {
368380 for (Container .PortMapping portMapping : ports ) {
369- if (Constant . TOMCAT_SERVICE_PORT . equals ( String .valueOf (portMapping .privatePort ()))) {
381+ if (StringUtils . equalsIgnoreCase ( containerServerPort , String .valueOf (portMapping .privatePort ()))) {
370382 publicPort = String .valueOf (portMapping .publicPort ());
371383 }
372384 }
@@ -409,4 +421,17 @@ private void sendTelemetry(boolean success, @Nullable String errorMsg) {
409421 private void showErrorMessage (String title , String message ) {
410422 MessageDialog .openError (this .getShell (), title , message );
411423 }
424+
425+ private String getPortFromDockerfile (@ NotNull String dockerFileContent ) {
426+ final Matcher result = Arrays .stream (dockerFileContent .split ("\\ R+" ))
427+ .map (value -> PORT_PATTERN .matcher (value ))
428+ .filter (Matcher ::matches )
429+ .findFirst ().orElse (null );
430+ return Optional .ofNullable (result ).map (matcher -> matcher .group (1 )).orElse (null );
431+ }
432+
433+ private String getPortByArtifact (@ NotNull String targetFilePath ) {
434+ final String fileExtension = FilenameUtils .getExtension (targetFilePath );
435+ return StringUtils .equalsIgnoreCase (fileExtension , WAR ) ? DEFAULT_TOMCAT_SERVICE_PORT : DEFAULT_SPRING_BOOT_SERVICE_PORT ;
436+ }
412437}
0 commit comments