Set a good creation time when building a docker image with jib
Jib maven plugin (or gradle task) is really great to create Docker images in your build but by default it has only two potential values for its creation time (the date the image was created at):
- EPOCH which is more or less the date 0 for a java program (so if you build today your image will already be 50 years old since we start in 1970 ;)),
- USE_CURRENT_TIMESTAMP which means use "now"
For files, it is almost the same, either EPOCH_PLUS_SECOND (epoch + 1s since EPOCH was buggy in some cases) or a constant date.
Using EPOCH defaults is mainly to ensure the reproducibility of the builds...however it breaks several use cases like the last modified date for web jars for examples.
To solve that, you can explicitly set the value of the creation time and file last modification time. For maven plugin, it is just a matter of setting two attributes of the container configuration:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-plugin.version}</version>
<configuration>
<!-- ... -->
<container>
<filesModificationTime>${image.lastmodified}</filesModificationTime>
<creationTime>${image.creation}</creationTime>
</container>
</configuration>
</plugin>
The question is now: what would be a good value to set for each of these properties? Enforcing to use "now" is not bad but breaks the reproducibility of the build which is not that great.
If your project is managed by a SCM, a good value can be the last commit date. It keeps the reproducibility of the build and still let the last modified date be modified when there is a change.
Since most projects are using git these days, there is already a good plugin to grab git metadata: git-commit-id-plugin.
To setup this plugin and enable to grab last commit time you can just add to your pom:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<phase>initialize</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>false</generateGitPropertiesFile>
<dateFormat>yyyy-MM-dd'T'HH:mm:ss'Z'</dateFormat>
<dateFormatTimeZone>GMT</dateFormatTimeZone>
<includeOnlyProperties>
<includeOnlyProperty>^git.commit.time$</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
Then you can just use the variable git.commit.time in jib configuration:
<creationTime>${git.commit.time}</creationTime>
<filesModificationTime>${git.commit.time}</filesModificationTime>
Now your image will have the correct time for your files and creation date!
From the same author:
In the same category: