【问题标题】:Command to tell the OS Version of a container image命令告诉容器镜像的操作系统版本
【发布时间】:2020-12-02 19:00:18
【问题描述】:

我有一个较旧的 docker 容器映像 .Net Core 3.1.1。我认为它使用 debian-slim 10.2 作为操作系统。我希望升级到.Net Core 3.1.10 图像。我认为使用 debian-slim 10.6。

在我制定容器计划时,我希望能够轻松判断容器映像是使用哪个版本的 debian-slim 构建的。

我可以在容器映像上运行一个命令来了解它正在运行的 debian-slim 版本吗?

如果需要示例图像,那么这是我正在查看的图像:mcr.microsoft.com/dotnet/core/aspnet:3.1.10-buster-slim。另一个比较老的,是mcr.microsoft.com/dotnet/core/aspnet:3.1.1-buster-slim

【问题讨论】:

  • 你可以使用Dedockifygithub.com/mrhavens/Dedockify之类的工具
  • @Hackerman - 我调查了Dedockify。虽然非常有趣,但它在进入操作系统级别容器之前就停止了。对于关键容器,我需要检查它是否有一个 FROM 声明,该声明与我试图获取信息的容器相同。 (有点混乱)。它仍然是一个有趣的工具,即使它无法提供我需要的信息。
  • 实际上,我使用 windows 作为我的主要操作系统。我下载了容器映像,创建了一个 powershell 别名以检查它是否有效,并且像一个魅力一样工作,你只需提供 image id 它会打印所有信息,甚至是 FROM 语句:)
  • @Hackerman - 嗯,也许我做错了。你能从这个图像mcr.microsoft.com/dotnet/core/aspnet:3.1.10-buster-slim 中获取操作系统级别的容器吗? (对我来说,它有一堆 docker 文件命令,包括 FROM 语句,但它与我试图获取详细信息的图像相同(mcr.microsoft.com/dotnet/core/aspnet:3.1.10-buster-slim)并且它没有关于操作系统图像的任何详细信息版本。)

标签: docker debian containers


【解决方案1】:

这个命令似乎给出了版本号,但只给出了主要版本:

docker run -it --rm -a stdout --entrypoint cat MyContainer:1.0.0 "/etc/os-release"

我将提出一个关于在 debian-slim 容器上查找次要版本号的新问题。

更新: 以下是获取次要版本的方法:https://stackoverflow.com/a/65117928/16241

更新:

虽然速度很慢,但如果它是 debian 或 ubuntu,此 powershell 脚本将获取容器映像的 linux 发行版和版本号。

function GetOsInfoFromImage
{
    param (
        [string] $image = $(throw "-image is required")
    )
    
    $containerId = docker run --detach --interactive $image
    # Get the basic os information from the container and put it in a hash table    
    $osReleaseInfo = docker exec --interactive --tty $containerId cat "/etc/os-release" | ConvertFrom-StringData 
    $distribution = $osReleaseInfo.ID
    

    if ($distribution -eq 'debian') {       
        $version = docker exec --interactive --tty $containerId cat "/etc/debian_version"
        
        # if there are not any man pages then we can be fairly confident that this is a slim build.
        # pulled from here: https://github.com/debuerreotype/debuerreotype/blob/master/scripts/.slimify-excludes
        $isSlim = (docker exec --interactive --tty $containerId ls "/usr/share/man/").Count > 0
        if ($isSlim = $true) {
            $distribution = "$($distribution)-slim"
        }
    } elseif ($distribution -eq 'Ubuntu') {
        $version = $osReleaseInfo.VERSION_ID.Trim('"')      
    }   
    
    # cleanup the container we had to make
    docker stop $containerId | Out-Null
    docker rm $containerId | Out-Null
    
    $finalVersion = "$($distribution.ToLower())_$($version)"    
    $finalVersion
}

需要很长时间的原因是我必须创建然后从图像中删除容器的实例。

可以这样使用:

GetOsInfoFromImage mcr.microsoft.com/dotnet/core/aspnet:3.1.1-buster-slim

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 2012-04-12
    • 2014-01-23
    • 1970-01-01
    • 2012-02-05
    • 2020-12-20
    相关资源
    最近更新 更多