Docker 使用 glob 模式复制文件?

Docker COPY files using glob pattern?

我有一个由 Yarn 管理的 monorepo,我想利用 Docker 缓存层来加速我的构建,为此我想先复制 package.jsonyarn.lock 个文件,运行 yarn install,然后复制其余文件。

这是我的回购结构:

packages/one/package.json
packages/one/index.js
packages/two/package.json
packages/two/index.js
package.json
yarn.lock

这是 Docker 文件中感兴趣的部分:

COPY package.json .
COPY yarn.lock .
COPY packages/**/package.json ./
RUN yarn install --pure-lockfile
COPY . .

问题是第三个COPY命令没有复制任何东西,我怎样才能达到预期的结果?

official Dockerfile reference for COPY <src> <dest>

中所述

The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

针对您的情况

Each may contain wildcards and matching will be done using Go’s filepath.Match rules.

这些是the rules。它们包含:

'*' matches any sequence of non-Separator characters

因此请尝试在您的模式中使用 * 而不是 **

只需使用.dockerignore 过滤掉不需要的文件。 refer this reference

在您的案例中,将此添加到您的。docker忽略。

*.js any file to skip copy

我假设您的文件位于 /home/package.json 之类的位置,并且想将这些文件复制到 docker 中的 /dest

Dockerfile 看起来像这样。 COPY /home /dest

这会将除 .dockerignore

中的列表之外的所有文件复制到 /home 目录

如果您无法在技术上枚举 Docker 文件中的所有子目录(即为每个子目录编写 COPY packages/one/package.json packages/one/),但想分两步复制所有文件并且利用 Docker 的缓存功能,您可以尝试以下解决方法:

  • 设计一个包装器脚本(例如,在 bash 中),将所需的 package.json 文件复制到一个单独的目录(例如,.deps/),该目录具有类似的层次结构,然后调用docker build …
  • 将Docker文件适配为事先复制(并重命名)单独的目录,然后调用yarn install --pure-lockfile

所有的东西放在一起,这可能会导致以下文件:

./build.bash:

#!/bin/bash

tag="image-name:latest"

rm -f -r .deps  # optional, to be sure that there is
# no extraneous "package.json" from a previous build

find . -type d \( -path \*/.deps \) -prune -o \
  -type f \( -name "package.json" \) \
  -exec bash -c 'dest=".deps/" && \
    mkdir -p -- "$(dirname "$dest")" && \
    cp -av -- "" "$dest"' bash '{}' \;
# instead of mkdir + cp, you may also want to use
# rsync if it is available in your environment...

sudo docker build -t "$tag" .

./Docker文件:

FROM …

WORKDIR /usr/src/app

# COPY package.json .  # subsumed by the following command
COPY .deps .
# and not "COPY .deps .deps", to avoid doing an extra "mv"
COPY yarn.lock .
RUN yarn install --pure-lockfile

COPY . .
# Notice that "COPY . ." will also copy the ".deps" folder; this is
# maybe a minor issue, but it could be avoided by passing more explicit
# paths than just "." (or by adapting the Dockerfile and the script and
# putting them in the parent folder of the Yarn application itself...)

有一个基于multistage-build特征的解决方案:

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 目录中的任何文件已更改,图层缓存也将被重新使用。

使用 Docker 的新 BuildKit 执行器,可以在 Docker 上下文中使用绑定安装,然后您可以根据需要从中复制任何文件。

例如,以下代码段将 Docker 上下文中的所有 package.json 文件复制到图像的 /app/ 目录(下例中的 workdir)

不幸的是,更改挂载中的任何文件仍然会导致图层缓存未命中。这可以使用多阶段方法解决 ,但这次不再需要显式删除。

# syntax = docker/dockerfile:1.2
FROM ... AS packages

WORKDIR /app/
RUN --mount=type=bind,target=/docker-context \
    cd /docker-context/; \
    find . -name "package.json" -mindepth 0 -maxdepth 4 -exec cp --parents "{}" /app/ \;

FROM ...

WORKDIR /app/
COPY --from=packages /app/ .

指定 mindepth/maxdepth 参数以减少要搜索的目录数量,这可以是 adjusted/removed 您的用例所需要的。

可能需要使用环境变量 DOCKER_BUILDKIT=1 启用 BuildKit 执行器,因为传统的执行器会默默地忽略绑定安装。

有关 BuildKit 和绑定边界的更多信息 can be found here

根据@Joost 的建议,我创建了一个 dockerfile,它利用 BuildKit 的强大功能来实现以下目标:

  • 通过将 npm 的缓存目录移动到构建缓存来更快 npm install
  • 如果自上次成功构建以来 package.json 文件没有任何变化,则跳过 npm install

伪代码:

  • 从构建上下文中获取所有 package.json 个文件
  • 将它们与上次成功构建的 package.json 文件进行比较
  • 如果发现更改,运行 npm install 并缓存 package.json 文件 + node_modules 文件夹
  • node_modules(新鲜的或缓存的)复制到图像中的所需位置
# syntax = docker/dockerfile:1.2
FROM node:14-alpine AS builder

# https://github.com/opencollective/opencollective/issues/1443
RUN apk add --no-cache ncurses

# must run as root
RUN npm config set unsafe-perm true

WORKDIR /app

# get a temporary copy of the package.json files from the build context
RUN --mount=id=website-packages,type=bind,target=/tmp/builder \
    cd /tmp/builder/ && \
    mkdir /tmp/packages && \
    chown 1000:1000 /tmp/packages && \
    find ./ -name "package.json" -mindepth 0 -maxdepth 6 -exec cp --parents "{}" /tmp/packages/ \;

# check if package.json files were changed since the last successful build
RUN --mount=id=website-build-cache,type=cache,target=/tmp/builder,uid=1000 \
    mkdir -p /tmp/builder/packages && \
    cd /tmp/builder/packages && \
    (diff -qr ./ /tmp/packages/ || (touch /tmp/builder/.rebuild && echo "Found an updated package.json"));

USER node

COPY --chown=node:node . /app

# run `npm install` if package.json files were changed, or use the cached node_modules/
RUN --mount=id=website-build-cache,type=cache,target=/tmp/builder,uid=1000 \
    echo "Creating NPM cache folders" && \
    mkdir -p /tmp/builder/.npm && \
    mkdir -p /tmp/builder/modules && \
    echo "Copying latest package.json files to NPM cache folders" && \
    /bin/cp -rf /tmp/packages/* /tmp/builder/modules && \
    cd /tmp/builder/modules && \
    echo "Using NPM cache folders" && \
    npm config set cache /tmp/builder/.npm && \
    if test -f /tmp/builder/.rebuild; then (echo "Installing NPM packages" && npm install --no-fund --no-audit --no-optional --loglevel verbose); fi && \
    echo "copy cached NPM packages" && \
    /bin/cp -rfT /tmp/builder/modules/node_modules /app/node_modules && \
    rm -rf /tmp/builder/packages && \
    mkdir -p /tmp/builder/packages && \
    cd /app && \
    echo "Caching package.json files" && \
    find ./ -name "package.json" -mindepth 0 -maxdepth 6 -exec cp --parents "{}" /tmp/builder/packages/ \; && \
    (rm /tmp/builder/.rebuild 2> /dev/null || true);

注意: 我只使用根文件夹的node_modules,在我的例子中,内部文件夹中的所有包都被提升到根