scaladockerprocessammonite

Unable to stop and rm docker containers from Scala script


I am trying to execute the following command from within an ammonite Scala script:

 Process("docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)")

I always get the following output:

unknown shorthand flag: 'a' in -a
See 'docker stop --help'.

I've tried so many different variations without any luck, however it works fine from the console. Some help would be much appreciated.


Solution

  • If you pass a string to Process then scala will just split it on spaces, and it won't be clever enough to interpret things like bash's command substitution $().

    The easiest workaround would be to use a Seq of commands, and wrap your stuff in a call to bash:

    Process(Seq("bash", "-c", "docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)"))