|
| 1 | +--- |
| 2 | +title: Image Content |
| 3 | +sidebar: user_docs |
| 4 | +permalink: image-content |
| 5 | +folder: docs |
| 6 | +--- |
| 7 | + |
| 8 | +So, you want to put stuff in your container? This is understandable. There are a few ways to do that, some of which are more reproducible than others. |
| 9 | + |
| 10 | +## Bootstrap |
| 11 | +We recommend strongly that you add content during an image <a href="/docs-bootstrap">bootstrap</a>, because the operations on your image will be recorded in the build specification file, and you'll remember. During bootstrap you can get content: |
| 12 | + |
| 13 | + - by way of Docker layers |
| 14 | + - by cloning a Github (or other) repository in the `%post` section |
| 15 | + - using wget, curl, or any other method to download in `%post` or `%setup` |
| 16 | + - Adding pairs of `path/on/host` `/path/in/container` to the `%files` section |
| 17 | + |
| 18 | + |
| 19 | +## After Bootstrap |
| 20 | +While we don't recommend these methods (this is not reproducible practice) there are ways to add content after image creation. |
| 21 | + |
| 22 | +### Writable Shell |
| 23 | +You can shell into your image with `--writable` and then issue any kind of content creation, or download using `wget` or `curl` or `git clone`, etc. |
| 24 | + |
| 25 | +### Copy |
| 26 | +Singularity has a `copy` command that is basically a wrapper around the system's `/bin/cp`. This means that it accepts all of the same arguments, and option flags and you should look at these: |
| 27 | + |
| 28 | +``` |
| 29 | +man cp |
| 30 | +
|
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +Notably, you might run into the issue of copying a file to your container, and then you can't execute it. This is because you need to use `-p` to preserve permissions: |
| 35 | + |
| 36 | +``` |
| 37 | +singularity copy container.img -p /path/host/script.sh /bin |
| 38 | +``` |
| 39 | + |
| 40 | +Done without sudo, the permissions might look something like this: |
| 41 | + |
| 42 | +``` |
| 43 | +$ singularity exec container.img ls -l /bin/script.sh |
| 44 | +-rwxrwxr-x 1 vanessa vanessa 11 Apr 13 16:07 /bin/script.sh |
| 45 | +``` |
| 46 | + |
| 47 | +If you want to change the owner to root, you can do that first: |
| 48 | + |
| 49 | +``` |
| 50 | +$ chmod 775 script.sh |
| 51 | +$ sudo chown root script.sh |
| 52 | +$ sudo singularity copy container.img -p script.sh /bin |
| 53 | +$ singularity exec container.img ls -l /bin/script.sh |
| 54 | +-rwxrwxr-x 1 root vanessa 11 Apr 13 16:07 /bin/script.sh |
| 55 | +$ singularity exec rosdep.img /bin/script.sh |
| 56 | +Hello |
| 57 | +``` |
| 58 | + |
| 59 | +### Mount |
| 60 | +Mount is useful for physically mounting a container (it may not even be working) and moving files around. |
| 61 | + |
| 62 | +``` |
| 63 | +mkdir mnt |
| 64 | +$ singularity mount centos7.img mnt |
| 65 | +centos7.img is mounted at: mnt |
| 66 | +
|
| 67 | +Spawning a new shell in this namespace, to unmount, exit shell |
| 68 | +Singularity: \w> |
| 69 | +``` |
| 70 | + |
| 71 | +Note that if you run **without** sudo you won't see any files in the folder. You must run with sudo for them to be viewable. |
| 72 | + |
| 73 | +``` |
| 74 | +mkdir mnt |
| 75 | +$ sudo singularity mount centos7.img mnt |
| 76 | +centos7.img is mounted at: mnt |
| 77 | +$ |
| 78 | +``` |
| 79 | + |
| 80 | +A key difference is that with sudo, it creates the mount and doesn't shell. But even with sudo, you can't copy files. The container is read only. |
| 81 | + |
| 82 | +``` |
| 83 | +$ cp file.sh mnt/bin/ |
| 84 | +cp: cannot create regular file 'mnt/bin/file.sh': Read-only file system |
| 85 | +$ sudo cp file.sh mnt/bin/ |
| 86 | +cp: cannot create regular file 'mnt/bin/file.sh': Read-only file system |
| 87 | +``` |
| 88 | + |
| 89 | +Now if we try with sudo and `--writable` |
| 90 | + |
| 91 | +``` |
| 92 | +sudo singularity mount --writable centos7.img mnt |
| 93 | +centos7.img is mounted at: mnt |
| 94 | +
|
| 95 | +$ sudo cp file.sh mnt/bin |
| 96 | +``` |
| 97 | + |
| 98 | +Does the file execute from outside the container? |
| 99 | + |
| 100 | +``` |
| 101 | +ls -l mnt/bin/file.sh |
| 102 | +-rw-r--r-x 1 root root 13 Apr 13 15:46 mnt/bin/file.sh |
| 103 | +
|
| 104 | +ls -l file.sh |
| 105 | +-rw-rw-r-x 1 vanessa vanessa 13 Apr 13 15:43 file.sh |
| 106 | +
|
| 107 | +./mnt/bin/file.sh |
| 108 | +hello |
| 109 | +``` |
| 110 | + |
| 111 | +Yep. And actually, using `mount` instead of `copy` seems to better preserve (most) of the file's |
| 112 | +attributes. We can do a bit better, however: |
| 113 | + |
| 114 | +``` |
| 115 | +ls -l mnt/bin/file.sh |
| 116 | +-rw-rw-r-x 1 vanessa vanessa 13 Apr 13 15:43 mnt/bin/file.sh |
| 117 | +``` |
| 118 | + |
| 119 | +We generally recommend that you do not manually move things. It's not good practice. |
0 commit comments