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.

1 Like

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/
6 Likes

This one from @dylansmith really worked!

Thanks!