How to create flexible onbuild docker image using env vars?

I have a basic onbuild tagged onbuild image that I would like to use as a template for the assembly, but I want the child image to be able to set ENV var to change ONBUILD instructions

base onbuild:

 FROM root-image RUN mkdir -p /app/src UNBUILD COPY . /app/src/ ONBUILD WORKDIR /app/src ONBUILD RUN ./build ${TARGET_APP} ONBUILD RUN cp ${TARGET_APP}/build/bin /app/bin 

my-app:

 FROM base-onbuild ENV TARGET_APP my-app CMD my-app 

According to the docker documentation, the onbuild steps onbuild started immediately after the FROM base-onbuild , therefore, before the ENV statement that sets TARGET_APP , therefore, the onbuild TARGET_APP did not onbuild TARGET_APP .

I also tried passing the value of TARGET_APP with the --build-args docker build argument, but this also had no effect.

Is there any other way that I can set variables and change UNBUILD steps?

similar question: Field placeholder for the ONBUILD section for images of child dockers to be used

+5
source share
1 answer

Mandatory arg

I have something that works for your question, but it will not work for all cases at all.

In your docker file after ONBUILD WORKDIR /app/src you will have:

 COPY build_copy.sh /config ONBUILD COPY target_app.txt /config ONBUILD RUN sh /config/build_copy.sh 

The build_copy.sh file will contain the following lines:

 TARGET_APP=`cat /config/target_app.txt` ONBUILD RUN ./build $TARGET_APP ONBUILD RUN cp $TARGET_APP/build/bin /app/bin 

The target_app.txt file should contain the text that you want to put in TARGET_APP. This will require the target_app.txt file in your child image.


Optional arg argument

You can mitigate this a bit by adding the “optional” text files in the / config folder to your child’s root directory and changing your ONBUILD to:

 ONBUILD COPY /config /config 

You will need only a folder, all files there will be optional. The shell script can then use:

 TARGET_APP_2=`cat /config/target_app.txt` if [ -n "$TARGET_APP_2" ] then TARGET_APP=$TARGET_APP_2 else TARGET_APP='default_app' fi 
+2
source

Source: https://habr.com/ru/post/1236852/


All Articles