OpenShift容器如何了解其图像ID? [英] How can OpenShift container learn its image ID?

查看:116
本文介绍了OpenShift容器如何了解其图像ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以模板变量INITDB的形式向OpenShift 3.9的基于PostgreSQL的专有Docker镜像添加配置选项.该映像提供了一个由持久性存储支持的数据库,从现在开始,只有在设置了该变量(标志)时,才应初始化该数据库.

I would like to add a configuration option to a proprietary, PostgreSQL-based Docker image for OpenShift 3.9 in the form of a template variable INITDB. The image provides a database that is backed by persistent storage, and from now on the database should only be initialized when that variable (flag) is set.

该图像是使用OpenShift的Docker构建策略构建的, 在DockerfileENTRYPOINT脚本中调用PostgreSQL的initdb:因此,只要容器启动,它就会执行.但是我只想设置标志仅在标记容器第一次启动时才起作用.否则可能发生的情况是,当容器首先启动时(应该是这种情况),数据库被初始化,但是当容器重新启动时,数据库也被重新初始化,例如因为迁移到另一个节点(这是不必要的).

The image is built with OpenShift's Docker build strategy and PostgreSQL's initdb is called in the Dockerfile's ENTRYPOINT script: so it executes whenever the container starts up. However I want to have a set flag only to have an effect when a flagged container starts up for the first time. Otherwise what could happen is that the database becomes initialized when the container starts up first (as should be the case) but also becomes re-initialized when the container is restarted e.g. because of migration to another node (this is unwanted).

所以我大概需要一些逻辑,以便脚本将容器的图像ID也在逻辑上持久存储在文件中,这样,只有在设置了标志的情况下,脚本才会调用initdb不存在或包含其他图像ID. 所以也许大致符合这些原则:

So I presumably need some logic whereby the script stores the container's image id in a file also in persistent storage with logic such that it calls initdb only if the flag is set and the file does not exist or contains another image id. So perhaps something roughly along those lines:

file=/mnt/pgdata/image_id
if [ -n "$INITDB" ] && [ $(cat $file) != $image_id]; then
  initdb ...
  echo $image_id > $file
fi

所以我的问题是这样的:正在运行的容器如何了解其图像的ID?是否有现成的环境变量(例如OPENSHIFT_...,到目前为止我还没有找到)还是必须通过API?第二种选择似乎可行,因为oc describe pods列出了图像ID"(并且由于oc explain pod.spec.containers.image).但是是否有必要/建议?如果是这样,那么在默认情况下,是否必须提供显式凭证或容器是否拥有适当的凭证?

So my question is this: how can a running container learn its image's id? Is there a ready environment variable (e.g. OPENSHIFT_... -- so far I have found none) or would it have to go through an API? The second choice seems feasible because oc describe pods lists "Image ID" (and because of oc explain pod.spec.containers.image). But is it necessary/advisable and if so, would one have to provide explicit credentials or do containers own appropriate credentials by default?

我还想了解OpenShift自己的/官方" PostgreSQL映像如何提供这种功能,但还没有找到正确的源代码.

I'd also be interested in finding out how OpenShift's own/"official" PostgreSQL image provides such functionality, but have not found the right source code yet.

推荐答案

其中一项评论中对hostname的建议是正确的.现在,以下代码可以正常工作:

The suggestion towardshostname received in one of the comments was on point. The following piece of code now serves as intended:

file=/mnt/pgdata/hostname
if [ -n \"$INITDB\" ] && [ \"$(cat $file)\" != $(hostname) ]; then
  initdb ...
  echo $(hostname) > $file
fi

这篇关于OpenShift容器如何了解其图像ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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