如何使用 Dockerfile 在一层中复制多个文件?
How to copy multiple files in one layer using a Dockerfile?
以下Dockerfile
包含四个COPY
层:
COPY README.md ./
COPY package.json ./
COPY gulpfile.js ./
COPY __BUILD_NUMBER ./
如何使用一层来复制这些文件?尝试了以下方法:
COPY [
"__BUILD_NUMBER ./",
"README.md ./",
"gulpfile ./",
"another_file ./",
]
COPY README.md package.json gulpfile.js __BUILD_NUMBER ./
或
COPY ["__BUILD_NUMBER", "README.md", "gulpfile", "another_file", "./"]
您还可以在源文件规范中使用通配符。 See the docs for a little more detail.
目录很特别!如果你写
COPY dir1 dir2 ./
实际效果如
COPY dir1/* dir2/* ./
如果您想在单个命令中将多个目录(不是它们的内容)复制到一个目标目录下,您需要设置构建上下文,以便您的源目录位于一个公共目录下 parent然后 COPY
即 parent.
简单
COPY README.md package.json gulpfile.js __BUILD_NUMBER ./
来自 doc
If multiple resources are specified, either directly or due to
the use of a wildcard, then must be a directory, and it must
end with a slash /.
COPY <all> <the> <things> <last-arg-is-destination>
但这里是文档的重要摘录:
If you have multiple Dockerfile steps that use different files from
your context, COPY them individually, rather than all at once. This
ensures that each step’s build cache is only invalidated (forcing the
step to be re-run) if the specifically required files change.
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy
可能值得一提的是,您还可以创建一个 .dockerignore
文件,以排除您 不想 复制的文件和目录:
https://docs.docker.com/engine/reference/builder/#dockerignore-file
Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.
为了便于审查,您可能需要多行 COPY
命令。例如。如果要复制的文件列表很长。
反斜杠转义行尾处理方式如下:
COPY __BUILD_NUMBER \
README.md \
gulpfile \
another_file \
./
我可以为 RUN
命令找到 example with backslash,但不能为 COPY
。它也适用于 COPY
。
原始答案警告异常处理目录。
如果您想通过只包含一个或多个目录和零个或多个特定文件的目录来实现这一点,您可以使用 .dockerignore
:
.dockerignore
文件忽略除 src
目录、test
目录 package.json
文件和 package-lock.json
文件之外的所有内容:
*
!src
!test
!package.json
!package-lock.json
然后Dockerfile
:
COPY . ./
这将仅包含 .dockerignore
文件中例外情况中包含的文件。
以下Dockerfile
包含四个COPY
层:
COPY README.md ./
COPY package.json ./
COPY gulpfile.js ./
COPY __BUILD_NUMBER ./
如何使用一层来复制这些文件?尝试了以下方法:
COPY [
"__BUILD_NUMBER ./",
"README.md ./",
"gulpfile ./",
"another_file ./",
]
COPY README.md package.json gulpfile.js __BUILD_NUMBER ./
或
COPY ["__BUILD_NUMBER", "README.md", "gulpfile", "another_file", "./"]
您还可以在源文件规范中使用通配符。 See the docs for a little more detail.
目录很特别!如果你写
COPY dir1 dir2 ./
实际效果如
COPY dir1/* dir2/* ./
如果您想在单个命令中将多个目录(不是它们的内容)复制到一个目标目录下,您需要设置构建上下文,以便您的源目录位于一个公共目录下 parent然后 COPY
即 parent.
简单
COPY README.md package.json gulpfile.js __BUILD_NUMBER ./
来自 doc
If multiple resources are specified, either directly or due to the use of a wildcard, then must be a directory, and it must end with a slash /.
COPY <all> <the> <things> <last-arg-is-destination>
但这里是文档的重要摘录:
If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once. This ensures that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy
可能值得一提的是,您还可以创建一个 .dockerignore
文件,以排除您 不想 复制的文件和目录:
https://docs.docker.com/engine/reference/builder/#dockerignore-file
Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.
为了便于审查,您可能需要多行 COPY
命令。例如。如果要复制的文件列表很长。
反斜杠转义行尾处理方式如下:
COPY __BUILD_NUMBER \
README.md \
gulpfile \
another_file \
./
我可以为 RUN
命令找到 example with backslash,但不能为 COPY
。它也适用于 COPY
。
原始答案警告异常处理目录。
如果您想通过只包含一个或多个目录和零个或多个特定文件的目录来实现这一点,您可以使用 .dockerignore
:
.dockerignore
文件忽略除 src
目录、test
目录 package.json
文件和 package-lock.json
文件之外的所有内容:
*
!src
!test
!package.json
!package-lock.json
然后Dockerfile
:
COPY . ./
这将仅包含 .dockerignore
文件中例外情况中包含的文件。