How do you cache Yarn Dependencies for a Docker image build in CircleCI?

Toli picture Toli · Oct 6, 2018 · Viewed 8.2k times · Source

My yarn installs take ~5 minutes right now. I'm trying to figure out a way to cut them down.

Right now in my Dockerfile I have the following:

COPY package.json yarn.lock node_modules /usr/src/app/
COPY ${YARN_CACHE} /root/.cache/yarn/
WORKDIR /usr/src/app

# We are doing this so that we can make use of layer caching 
# (i.e. most likely yarn deps won't change as often as the app code)
COPY . /usr/src/app

# yarn install
RUN yarn install

And in my circle file I have

  - restore_cache:
     keys:
       # only restores cache if the yarn file is the same
       - yarn-packages-v4-{{ checksum "yarn.lock" }}
  - run: docker pull "xxx.dkr.ecr.us-east-1.amazonaws.com/website:latest"
  - run: docker build --build-arg NODE_ENV=production --build-arg YARN_CACHE=$(yarn cache dir) --force-rm -t xxx.dkr.ecr.us-east-1.amazonaws.com/website:build-${CIRCLE_BUILD_NUM} .

However my yarn install still takes 5 minutes. Am I doing something wrong?

Answer

Tom Parker-Shemilt picture Tom Parker-Shemilt · Nov 2, 2018

The problem is that the result of yarn cache dir is an external folder, that either doesn't exist in the docker build or is just empty. You've got a couple of options