确定Docker映像的OS分布 [英] determine OS distribution of a docker image

查看:90
本文介绍了确定Docker映像的OS分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定任何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')
$ 

Q1 。是否可以在不知道特定图像中存在哪个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脚本中获取配置。

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上:

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

这篇关于确定Docker映像的OS分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆