Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Docker container build timestamp in container?

I would like to build a container which shows up in its startup log its build datetime. Is there a way to have that information injected from my build machine into the container ?

like image 511
Riduidel Avatar asked Aug 06 '18 08:08

Riduidel


2 Answers

The output of each RUN step during a build is the changes to the filesystem. So you can output the date to a file in your image. And the logs from the container are just the stdout from commands you run. So you can cat out the date inside your entrypoint.

In code, you'd have at the end of your Dockerfile:

RUN date >/build-date.txt

And inside an entrypoint script:

#!/bin/sh
#.... Initialization steps
echo Image built: $(cat /build-date.txt)
#.... More initialization steps
# run the command
exec "$@"
like image 157
BMitch Avatar answered Sep 22 '22 10:09

BMitch


You could use an ARG to pass in the current build timestamp at build time. You'd have to docker build --build-arg build-date=$(date) or something like that. Having done that, you can refer to the argument using something similar to shell variable syntax at build time.

This is probably more useful if you have a significant build step in your Dockerfile (you're compiling a Go application) or if the metadata you're trying to embed is harder to generate programmatically (the source control version stamp, the name of the person running the build command).

like image 26
David Maze Avatar answered Sep 23 '22 10:09

David Maze