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 ?
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 "$@"
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With