21

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?

tk421
  • 5,775
  • 6
  • 23
  • 34
Toli
  • 5,547
  • 8
  • 36
  • 56

2 Answers2

10

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

Tom Parker-Shemilt
  • 1,679
  • 15
  • 26
4

As per Tom's first point, putting this anywhere before RUN yarn install within same Docker build stage can help by caching all yarn cache to volumed directory.

ENV YARN_CACHE_FOLDER=/usr/local/yarn-cache
VOLUME /usr/local/yarn-cache
hasnat
  • 2,647
  • 1
  • 20
  • 23