如何验证是否在 Singularity|Apptainer 容器中?

How to verify if in Singularity|Apptainer container?

根据 shell doc:

The change in prompt indicates that you have entered the container (though you should not rely on that to determine whether you are in container or not).

所以除了提示的变化之外,应该如何确定它们是否在容器中?

一种方法是在 singularity shell 命令中传递 --cleanenv 参数并检查 PATH 变量是否与主机用户的 PATH 相同:

#add an arbitrary file location to your PATH variable and check that it is present for the host
export PATH=$PATH:/path/to/foo/bar
echo $PATH

#now pull up a shell in your container with --cleanenv to ignore the environmental variables of the host - such as the PATH we just exported
singularity shell --cleanenv yourimage.sif
#check that /path/to/foo/bar is not in PATH in your container
echo $PATH

您可以检查一些环境变量:

  • SINGULARITY_BIND - 如果没有设置 binds/mounts
  • ,可能仍然是空的
  • SINGULARITY_COMMAND - 例如, execshell
  • SINGULARITY_CONTAINER - 主机上图像的路径 OS
  • SINGULARITY_ENVIRONMENT - 通常是 /.singularity.d/env/91-environment.sh 或类似的
  • SINGULARITY_NAME - 奇点图像的文件名

或者,检查 /.singularity.d/Singularity 是否存在。如果在奇点容器内,则为创建图像时使用的奇点定义的副本。一般来说,/.singularity.d 不太可能存在于普通主机上 OS 除非有人做了一些非常奇怪的事情。