我需要确定任何 docker 映像的操作系统分发名称。我可以将 ubuntu:latest 标记为 image1:latest ,但我应该可以在启动时获取 image1:latest 的分布信息。
为此,我使用了下面提到的命令来确定操作系统版本:
$ docker tag ubuntu image1
$
$ docker run -it image1 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py"
('Ubuntu', '14.04', 'trusty')
$
但是,这取决于图像中是否包含 python2 或 python3 。 ubuntu:12.04 失败,我需要在那里使用 python2。
$ docker run -it ubuntu /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py"
('Ubuntu', '14.04', 'trusty')
$
$ docker run -it ubuntu:12.04 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python3 test.py"
/bin/sh: 1: python3: not found
$
$ docker run -it ubuntu:12.04 /bin/sh -c "echo import platform > test.py; echo print\(platform.dist\(\)\) >> test.py; python2 test.py"
('Ubuntu', '12.04', 'precise')
$
Q1。有没有一种方法可以在不知道特定图像中有哪个版本的 python 的情况下实现相同的目标?
注意:目标是确定哪个是用于构建此图像的基础图像。我无权访问用于构建此映像的 Dockerfile。
Q2。还有另一种使用入口点的方法。我可以使用 Dockerfile 从当前图像构建一个单独的图像。或者,我可以在创建容器时在 cmdline 中指定入口点,但我需要脚本可以在容器内访问。我猜我在使用 cmdline 时可能需要共享存储,有没有更好的方法来实现这一点?任何指针都会非常有帮助。
谢谢。
原文由 Rahul 发布,翻译遵循 CC BY-SA 4.0 许可协议
文件系统层次结构标准 对
/etc/os-release
有一个标准定义,它应该在大多数发行版中可用:这意味着您可以仅来源
/etc/os-release
并使用$NAME
或$ID
来识别分布。例如,在 Fedora 上它看起来像这样:在 Debian 上: