0

Can ADD command be customised to work with command line argument. Basically i have different versions of a file let's say file1 and file2, and based on some condition passed through command line.

The following command works correctly and transfers the file from host to docker, but i didn't find any references to do it conditionally.
ADD target/file.yml file.yml

Aseem Goyal
  • 2,683
  • 3
  • 31
  • 48
  • Possible duplicate of [Conditional COPY/ADD in Dockerfile?](https://stackoverflow.com/questions/31528384/conditional-copy-add-in-dockerfile) – David Maze Aug 29 '19 at 10:07

1 Answers1

0

No, ADD does not support the conditional way of copying files.

But there is a way to deal with such configuration while coping from Host.

  • Copy all configuration to some temp location in your case copy all file1 file2 to some /temp location, then base on ARG pass to docker build, move the file to target.
  • Or do the above using docker entrypoint based on ENV, instead of base on ARG
FROM alpine
ADD target/ /temp_target
RUN mkdir /target

#filename to be copied to the target 
ARG file_name

# pass update_file true or false if true file wil be update to new that is passed to build-args
ARG update_file

ENV file_name=$file_name
ARG update_file=$update_file

#check if update_file is set and its value is true then copy the file from temp to target, else copy file1 to target
RUN if [ ! -z "$update_file" ] && [ "${update_file}" == true ];then \
   echo "$update_file"; \
   echo "echo file in temp_target"; \
   ls /temp_target ;\
   echo "updating file target" ;\
   cp /temp_target/"${file_name}" /target/ ; \
   echo "list of updated files in target"; \
   ls /target ; \
   else \
   echo "copy with default file that is ${file_name}" ;\
   cp /temp_target/file1 /target ;\
   fi

Build:

This will not updated file, will copy with default filename that is file1

docker build --no-cache --build-arg file_name=file1 --build-arg update_file=false -t abc .

This will update file in the target, so the new file will be in the target is file2.

 docker build --no-cache --build-arg file_name=file2 --build-arg update_file=true -t abc .
Adiii
  • 54,482
  • 7
  • 145
  • 148