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

发布于 2025-01-15 02:43:04 字数 243 浏览 3 评论 0原文

根据 shell 文档

提示的变化表明您已进入容器(尽管您不应该依赖它来确定您是否在容器中)。

那么除了提示符的变化之外,应该如何判断它们是否在容器中呢?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多像笑话 2025-01-22 02:43:04

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

  • 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 set
  • SINGULARITY_COMMAND - e.g., exec, shell, etc.
  • SINGULARITY_CONTAINER - path to the image on the host OS
  • SINGULARITY_ENVIRONMENT - usually /.singularity.d/env/91-environment.sh or something similar
  • SINGULARITY_NAME - filename of the singularity image

Alternatively, 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.

我不会写诗 2025-01-22 02:43:04

实现此目的的一种方法是在奇异 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

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:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文