'Copying files in to pre-built singularity containers
I have a pre-built singularity container. I would like to copy some files from the host in to the container. Will I be able to edit the bootstrap file after the container has been built? Or is there a simpler command to copy files on to the container?
Solution 1:[1]
When it comes to singularity in general the best alternatives I've found are either to
- Bind/mount the file/files into the container at runtime or
- Rebuild container from localimage
Suppose you have a singularity container container.sif and a file you want to copy from host at /path/on/host to /path/in/container.
Binding into container
The flag -B/--bind can be used with singularity exec, singularity shell and similar commands if this is enabled on your system.
For example
singularity shell --bind /path/on/host:/path/in/container container.sif
Rebuilding from localimage
Instead of rebuilding the container from scratch for adding a file you can start from container.sif and do changes on that. An example definition file Singularity.new_container could be
Bootstrap: localimage
From: container.sif
%files
/path/on/host /path/in/container
and build as usual with e.g.
singularity build --fakeroot new_container.sif Singularity.new_container
Some notes on this approach:
- Wildcard expansion does not work in %files
- Running
singularity inspect -d new_container.sifwill be very uninformative
Solution 2:[2]
If you are talking about copying files to a Running container:
? kubectl cp --help
Copy files and directories to and from containers.
Examples:
# !!!Important Note!!!
# Requires that the 'tar' binary is present in your container
# image. If 'tar' is not present, 'kubectl cp' will fail.
#
# For advanced use cases, such as symlinks, wildcard expansion or
# file mode preservation consider using 'kubectl exec'.
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
# Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
Options:
-c, --container='': Container name. If omitted, use the kubectl.kubernetes.io/default-container annotation for
selecting the container to be attached or the first container in the pod will be chosen
--no-preserve=false: The copied file/directory's ownership and permissions will not be preserved in the container
Usage:
kubectl cp <file-spec-src> <file-spec-dest> [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
Solution 3:[3]
Copying Files to Singularity Containers:
Docker: (Background)
In Docker, to copy and save files, we can leverage a few commands. (1) first docker cp, Once files are copied. Then (2) one must do some sort of "commit" to keep the updated image (i.e., at least in Docker that is how it works.)
- Docker cp - Copy files/folders between a container and the local filesystem
- Docker commit - Create a new image from a container’s changes
Singularity:
- Writable Mode: You can enter the sandbox with
sudo singularity shell --writable sandbox/. Then, you can make your changes there. - Save: In writeable mode, changes are saved in image/directory. Then, you can create a read-only image from the changes made in the sandbox.
- Privilege Mode One must use privilege mode (
sudo) to make all the changes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VRehnberg |
| Solution 2 | pkaramol |
| Solution 3 | itsmrbeltre |
