# Command line scripting with SlicerDockers

**URL:** <https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355>\
**Category:** Support\
**Tags:** scripted, docker\
**Created:** [September 20, 2022, 11:54am UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355 "2022-09-20T11:54:53Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 20, 2022, 11:54am UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/1 "2022-09-20T11:54:53Z")

</div>

Hi all,

A question regarding executing command line scripts while using SlicerDockers:

- I am using [GitHub - pieper/SlicerDockers: docker config files for slicer](https://github.com/pieper/SlicerDockers), everything works succesfully, I am able to access Slicer interface via the hosted vnc client.
- If possible I would like to send command line requests to the running slicer instance, similar to `Slicer.exe --python-code "slicer.util.loadScene('f:/2013-08-23-Scene.mrb')"` or `Slicer.exe --python-code "doSomething; doSomethingElse; etc." --testing --no-splash --no-main-window`
- Ultimately I am trying to figure out a way to create a way to open a slicer window from a custom web application, by creating a button which would execute a command, which opens a locally stored nifti file with the running slicer instance (and visible in the vnc window). I know that this exists - [New DICOMweb features: launch Slicer from web browser and download/upload data sets to the cloud using DICOMweb](https://discourse.slicer.org/t/new-dicomweb-features-launch-slicer-from-web-browser-and-download-upload-data-sets-to-the-cloud-using-dicomweb/17811) but in my case, all the files can be stored in the local storage, no communication with external DICOMWeb databases is necessary.

Is it possible with SlicerDockers? Or is there a better way to achieve similar functionality?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 20, 2022, 8:19pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/2 "2022-09-20T20:19:16Z")

</div>

Yes, you can send commands to a running Slicer with the [WebServer](https://slicer.readthedocs.io/en/latest/user_guide/modules/webserver.html) module and the [slicer/exec](https://slicer.readthedocs.io/en/latest/user_guide/modules/webserver.html#slicer-endpoints) endpoint. You can use the `--python-code` command line option to make sure the WebServer is running at startup. The `exec` endpoint allows you to execute arbitrary python code and get the results via http (of course be careful who has access to this endpoint!). You would also need to expose the port from your docker run command, 2016 by default. If you have shared the volume with the files to the docker instance then you can load the files using the passed in commands and have them show up in your vnc window.

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 21, 2022, 1:52pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/3 "2022-09-21T13:52:58Z")

</div>

Thank you for your answer!  
I not yet sure how can I launch the WebServer using [GitHub - pieper/SlicerDockers: docker config files for slicer](https://github.com/pieper/SlicerDockers) .  
WebServer module does not seem to be available in the Slicer instance hosted in the vnc. Is there a way to enable it either after start or when starting the docker container/inside the docker container?

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 21, 2022, 2:39pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/4 "2022-09-21T14:39:56Z")

</div>

I managed to add the WebServer module using [GitHub - pieper/SlicerWeb: Slicer modules that support web services and web applications](https://github.com/pieper/SlicerWeb) and adding the `--env SLICER_ARGUMENTS="--additional-module-paths /home/researcher/Documents/SlicerWeb/WebServer"` argument to the docker run command. However this way does seem to be limited in functionality, as I can only open the [http://localhost:2016/](http://localhost:2016/) (by exposing the port) but exec commands does not seem to be available (returns `unknown command "/exec"`) probably because I do not see the option to start server in the Web Server module started in this way.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 21, 2022, 3:09pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/5 "2022-09-21T15:09:45Z")

</div>

Slicer-5.0.3 has the Web Server module built in, so there is no need to set up anything else. You can start Slicer (with the exec interface enabled) from the command line like this:

```
"%localappdata%\NA-MIC\Slicer 5.0.3\Slicer.exe" --python-code "wslogic=getModuleLogic('WebServer'); wslogic.addDefaultRequestHandlers(enableExec=True); wslogic.start()"

```

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 21, 2022, 3:16pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/6 "2022-09-21T15:16:50Z")

</div>

Right, but for my usecase, I need the Slicer to be running in a docker container and accessible via vnc client, so I am using [GitHub - pieper/SlicerDockers: docker config files for slicer](https://github.com/pieper/SlicerDockers) . However, SlicerDockers seems to run the Slicer version 5.0.2 version, so thats probably why the Web Server module in not available in the launched Slicer instance. Maybe I can try to manually rebuild the docker with Slicer version 5.0.3 or are there are better alternatives?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 21, 2022, 3:20pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/7 "2022-09-21T15:20:52Z")

</div>

The web server in 5.0.3 is refactored and improved, so I would recommend it over the experimental version that was developed in a separate extension.

All you need to do is to [change the version to 5.0.3](https://github.com/pieper/SlicerDockers/blob/master/build.sh#L4) and rebuild the image. Maybe @pieper could do it and upload the new image to dockerhub.

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 21, 2022, 3:23pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/8 "2022-09-21T15:23:22Z")

</div>

That would certainly be excellent 👍 Otherwise, I will try to rebuild the image on my end, thanks

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 21, 2022, 3:42pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/9 "2022-09-21T15:42:37Z")

</div>

@NaglisR did you run `stevepieper/slicer:5.0.2` ? that should have the WebServer option built in. I haven’t had a chance to update to 5.0.3 yet but 5.0.2 should work the same mostly. Let us know how it goes.

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 21, 2022, 5:22pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/10 "2022-09-21T17:22:29Z")

</div>

It seems to me that 5.0.2 does not yet have the inbuilt Web Server module, checked that by downloading [https://download.slicer.org/bitstream/6286ce04e8408647b39f803a](https://download.slicer.org/bitstream/6286ce04e8408647b39f803a) .  
I am trying to rebuild the SlicerDockers image after changing the version [SlicerDockers/build.sh at master · pieper/SlicerDockers · GitHub](https://github.com/pieper/SlicerDockers/blob/master/build.sh#L4) and the download link [SlicerDockers/Dockerfile at 5949f71fab141740ba0da7c03af3825cc9b91798 · pieper/SlicerDockers · GitHub](https://github.com/pieper/SlicerDockers/blob/5949f71fab141740ba0da7c03af3825cc9b91798/slicer/Dockerfile#L22)  
However, I am not yet able to rebuilt the image, as it seems some minor adjustments have to be made elsewhere (probably names of some atributes have to be updated?).

```auto
Step 7/7 : RUN for ext in ${SLICER_EXTS} ; do echo "Installing ${ext}" ; EXTENSION_TO_INSTALL=${ext} su researcher -c "xvfb-run --auto-servernum /opt/slicer/Slicer --python-script /tmp/install-slicer-extension.py" ; done
 ---> Running in 975b22429285
Installing MarkupsToModel
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-researcher'
Switch to module: "Welcome"
installing MarkupsToModel
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "<string>", line 7, in <module>
AttributeError: qSlicerExtensionsManagerModel has no attribute named 'retrieveExtensionMetadataByName'

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 21, 2022, 8:59pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/11 "2022-09-21T20:59:44Z")

</div>

Thanks testing this - I was mistaken, the 5.0.2 version didn’t have the WebServer module as you discovered. I’m looking into updating the images to include a 5.0.3 version. I’ll report back if I get one uploaded. We should be able to fix the extensions (slicer-plus image) but the base slicer image wouldn’t need the extension manager so you can probably run that even if the build script doesn’t complete.

If you want to experiment without fixing the docker, you can also just copy the WebServer code directly from the Slicer source code into the module path of the 5.0.2 instance. There’s no compilation needed and I don’t think there’s even any configuration done during the cmake process, just copying the files.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 21, 2022, 9:04pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/12 "2022-09-21T21:04:24Z")

</div>

It turned out to be [easy enough](https://github.com/pieper/SlicerDockers/commit/d0facca5d43d5ce7c9a5e77babf61a9f9e2cb8ce) to update the base image, so now this should work to run 5.0.3 with the WebServer pre-installed.

`docker run -d -p 8080:8080 --name slicer stevepieper/slicer:5.0.3`

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [September 22, 2022, 5:09am UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/13 "2022-09-22T05:09:00Z")

</div>

Yes, pulled the stevepieper/slicer:5.0.3, everything seems to be working now, thank you!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 22, 2022, 11:29am UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/14 "2022-09-22T11:29:49Z")

</div>

Great! Your use case with a custom web app and containerized Slicer access sounds pretty interesting. If you can share your app source code, or just a small demo script, that would be good for others to learn from.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 22, 2022, 4:56pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/15 "2022-09-22T16:56:53Z")

</div>

> [@pieper](#):
>
> We should be able to fix the extensions (slicer-plus image) but the base slicer image wouldn’t need the extension manager so you can probably run that even if the build script doesn’t complete

FYI, here is an extension install code snippet that is compatible with all Slicer versions (including the latest Slicer-5.0.3):

> <https://github.com/Slicer/SlicerDocker/blob/10a8f38f6086f8d97a2a9cf81446974da2053001/slicer-notebook/install.sh#L41-L76>

In Slicer-5.1 and later the installation got simpler, see in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#download-and-install-extension).

---

<div class="post-metadata">

**Author:** ![NaglisR](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/naglisr/32/16280_2.png) [@NaglisR](https://discourse.slicer.org/u/NaglisR)\
**Post date:** [August 16, 2023, 8:34am UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/16 "2023-08-16T08:34:55Z")

</div>

Dear @pieper would it be possible to ask you to do the same (rebuild the SlicerDockers image in dockerhub) to the newest stable release 5.2.2?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 16, 2023, 3:14pm UTC](https://discourse.slicer.org/t/command-line-scripting-with-slicerdockers/25355/17 "2023-08-16T15:14:16Z")

</div>

I haven’t had a chance to work on those in a while. If you could make a PR with the update I could have a look.
