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

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

问题描述

我有一个由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?

推荐答案

要跟踪@FezVrasta对我的第一个答案,如果您无法枚举Dockerfile中所有危急的子目录,但想要将所有文件复制为两个要利用Docker缓存功能的步骤,您可以尝试以下解决方法:

To follow-up @FezVrasta's comment to my first answer, if you can't possibly enumerate all the subdirectories at stake in the Dockerfile, but want to copy all the files in two steps to take advantage of Docker cache capabilities, you could try the following workaround:


  • 设计一个用于复制的包装脚本(例如,以bash格式)将所需的 package.json 文件保存到具有类似层次结构的单独目录(例如, .deps / ),然后调用 docker build…

  • 使Dockerfile适应以预先复制(并重命名)单独的目录,然后调用 yarn install --pure-lockfile ...

  • Devise a wrapper script (say, in bash) that copies the required package.json files to a separate directory (say, .deps/) built with a similar hierarchy, then call docker build …
  • Adapt the Dockerfile to copy (and rename) the separate directory beforehand, and then call yarn install --pure-lockfile

所有内容放在一起,可能导致以下情况ing文件:

All things put together, this could lead to the following files:

### ./build.bash ###
#!/bin/bash

tag=copy-example: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/$1" && \
    mkdir -p -- "$(dirname "$dest")" && \
    cp -av -- "$1" "$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" .

### ./Dockerfile ###
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...)

这篇关于使用Glob模式的Docker COPY文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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