# Invoking docker from a slicer module

**URL:** https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529
**Category:** Development
**Tags:** python
**Created:** [July 27, 2022, 5:26pm UTC](https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529 "2022-07-27T17:26:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lee\_Newberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lee_newberg/32/7615_2.png) [@Lee\_Newberg](https://discourse.slicer.org/u/Lee_Newberg)
#### Post date: [July 27, 2022, 5:26pm UTC](https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529/1 "2022-07-27T17:26:13Z")

</div>

I am modifying [LungAIR](https://github.com/KitwareMedical/lungair-desktop-application), 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?

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [July 27, 2022, 5:55pm UTC](https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529/2 "2022-07-27T17:55:37Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#module-slicer.util)

For example:

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

```

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [July 27, 2022, 5:58pm UTC](https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529/3 "2022-07-27T17:58:53Z")

</div>

Depending on how involved the handling of docker containers would be, you may also want to consider looking at [docker-py](https://docker-py.readthedocs.io/en/stable/)

---

<div class="post-metadata">

### Author: ![Lee\_Newberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lee_newberg/32/7615_2.png) [@Lee\_Newberg](https://discourse.slicer.org/u/Lee_Newberg)
#### Post date: [August 4, 2022, 2:32pm UTC](https://discourse.slicer.org/t/invoking-docker-from-a-slicer-module/24529/4 "2022-08-04T14:32:37Z")

</div>

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.
