Docker COPY 文件使用 glob 模式? [英] Docker COPY files using glob pattern?

查看:17
本文介绍了Docker COPY 文件使用 glob 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 Yarn 管理的 monorepo,我想利用 Docker 缓存层来加速我的构建,为此我想先复制 package.jsonyarn.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆