如何验证是否在 Singularity|Apptainer 容器中?
According to the 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).
So other than the change in prompt, how should one determine whether they are in a container or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查一些环境变量:
SINGULARITY_BIND
- 如果未设置绑定/挂载,可能仍为空SINGULARITY_COMMAND
- 例如, < code>exec、shell
等。SINGULARITY_CONTAINER
- 主机操作系统上映像的路径SINGULARITY_ENVIRONMENT
- 通常/.singularity.d/env/91-environment.sh
或类似的内容SINGULARITY_NAME
- 奇点图像的文件名或者,检查因为
/.singularity.d/Singularity
的存在。如果在奇点容器内,则这是创建图像时使用的奇点定义的副本。一般来说,/.singularity.d
不太可能存在于正常的主机操作系统上,除非有人做了一些非常奇怪的事情。There are a few environment variables you can check for:
SINGULARITY_BIND
- may still be empty if no binds/mounts are setSINGULARITY_COMMAND
- e.g.,exec
,shell
, etc.SINGULARITY_CONTAINER
- path to the image on the host OSSINGULARITY_ENVIRONMENT
- usually/.singularity.d/env/91-environment.sh
or something similarSINGULARITY_NAME
- filename of the singularity imageAlternatively, checking for the existence of
/.singularity.d/Singularity
. If inside a singularity container, that is a copy of the Singularity definition used when creating the image. In general, it is really unlikely for/.singularity.d
to exist on a normal host OS unless someone did something really weird.实现此目的的一种方法是在奇异 shell 命令中传递 --cleanenv 参数,并检查 PATH 变量是否与主机用户的 PATH 相同:
One way to do this is by passing the --cleanenv argument in the singularity shell command and checking if the PATH variable is the same as your host user's PATH: