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?
-
Can you build a custom Docker image, starting `FROM` the base Singularity image, `COPY`ing your files in? You'd need to push that image to a registry and name it as the `image:` in your Deployment spec. But then the files would exist in the container _before_ it started, and independent of which host in the cluster it runs on. – David Maze Feb 02 '22 at 00:03
3 Answers
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.sif
will be very uninformative

- 431
- 3
- 9
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).

- 16,451
- 43
- 149
- 324
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.

- 412
- 7
- 18
-
@DavidMaze the question is asking about singularity. I provided an example of a container that is more mainstream than singularity to make sure the reader/asker understands what I am presenting. I understand that it is not best practice which is not what he is asking here. However, based on the presented hypothesis here. Can you contribute "something" to the discussion to say what is the best practice? Goal: how to write to a container (singularity). – itsmrbeltre Feb 02 '22 at 00:07