从 Dockerfile 运行 Docker Inspect

发布于 2025-01-11 10:17:13 字数 866 浏览 0 评论 0 原文

我正在创建一个简单的 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 }}'

I am creating a simple HTML and want it to display the time docker container is built. I would like to use Docker inspect, Since docker inspect is run from inside a docker container am I correct that it is not possible to use it in Docker file?

My html file is

<!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>

MY DOCKER FILE is

FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN docker inspect -f '{{ .Created }}'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北陌 2025-01-18 10:17:13

我不确定我是否理解您想要衡量的内容。但是,如果 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.

草莓味的萝莉 2025-01-18 10:17:13

可以使用某种形式的 docker-in-docker (例如 docker 图像)。但是,我不认为这会做你想做的事 - 你无法获得尚未实际创建的图像的创建日期。

您可以使用 date 命令来获取当前时间的时间戳:

$ date
Tue Mar  1 23:43:44 -03 2022

但是,我建议不要这样做。它会与 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:

$ date
Tue Mar  1 23:43:44 -03 2022

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.

邮友 2025-01-18 10:17:13

Docker Inspect 可能会获得构建时间,但它

  1. 需要调用 docker 命令,引入太多依赖项,因此对于这种琐碎的任务来说太“繁重”。
  2. 只能在镜像构建之后运行。

更简单、更轻便的方法是在构建 docker 时替换 html 文件。

  1. 定义要在 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>
<p>DATEPLACEHOLDER</p>
</body>
</html>
  1. 将占位符替换为日期变量。
FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN export NOW=$(date); sed -i "s/DATEPLACEHOLDER/$(NOW)/g" "/usr/share/nginx/html"

或者您可以使用RUN echo date>; /tmp/date.txt,并在docker启动时将值替换为html(可以在dockerfileCMD部分完成)。

然而,这种方法也有其缺点。即使 dockerfile 中没有任何更改,每次构建都会构建一个新映像。并且如果替换后还有其他重要步骤,docker构建缓存无效,导致构建时间更长,推送时间更长。

Docker Inspect might get the build time, but it

  1. Requires invoking docker command, introducing too much dependency, and hence too 'heavy' for such trivil task.
  2. Can only run after the image build.

The simpler and lighter way, is to replace the html file when building the docker.

  1. Define a place holder(a unique constant string to be replaced) to be replaced in html file.
<!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>
<p>DATEPLACEHOLDER</p>
</body>
</html>
  1. Substitute the place holder with date variable.
FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN export NOW=$(date); sed -i "s/DATEPLACEHOLDER/$(NOW)/g" "/usr/share/nginx/html"

Or you can use RUN echo date > /tmp/date.txt, and subsitute the value to html when docker startup (can be done in CMD section of dockerfile).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文