确定 docker 映像的操作系统分布

2022-01-14 00:00:00 python docker dockerfile

问题描述

我需要确定任何 docker 映像的操作系统分发名称.我可以将ubuntu:latest标记为image1:latest,但是应该可以在image1:latest启动的时候得到它的分布信息.

I need to determine the OS distribution name for any docker image. I can tag ubuntu:latest as image1:latest, but I should be able to get the distribution information of image1:latest when it is launched.

为了实现这一点,我使用下面提到的命令来确定操作系统版本:

For achieving this, I used the below mentioned command to determine the OS version:

$ 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.

However, this has a dependency on whether the image has python2 or python3 in it. It fails for ubuntu:12.04 and I need to use python2 there.

$ 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')
$ 

第一季度.有没有一种方法可以在不知道特定图像中存在哪个版本的 python 的情况下实现相同的目标?

Q1. Is there a way I can achieve the same without knowing which version of python is there in a particular image?

注意:目标是确定用于构建此映像的基础映像.我无权访问用于构建此映像的 Dockerfile.

NOTE: The goal is to determine which was the base image used to build this image. I don't have access to the Dockerfile used to build this image.

第二季度.还有另一种使用入口点的方法.我可以使用 Dockerfile 从当前图像构建一个单独的图像.或者,我可以在创建容器时在 cmdline 中指定入口点,但我需要在容器内可以访问脚本.我猜我在使用 cmdline 时可能需要共享存储,有没有更好的方法来实现这一点?任何指针都会很有帮助.

Q2. There is another approach of using entrypoint. I can build a separate image from the current image using Dockerfile. Or, I can specify entrypoint in cmdline when creating container but I need the script to be accessible within the container. I am guessing that I might need shared storage when using cmdline, is there a better way to achieve this? Any pointers would be really helpful.

谢谢.


解决方案

文件系统层次标准有一个 /etc/os-release 的标准定义,应该在大多数发行版上都可用:

The Filesystem Hierarchy Standard has a standard definition for /etc/os-release, which should be available on most distributions:

/etc/os-release 和/usr/lib/os-release 文件包含操作系统标识数据.

The /etc/os-release and /usr/lib/os-release files contain operating system identification data.

os-release 的基本文件格式是换行符分隔的类似环境的 shell 兼容变量赋值列表.可以从 shell 脚本中获取配置.

The basic file format of os-release is a newline-separated list of environment-like shell-compatible variable assignments. It is possible to source the configuration from shell scripts.

这意味着您可以只获取 /etc/os-release 并使用 $NAME$ID 来识别发行版.例如,在 Fedora 上它看起来像这样:

This means you can just source /etc/os-release and use $NAME or $ID to identify the distribution. As an example, on Fedora it looks like this:

% source /etc/os-release
% echo $NAME
Fedora
% echo $ID
fedora

在 Debian 上:

On Debian:

% source /etc/os-release
% echo $NAME
Debian GNU/Linux
% echo $ID
debian

相关文章