3

I'm trying to build a docker image that optionally adds a yarn (or npm lockfile) while building. I'd like to add it explicitly, but also not fail the build if it isn't included.

The intent is to respect it if the hosted application uses a deterministic build process, but not force it to. I'd also like to let an application use this container to bootstrap itself into deterministic builds.

Here's what I'm starting with:

FROM node:8.12.0-alpine
USER node
WORKDIR ${my_workdir}
COPY --chown=node:node src/yarn.lock ./
COPY --chown=node:node src/package*.json ./
RUN yarn && yarn cache clean
COPY --chown=node:node src/ .
CMD []

Is there a command or option I can use instead of copy that won't fail if the src/yarn.lock file isn't on the filesystem?

tk421
  • 5,775
  • 6
  • 23
  • 34
Dan Monego
  • 9,637
  • 6
  • 37
  • 72
  • 1
    I think this answer will clarify _why_ COPY always fails if the file does not exist and why there's no alternative: https://stackoverflow.com/questions/31528384/conditional-copy-add-in-dockerfile – Sergiu Paraschiv Mar 21 '19 at 15:03
  • Usually best practice is to check these lock files into source control, so it should always be there. – David Maze Mar 21 '19 at 15:04
  • What would happen if you just ran yarn after copying the full `src/` directory, implicitly copying the (potential) yarn.lock as well? Do you need the ability to overwrite files generated by yarn with files from the source repository? – Christoph Burschka Mar 21 '19 at 15:37
  • @ChristophBurschka usually you copy the package.json and yarn.lock and run yarn/npm before copying the source code for leveraging the build cache as if the dependency definition didn't change, this step will use the cache. – jmaitrehenry Mar 21 '19 at 15:52
  • Ah, I see. It does make sense to cache the yarn build independently of the source code; true. – Christoph Burschka Mar 21 '19 at 16:00

1 Answers1

2

You can try adding the yarn.lock as yarn.lock* along with another file so that the COPY won't fail. Something along these line should do the trick:

FROM node:8.12.0-alpine
USER node
WORKDIR ${my_workdir}
COPY --chown=node:node src/package*.json src/yarn.lock* ./
RUN yarn && yarn cache clean
COPY --chown=node:node src/ .
CMD []

Hope it helps!

Bogsan
  • 631
  • 6
  • 12