有没有办法在单个 ADD/COPY 命令(dockerfile)中同时 ADD/COPY 目录和文件?
Is there a way to ADD/COPY both directory and file in a single ADD/COPY command (dockerfile)?
在我的 dockerfile 中,我试图 ADD/COPY 一个文件和一个目录到 destinationFolder。预期输出为:
destinationFolder/test.txt
destinationFolder/test_dir/*
但是,在 ADD command (as mentioned in note) 中,不会复制目录本身,只会复制其内容。
ADD test.txt test_dir /destinationFolder/
有没有办法使用单个 ADD/COPY 命令实现所需的输出?
2 种解决方案,具体取决于您当前项目目录的具体内容。基线是相同的:将所有内容放在同一个目录中,并将该目录的内容直接推送到图像中的目标目录。
此外,如 Docker best practice 中所述,如果您不需要任何最新的特殊功能,我建议您使用 COPY
而不是 ADD
。
将您的内容移至单独的文件夹
所有你想推送到目标文件夹的内容都可以放在你一次推送的专用文件夹中:
在你的项目目录中你可以
mkdir imageContents
mv test.text test_dir imageContents
然后您将 Docker 文件修改为:
COPY imageContents destinationFolder/
从您的项目根目录复制确切的结构
这个想法是一样的,只是你将根目录或你的项目直接推送到你的图像。这也适用于您放置在那里并且不会被忽略的任何其他文件夹。请注意,您可以将其用于现有文件夹结构以简单地添加一些文件(如 /etc/
结构)。
创建一个 .dockerignore
文件以确保您只复制相关内容:
.dockerignore
Dockerfile
.git
# Add any other files that should be out of context
# Dummy examples below
.yamllint
mysecretfiles
重新创建您想要推送到图像的确切文件结构,在您的情况下:
mkdir destinationFolder
mv test.txt test_dir destinationFolder
最后添加一些你想推送到你的镜像的其他文件
mkdir -p etc/myapp
echo "config1=myvalue" > etc/myapp/myapp.conf
并将您的 Docker 文件修改为:
COPY . /
制作你自己的方法
您可以轻松地混合使用上述两种方法来满足您对图像文件中任何目标的确切需求。
在我的 dockerfile 中,我试图 ADD/COPY 一个文件和一个目录到 destinationFolder。预期输出为:
destinationFolder/test.txt
destinationFolder/test_dir/*
但是,在 ADD command (as mentioned in note) 中,不会复制目录本身,只会复制其内容。
ADD test.txt test_dir /destinationFolder/
有没有办法使用单个 ADD/COPY 命令实现所需的输出?
2 种解决方案,具体取决于您当前项目目录的具体内容。基线是相同的:将所有内容放在同一个目录中,并将该目录的内容直接推送到图像中的目标目录。
此外,如 Docker best practice 中所述,如果您不需要任何最新的特殊功能,我建议您使用 COPY
而不是 ADD
。
将您的内容移至单独的文件夹
所有你想推送到目标文件夹的内容都可以放在你一次推送的专用文件夹中:
在你的项目目录中你可以
mkdir imageContents
mv test.text test_dir imageContents
然后您将 Docker 文件修改为:
COPY imageContents destinationFolder/
从您的项目根目录复制确切的结构
这个想法是一样的,只是你将根目录或你的项目直接推送到你的图像。这也适用于您放置在那里并且不会被忽略的任何其他文件夹。请注意,您可以将其用于现有文件夹结构以简单地添加一些文件(如 /etc/
结构)。
创建一个 .dockerignore
文件以确保您只复制相关内容:
.dockerignore
Dockerfile
.git
# Add any other files that should be out of context
# Dummy examples below
.yamllint
mysecretfiles
重新创建您想要推送到图像的确切文件结构,在您的情况下:
mkdir destinationFolder
mv test.txt test_dir destinationFolder
最后添加一些你想推送到你的镜像的其他文件
mkdir -p etc/myapp
echo "config1=myvalue" > etc/myapp/myapp.conf
并将您的 Docker 文件修改为:
COPY . /
制作你自己的方法
您可以轻松地混合使用上述两种方法来满足您对图像文件中任何目标的确切需求。