|
| 1 | +# Bind mounting host system directories into a container |
| 2 | + |
| 3 | +It's possible to create and modify files on the host system from within the container. In fact, that's exactly what we did in the previous example when we created output files in our home directory. |
| 4 | + |
| 5 | +Let's be more explicit. Consider this example. |
| 6 | + |
| 7 | +``` |
| 8 | +$ singularity shell lolcow.sif |
| 9 | +
|
| 10 | +Singularity> echo wutini > ~/jawa.txt |
| 11 | +
|
| 12 | +Singularity> cat ~/jawa.txt |
| 13 | +wutini |
| 14 | +
|
| 15 | +Singularity> exit |
| 16 | +
|
| 17 | +$ cat ~/jawa.txt |
| 18 | +wutini |
| 19 | +``` |
| 20 | + |
| 21 | +Here we shelled into a container and created a file with some text in our home directory. Even after we exited the container, the file still existed. How did this work? |
| 22 | + |
| 23 | +There are several special directories that Singularity _bind mounts_ into |
| 24 | +your container by default. These include: |
| 25 | + |
| 26 | +- `$HOME` |
| 27 | +- `/tmp` |
| 28 | +- `/proc` |
| 29 | +- `/sys` |
| 30 | +- `/dev` |
| 31 | + |
| 32 | +You can specify other directories to bind using the `--bind` option or the environmental variable `$SINGULARITY_BINDPATH` |
| 33 | + |
| 34 | +Let's say we want to access a directory called `/data` from within our container. For this example, we first need to create this new directory with some data on our host system. |
| 35 | + |
| 36 | +``` |
| 37 | +$ sudo mkdir /data |
| 38 | +
|
| 39 | +$ sudo chown $USER:$USER /data |
| 40 | +
|
| 41 | +$ echo 'I am your father' > /data/vader.txt |
| 42 | +``` |
| 43 | + |
| 44 | +Now let's see how bind mounts work. First, let's list the contents of `/data` within the container without bind mounting `/data` on the host system to it. |
| 45 | + |
| 46 | +``` |
| 47 | +$ $ singularity exec lolcow.sif ls -l /data |
| 48 | +ls: cannot access '/data': No such file or directory |
| 49 | +``` |
| 50 | + |
| 51 | +Nothing there! Now let's repeat the same command but using the `--bind` option to bind mount `/data` into the container. |
| 52 | + |
| 53 | +``` |
| 54 | +$ singularity exec --bind /data lolcow.sif ls -l /data |
| 55 | +total 4 |
| 56 | +-rw-rw-r-- 1 student student 17 Mar 2 00:51 vader.txt |
| 57 | +``` |
| 58 | + |
| 59 | +Now a `/data` directory is created in the container and it is bind mounted to the `/data` directory on the host system. |
| 60 | + |
| 61 | +You can bind mount a source directory of one name on the host system to a destination of another name using a `source:destination` syntax, and you can bind mount multiple directories as a comma separated string. For instance: |
| 62 | + |
| 63 | +``` |
| 64 | +$ singularity shell --bind src1:dest1,src2:dest2,src3:dest3 some.sif |
| 65 | +``` |
| 66 | + |
| 67 | +If no colon is present, Singularity assumes the source and destination are identical. To do the same thing with an environment variable, you could do the following: |
| 68 | + |
| 69 | +``` |
| 70 | +$ export SINGULARITY_BINDPATH=src1:dest1,src2:dest2,src3:dest3 |
| 71 | +``` |
| 72 | + |
| 73 | +For a lot more info on how to bind mount host directories to your container, check out the [NIH HPC Binding external directories](https://hpc.nih.gov/apps/singularity.html#bind) section. |
0 commit comments