如何在 CircleCI 中为 Docker 镜像构建缓存 Yarn 依赖项?

Tol*_*oli 16 docker circleci yarnpkg

yarn install现在需要大约 5 分钟。我正在想办法减少它们。

现在在我的 Dockerfile 中,我有以下内容:

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
Run Code Online (Sandbox Code Playgroud)

在我的圈子文件中,我有

  - 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} .
Run Code Online (Sandbox Code Playgroud)

但是我的纱线安装仍然需要 5 分钟。难道我做错了什么?

Tom*_*ilt 8

问题是结果yarn cache dir是一个外部文件夹,该文件夹在 docker build 中不存在或只是空的。你有几个选择