从容器读取docker标签 [英] Read docker tag from container

查看:153
本文介绍了从容器读取docker标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行中的容器中获取当前图像标签?目前,我正在传递一个与标签名称相同的环境变量,但是如果我能以某种方式从docker提供的环境变量中读取它,那就太好了。

Is it possible to get the current image tag inside a running container? Currently I am passing an environment variable with the same name as the tag, but it would be nice if I could somehow read it from a docker supplied environment variable.

<name>/<image>:<tag>

我正在配置中执行 sed 基于< tag>

I am doing a sed in a config based on the <tag>.

推荐答案

注意在容器中添加curl和jq并安装docker socket时,可以通过在容器中运行以下脚本来检索图像:

If you don't mind adding curl and jq to the container and also mounting the docker socket , you can retrieve the image by running the following script inside the container:

#!/bin/bash

CONTAINER_ID=$(head -1 /proc/self/cgroup | rev | cut -d/ -f 1 | rev)
curl --unix-socket /var/run/docker.sock http:/v1.40/containers/{$CONTAINER_ID}/json | jq .Config.Image

第一行从/ proc / self / cgroup获取容器ID第二个使用docker api来检查容器。

The first line gets the container id from /proc/self/cgroup and the second uses docker api to inspect the container.

这篇关于从容器读取docker标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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