0

I'm trying to stop and remove containers programmatically using a Kotlin process that execute the following command: docker-compose down --remove-orphans ,but the process never terminates, even after I call process.waitFor(). Alternatively, when I execute the same command in a powershell/cmd window the command terminates successfully. Does anyone knows if there are any issues with 'Runtime.GetRuntime().exec()' or the docker-compose down command, and if there's another way to execute cmd/powershell commands (and receive their exit value) in Kotlin? Thanks in advance.

My code:

 val process: Process?
 val sb = StringBuilder()
 
 val command = "powershell.exe -command docker-compose down --remove-orphans"
 process = Runtime.GetRuntime().exec(command)
 
 process.waitFor()
 
 val processStream: InputStream?
 if(process.exitValue() == 0){
     processStream = process.inputStream
 } else {
     processStream = process.errorStream
 }
 
 Scanner(processStream).use{
     while(it.hasNextLine())
         sb.append(it.nextLine() + System.lineSeparator())
 }
 
 println(sb.toString())
rzarviv
  • 43
  • 4
  • I am not sure why are you using `powershell...` to execute `docker-compose`? Is this the way how the things are done in Windows with Docker? – Alexey R. Nov 10 '20 at 18:09
  • Will it work with ```"powershell.exe -command \"docker-compose down --remove-orphans\""```? Also see: https://stackoverflow.com/questions/35421699/how-to-invoke-external-command-from-within-kotlin-code – Михаил Нафталь Nov 10 '20 at 18:46

0 Answers0