Invoking docker from a slicer module

I am modifying LungAIR, a customization of 3D Slicer, and I am going to have the new module call off to a docker image (on the same computer) to perform a calculation. It would be great if there is a 3D Slicer module out there that already does something similar that I could emulate; anyone know of any?

I suspect that the MONAI Label back end for 3D Slicer could be used, but I don’t think I need all that … or is that the best practice even in a fairly simple case?

Generally speaking, using the functions slicer.util.launchConsoleProcess and slicer.util.logProcessOutput would allow you to run processes like docker and capture its output.

See https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#module-slicer.util

For example:

import shutil
dockerExecutablePath = shutil.which('docker')
commandLine = [dockerExecutablePath, "run", "--rm", "hello-world:latest"]
proc = slicer.util.launchConsoleProcess(commandLine)
slicer.util.logProcessOutput(proc)

Depending on how involved the handling of docker containers would be, you may also want to consider looking at docker-py

Thank you @jcfr, this is useful for launching the docker executable.

On a closely related matter, what is the best practice for making a new docker image available via the above slicer.util.launchConsoleProcess(commandLine)?

  1. Insert CMakeLists.txt commands so that the docker image is built locally for each user at build time?
  2. Build the docker image myself and upload it to some public repository, so that the above suggested code will automagically find and download the image if it isn’t already available. If so, which public repository?
  3. Something else?

Especially useful would be 3D Slicer code / module that already does the best practice, as a template.