@@ -156,39 +156,6 @@ private String runWslCommandAsRoot(String command, int timeoutSeconds) {
156156 }
157157 }
158158
159- /**
160- * Run a Windows command with UAC elevation (administrator prompt).
161- * Uses PowerShell {@code Start-Process -Verb RunAs} to trigger the UAC dialog.
162- *
163- * @param command The executable to run elevated
164- * @param arguments Arguments for the command
165- * @param waitForExit Whether to wait for the elevated process to complete
166- * @return true if the elevated process was started (and completed successfully if waitForExit)
167- */
168- private boolean runElevatedWindowsCommand (String command , String arguments , boolean waitForExit ) {
169- try {
170- String psCommand = String .format (
171- "Start-Process '%s' -ArgumentList '%s' -Verb RunAs%s" ,
172- command , arguments , waitForExit ? " -Wait" : "" );
173-
174- ProcessBuilder pb = new ProcessBuilder ("powershell.exe" , "-Command" , psCommand );
175- pb .redirectErrorStream (true );
176- Process process = pb .start ();
177- byte [] output = readAllBytes (process .getInputStream ());
178- int exitCode = process .waitFor ();
179-
180- String outputStr = new String (output ).trim ();
181- if (!outputStr .isEmpty ()) {
182- log .debug ("Elevated command output: {}" , outputStr );
183- }
184-
185- return exitCode == 0 ;
186- } catch (IOException | InterruptedException e ) {
187- log .warn ("Failed to run elevated command: {}" , e .getMessage ());
188- return false ;
189- }
190- }
191-
192159 @ Override
193160 public String getInstanceId () {
194161 return instanceId ;
@@ -585,12 +552,12 @@ private void createInstanceDirectories() throws IOException {
585552 public void start () throws IOException , InterruptedException {
586553 log .info ("Starting Docker (instance: {})..." , instanceId );
587554
588- boolean isAdmin = isAdministrator ();
555+ boolean isAdmin = WindowsElevation . isAdministrator ();
589556 log .debug ("Administrator check result: {}" , isAdmin );
590-
557+
591558 if (isAdmin ) {
592559 log .info ("Running with administrator privileges. Using native Windows Docker" );
593- ensureContainersFeatureEnabled ( );
560+ WindowsFeatures . ensureEnabled ( WindowsFeatures . CONTAINERS );
594561 startNativeDocker ();
595562 } else {
596563 log .info ("Not running as administrator. Checking for WSL2..." );
@@ -603,30 +570,29 @@ public void start() throws IOException, InterruptedException {
603570 startWsl2Docker ();
604571 } else {
605572 log .error ("Docker requires either administrator privileges or a usable WSL2 Linux distribution." );
606- log .error ("You have WSL but only Docker Desktop's internal distros are installed." );
607-
608- if (promptForAutoSetup ("A Linux distribution (Ubuntu) needs to be installed in WSL.\n " +
609- "This requires a one-time administrator elevation (UAC prompt)." )) {
610- log .info ("Installing Ubuntu in WSL2 via elevated command..." );
611- boolean success = runElevatedWindowsCommand ("wsl" , "--install -d Ubuntu" , true );
612-
613- if (success ) {
614- log .info ("Ubuntu WSL installation started successfully." );
615- log .info ("Please complete the Ubuntu setup (create username/password) when prompted," );
616- log .info ("then run your application again." );
617- throw new RuntimeException ("WSL Ubuntu installed. Please complete setup and restart the application." );
618- } else {
619- log .error ("Elevated installation was cancelled or failed." );
620- log .error ("You can install it manually:" );
621- printWslInstallManualInstructions ();
622- throw new RuntimeException ("Cannot start Docker: WSL Ubuntu installation failed. See instructions above." );
623- }
624- } else {
573+ log .error ("No usable WSL2 Linux distribution was found (WSL is missing, or only Docker Desktop's internal distros exist)." );
574+
575+ if ("false" .equals (System .getProperty ("docker.auto.setup" ))) {
625576 log .error ("To fix this, install a Linux distribution:" );
626577 printWslInstallManualInstructions ();
627578 throw new RuntimeException ("Cannot start Docker: no admin privileges and no usable WSL2 Linux distro. " +
628579 "Install Ubuntu with 'wsl --install -d Ubuntu' (requires admin once)." );
629580 }
581+
582+ log .info ("Installing Ubuntu in WSL. Opening the Windows elevation prompt..." );
583+ int exitCode = WindowsElevation .runElevated ("wsl --install -d Ubuntu" );
584+
585+ if (exitCode == 0 ) {
586+ log .info ("Ubuntu WSL installation started successfully." );
587+ log .info ("Please complete the Ubuntu setup (create username/password) when prompted," );
588+ log .info ("then run your application again." );
589+ throw new RuntimeException ("WSL Ubuntu installed. Please complete setup and restart the application." );
590+ } else {
591+ log .error ("The elevation prompt was declined or the installation failed." );
592+ log .error ("You can install it manually:" );
593+ printWslInstallManualInstructions ();
594+ throw new RuntimeException ("Cannot start Docker: WSL Ubuntu installation failed. See instructions above." );
595+ }
630596 }
631597 }
632598 }
@@ -1025,47 +991,6 @@ private String runWslCommand(String command, boolean useSudo, int timeoutSeconds
1025991 }
1026992 }
1027993
1028- private void ensureContainersFeatureEnabled () throws IOException , InterruptedException {
1029- ProcessBuilder pb = new ProcessBuilder ("powershell.exe" , "-Command" , "Get-WindowsOptionalFeature -Online -FeatureName Containers" );
1030- pb .redirectErrorStream (true );
1031- Process process = pb .start ();
1032-
1033- StringBuilder output = new StringBuilder ();
1034- try (BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
1035- String line ;
1036- while ((line = reader .readLine ()) != null ) {
1037- output .append (line ).append ("\n " );
1038- }
1039- }
1040-
1041- process .waitFor ();
1042- String outputStr = output .toString ();
1043-
1044- if (!outputStr .contains ("State : Enabled" )) {
1045- log .info ("Enabling Windows Containers feature (requires reboot)..." );
1046- ProcessBuilder enablePb = new ProcessBuilder ("powershell.exe" , "-Command" , "Enable-WindowsOptionalFeature -Online -FeatureName Containers -All" );
1047- enablePb .inheritIO ();
1048- Process enableProcess = enablePb .start ();
1049- enableProcess .waitFor ();
1050- throw new RuntimeException ("Windows Containers feature enabled. Reboot required." );
1051- }
1052- }
1053-
1054- private boolean isAdministrator () {
1055- try {
1056- ProcessBuilder pb = new ProcessBuilder ("net" , "session" );
1057- pb .redirectErrorStream (true );
1058- Process process = pb .start ();
1059- byte [] output = readAllBytes (process .getInputStream ());
1060- int exitCode = process .waitFor ();
1061- log .debug ("Admin check (net session) exit code: {}" , exitCode );
1062- return exitCode == 0 ;
1063- } catch (IOException | InterruptedException e ) {
1064- log .debug ("Admin check failed with exception: {}" , e .getMessage ());
1065- return false ;
1066- }
1067- }
1068-
1069994 private String wslDistro = null ;
1070995
1071996 private boolean isWsl2Available () {
0 commit comments