'Docker Build - Inject data into project config file during build

I have a config file in the following path of my project:

<root_where_docker_file_id>/api/src/main/resources/application.yml

And the following dockerfile:

FROM gradle:jdk17 AS build


COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle :api:bootJar --no-daemon --stacktrace
RUN gradle test --no-daemon --stacktrace

FROM openjdk:17-oracle
COPY --from=build /home/gradle/src/api/build/libs/api-0.0.1-SNAPSHOT.jar /api.jar

EXPOSE 8091
ENTRYPOINT ["java", "-jar","/api.jar"]

The yaml file has a property VERSION that is manually set at the moment, but I'm looking for a way to update it during the docker build, how can this be done?

The version will be auto pulled from github latest tag, it's a separate issue but if someone knows the command for that as well that would be super.



Solution 1:[1]

I have encountered similar problems

You can use sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename

For example, RUN sed -i 's/VERSION=1.0/VERSION=2.0/g' <root_where_docker_file_id>/api/src/main/resources/application.yml

This will modify the file in-place, and change the version number, as long as you can match it with sed.

If you know how to pull the version on the go, do this:

RUN sed -i 's/VERSION=1.0/VERSION=$(pull-version-command)/g' <root_where_docker_file_id>/api/src/main/resources/application.yml

Otherwise you probably have to modify the Dockerfile before running it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neo