ADD
命令是否可以自定义以使用命令行参数。 基本上,我有一个文件的不同版本,比如说file1和file2,并且基于通过命令行传递的某些条件。
以下命令工作正常并将file
从主机传输到 docker,但我没有找到任何有条件地执行此操作的引用。ADD target/file.yml file.yml
不,ADD
不支持复制文件的条件方式。
但是有一种方法可以在从主机应对时处理此类配置。
- 将所有配置复制到您的案例中的某个临时位置 将所有
file1 file2
复制到某个/temp
位置,然后基于ARG
传递到 docker 构建,将file
移动到target
。 - 或者使用基于
ENV
的 docker 入口点执行上述操作,而不是基于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
建:
这不会更新文件,将使用默认文件名 file1 进行复制
docker build --no-cache --build-arg file_name=file1 --build-arg update_file=false -t abc .
这将更新目标中的文件,因此新文件将位于目标 file2 中。
docker build --no-cache --build-arg file_name=file2 --build-arg update_file=true -t abc .