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())