|
| 1 | +!!! important "Summary" |
| 2 | + |
| 3 | + Here, we describe how to run *NiPreps* with Docker containers. |
| 4 | + To illustrate the process, we will show the execution of *fMRIPrep*, but these guidelines extend to any other end-user *NiPrep*. |
| 5 | + |
| 6 | +## Before you start: install Docker |
| 7 | + |
| 8 | +Probably, the most popular framework to execute containers is Docker. |
| 9 | +If you are to run a *NiPrep* on your PC/laptop, this is the **RECOMMENDED** way of execution. |
| 10 | +Please make sure you follow the Docker installation instructions. |
| 11 | +You can check your Docker Runtime installation running their `hello-world` image: |
| 12 | + |
| 13 | +```Shell |
| 14 | +$ docker run --rm hello-world |
| 15 | +``` |
| 16 | + |
| 17 | +If you have a functional installation, then you should obtain the following output: |
| 18 | + |
| 19 | +```Shell |
| 20 | +Hello from Docker! |
| 21 | +This message shows that your installation appears to be working correctly. |
| 22 | + |
| 23 | +To generate this message, Docker took the following steps: |
| 24 | + 1. The Docker client contacted the Docker daemon. |
| 25 | + 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. |
| 26 | + (amd64) |
| 27 | + 3. The Docker daemon created a new container from that image which runs the |
| 28 | + executable that produces the output you are currently reading. |
| 29 | + 4. The Docker daemon streamed that output to the Docker client, which sent it |
| 30 | + to your terminal. |
| 31 | + |
| 32 | +To try something more ambitious, you can run an Ubuntu container with: |
| 33 | + $ docker run -it ubuntu bash |
| 34 | + |
| 35 | +Share images, automate workflows, and more with a free Docker ID: |
| 36 | + https://hub.docker.com/ |
| 37 | + |
| 38 | +For more examples and ideas, visit: |
| 39 | + https://docs.docker.com/get-started/ |
| 40 | +``` |
| 41 | + |
| 42 | +After checking your Docker Engine is capable of running Docker images, you are ready to pull your first *NiPreps* container image. |
| 43 | + |
| 44 | +## Docker images |
| 45 | + |
| 46 | +For every new version of the particular *NiPrep* app that is released, a corresponding Docker image is generated. |
| 47 | +The Docker image *becomes* a container when the execution engine loads the image and adds an extra layer that makes it *runnable*. In order to run *NiPreps* Docker images, the Docker Runtime must be installed. |
| 48 | +<!-- (see `installation_docker`{.interpreted-text |
| 49 | +role="ref"}).--> |
| 50 | + |
| 51 | +Taking *fMRIPrep* to illustrate the usage, first you might want to make sure of the exact version of the tool to be used: |
| 52 | + |
| 53 | +``` Shell |
| 54 | +$ docker pull poldracklab/fmriprep:<latest-version> |
| 55 | +``` |
| 56 | + |
| 57 | +You can run *NiPreps* interacting directly with the Docker Engine via the `docker run` interface. |
| 58 | + |
| 59 | + |
| 60 | +## Running a *NiPrep* with a lightweight wrapper |
| 61 | + |
| 62 | +Some *NiPreps* include a lightweight wrapper script for convenience. |
| 63 | +That is the case of *fMRIPrep* and its `fmriprep-docker` wrapper. |
| 64 | +Before starting, make sure you [have the wrapper installed](https://fmriprep.readthedocs.io/en/latest/installation.html#the-fmriprep-docker-wrapper). |
| 65 | +When you run `fmriprep-docker`, it will generate a Docker command line for you, print it out for reporting purposes, and then execute it without further action needed, e.g.: |
| 66 | + |
| 67 | +``` Shell |
| 68 | +$ fmriprep-docker /path/to/data/dir /path/to/output/dir participant |
| 69 | +RUNNING: docker run --rm -it -v /path/to/data/dir:/data:ro \ |
| 70 | + -v /path/to_output/dir:/out poldracklab/fmriprep:1.0.0 \ |
| 71 | + /data /out participant |
| 72 | +... |
| 73 | +``` |
| 74 | + |
| 75 | +`fmriprep-docker` implements [the unified command-line interface of BIDS Apps](framework.md#a-unified-command-line-interface), and automatically translates directories into Docker mount points for you. |
| 76 | + |
| 77 | +We have published a [step-by-step tutorial](http://reproducibility.stanford.edu/fmriprep-tutorial-running-the-docker-image/) illustrating how to run `fmriprep-docker`. |
| 78 | +This tutorial also provides valuable troubleshooting insights and advice on what to do after |
| 79 | +*fMRIPrep* has run. |
| 80 | + |
| 81 | +## Running a *NiPrep* directly interacting with the Docker Engine |
| 82 | + |
| 83 | +If you need a finer control over the container execution, or you feel comfortable with the Docker Engine, avoiding the extra software layer of the wrapper might be a good decision. |
| 84 | + |
| 85 | +**Accessing filesystems in the host within the container**: |
| 86 | +Containers are confined in a sandbox, so they can't access the host in any ways |
| 87 | +unless you explicitly prescribe acceptable accesses to the host. The |
| 88 | +Docker Engine provides mounting filesystems into the container with the |
| 89 | +`-v` argument and the following syntax: |
| 90 | +`-v some/path/in/host:/absolute/path/within/container:ro`, where the |
| 91 | +trailing `:ro` specifies that the mount is read-only. The mount |
| 92 | +permissions modifiers can be omitted, which means the mount will have |
| 93 | +read-write permissions. In general, you'll want to at least provide two |
| 94 | +mount-points: one set in read-only mode for the input data and one |
| 95 | +read/write to store the outputs. Potentially, you'll want to provide |
| 96 | +one or two more mount-points: one for the working directory, in case you |
| 97 | +need to debug some issue or reuse pre-cached results; and a |
| 98 | +[TemplateFlow](https://www.templateflow.org) folder to preempt the |
| 99 | +download of your favorite templates in every run. |
| 100 | + |
| 101 | +**Running containers as a user**: |
| 102 | +By default, Docker will run the |
| 103 | +container as **root**. Some share systems my limit this feature and only |
| 104 | +allow running containers as a user. When the container is run as |
| 105 | +**root**, files written out to filesystems mounted from the host will |
| 106 | +have the user id `1000` by default. In other words, you'll need to be |
| 107 | +able to run as root in the host to change permissions or manage these |
| 108 | +files. Alternatively, running as a user allows preempting these |
| 109 | +permissions issues. It is possible to run as a user with the `-u` |
| 110 | +argument. In general, we will want to use the same user ID as the |
| 111 | +running user in the host to ensure the ownership of files written during |
| 112 | +the container execution. Therefore, you will generally run the container |
| 113 | +with `-u $( id -u )`. |
| 114 | + |
| 115 | +You may also invoke `docker` directly: |
| 116 | + |
| 117 | +``` Shell |
| 118 | +$ docker run -ti --rm \ |
| 119 | + -v path/to/data:/data:ro \ |
| 120 | + -v path/to/output:/out \ |
| 121 | + poldracklab/fmriprep:<latest-version> \ |
| 122 | + /data /out/out \ |
| 123 | + participant |
| 124 | +``` |
| 125 | + |
| 126 | +For example: : |
| 127 | + |
| 128 | +``` |
| 129 | +$ docker run -ti --rm \ |
| 130 | + -v $HOME/ds005:/data:ro \ |
| 131 | + -v $HOME/ds005/derivatives:/out \ |
| 132 | + -v $HOME/tmp/ds005-workdir:/work \ |
| 133 | + poldracklab/fmriprep:<latest-version> \ |
| 134 | + /data /out/fmriprep-<latest-version> \ |
| 135 | + participant \ |
| 136 | + -w /work |
| 137 | +``` |
| 138 | + |
| 139 | +Once the Docker Engine arguments are written, the remainder of the |
| 140 | +command line follows the [usage](https://fmriprep.readthedocs.io/en/latest/usage.html). |
| 141 | +In other words, the first section of the command line is all equivalent to the |
| 142 | +`fmriprep` executable in a *bare-metal* installation: : |
| 143 | + |
| 144 | +``` Shell |
| 145 | +$ docker run -ti --rm \ # These lines |
| 146 | + -v $HOME/ds005:/data:ro \ # are equivalent to |
| 147 | + -v $HOME/ds005/derivatives:/out \ # a call to the App's |
| 148 | + -v $HOME/tmp/ds005-workdir:/work \ # entry-point. |
| 149 | + poldracklab/fmriprep:<latest-version> \ # |
| 150 | + \ |
| 151 | + /data /out/fmriprep-<latest-version> \ # These lines correspond |
| 152 | + participant \ # to the particular BIDS |
| 153 | + -w /work # App arguments. |
| 154 | +``` |
0 commit comments