How can OpenShift container learn its image ID?

守給你的承諾、 提交于 2019-12-24 11:34:00

问题


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.

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

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

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?

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.


回答1:


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


来源:https://stackoverflow.com/questions/52659933/how-can-openshift-container-learn-its-image-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!