Docker COPY 文件使用 glob 模式? [英] Docker COPY files using glob pattern?
问题描述
我有一个由 Yarn 管理的 monorepo,我想利用 Docker 缓存层来加速我的构建,为此我想先复制 package.json
和 yarn.lock
文件,运行 yarn install
然后复制其余文件.
I have a monorepo managed by Yarn, I'd like to take advantage of the Docker cache layers to speed up my builds, to do so I'd like to first copy the package.json
and yarn.lock
files, run yarn install
and then copy the rest of the files.
这是我的回购结构:
packages/one/package.json
packages/one/index.js
packages/two/package.json
packages/two/index.js
package.json
yarn.lock
这是 Dockerfile 中感兴趣的部分:
And this is the interested part of the Dockerfile:
COPY package.json .
COPY yarn.lock .
COPY packages/**/package.json ./
RUN yarn install --pure-lockfile
COPY . .
问题是第三个COPY
命令没有复制任何东西,我怎样才能达到预期的效果?
The problem is that the 3rd COPY
command doesn't copy anything, how can I achieve the expected result?
推荐答案
有一个基于多阶段构建 功能:
FROM node:12.18.2-alpine3.11
WORKDIR /app
COPY ["package.json", "yarn.lock", "./"]
# Step 2: Copy whole app
COPY packages packages
# Step 3: Find and remove non-package.json files
RUN find packages ! -name "package.json" -mindepth 2 -maxdepth 2 -print | xargs rm -rf
# Step 4: Define second build stage
FROM node:12.18.2-alpine3.11
WORKDIR /app
# Step 5: Copy files from the first build stage.
COPY --from=0 /app .
RUN yarn install --frozen-lockfile
COPY . .
# To restore workspaces symlinks
RUN yarn install --frozen-lockfile
CMD yarn start
在Step 5
中,即使packages
目录中的任何文件发生变化,图层缓存也会被重用.
On Step 5
the layer cache will be reused even if any file in packages
directory has changed.
这篇关于Docker COPY 文件使用 glob 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!