Docker Community Forums

Share and learn in the Docker community.

COPY only if file exist

Hi All,

Is there a way to execute COPY only if the file exists on the host?
Maybe there is a way to do that check from a shell script that is executed in Dockerfile…

Thanks,
Alex.

2 Likes

Hi,

COPY instruction in Dockerfile expects a file to exist in the source location mentioned. If the source file is not there the build will fail. Dockerfie doesn’t have the feature to conditionally execute instructions.

Regards

You can use wildcards to optionally copy. For example, to COPY a yarn.lock file that may or may not exist, do:

COPY package.json yarn.* /opt/app/
5 Likes