从 Dockerfile 运行 Docker Inspect
我正在创建一个简单的 HTML,并希望它显示 docker 容器的构建时间。我想使用 Docker 检查,因为 docker 检查是从 docker 容器内部运行的,我是否正确地认为不可能在 Docker 文件中使用它?
我的 html 文件是
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to Nginx Web Server</h1>
<p>Date/Time: <span id="datetime"></span></p>
<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = (("0"+dt.getDate()).slice(-2)) +"."+ (("0"+(dt.getMonth()+1)).slice(-2)) +"."+ (dt.getFullYear()) +" "+ (("0"+dt.getHours()).slice(-2)) +":"+ (("0"+dt.getMinutes()).slice(-2));
</script>
<p>This server is running through Docker container deployed:</p>
</body>
</html>
我的 DOCKER 文件是
FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN docker inspect -f '{{ .Created }}'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解您想要衡量的内容。但是,如果 docker 没有安装在您的镜像中,那么您就无法调用它。
I'm not sure I understand what you're trying to measure. But if docker is not installed within your image, then you cannot invoke it.
可以使用某种形式的 docker-in-docker (例如 docker 图像)。但是,我不认为这会做你想做的事 - 你无法获得尚未实际创建的图像的创建日期。
您可以使用
date
命令来获取当前时间的时间戳:但是,我建议不要这样做。它会与 docker 的缓存发生奇怪的交互 - 该日期将被缓存以用于后续构建,除非您更改 index.html。
It's possible to run
docker inspect
in a container using some form of docker-in-docker (like the docker image). However, I don't think this will do what you want to do - you can't get the creation date of an image which hasn't actually been created yet.You could instead use the
date
command to get a timestamp for the current time:However, I'd recommend against doing this. It would interact weirdly with docker's caching - that date would be cached for subsequent builds, unless you change your index.html.
Docker Inspect 可能会获得构建时间,但它
更简单、更轻便的方法是在构建 docker 时替换 html 文件。
或者您可以使用
RUN echo date>; /tmp/date.txt
,并在docker启动时将值替换为html(可以在dockerfile
的CMD
部分完成)。然而,这种方法也有其缺点。即使 dockerfile 中没有任何更改,每次构建都会构建一个新映像。并且如果替换后还有其他重要步骤,docker构建缓存无效,导致构建时间更长,推送时间更长。
Docker Inspect
might get the build time, but itdocker
command, introducing too much dependency, and hence too 'heavy' for such trivil task.The simpler and lighter way, is to replace the html file when building the docker.
date
variable.Or you can use
RUN echo date > /tmp/date.txt
, and subsitute the value to html when docker startup (can be done inCMD
section ofdockerfile
).This method, however, also has its drawbacks. Even nothing in
dockerfile
changed, a new image will be built for each build. And if there are other important steps after the substitution, docker build caches are invalid, resulting longer build time and longer push time.