使用 Docker 文件中的 COPY 将文件夹从 Windows 主机操作系统复制到 Docker 映像中 [英] Copy folder from Windows host OS into Docker image using COPY in Docker file
问题描述
我想将文件夹 D:\test
从我的主机操作系统 (Windows) 复制到我的 docker 镜像.
I want to copy folder D:\test
from my host OS (Windows) to my docker image.
我的 Docker 文件是 D:\Programs>
My Docker file is D:\Programs>
Docker 文件
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
COPY D:/test to /root/test
#USING 'COPY' here instead of 'ADD' because of https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile
从文件夹 D:\Programs>我运行命令 docker build .-t 测试
From folder D:\Programs> I run command docker build . -t test
但我得到错误:
复制失败:在构建上下文中找不到文件或被 .dockerignore 排除:stat test:文件不存在
COPY failed: file not found in build context or excluded by .dockerignore: stat test: file does not exist
我尝试了命令 COPY D:/test to/root/test
和 COPY ./test to/root/test
我知道发生此错误是因为我从中复制的路径必须与构建上下文相关(docker build 中的 .).它不能是构建上下文之外系统上的任意路径.
I know this error occurs because the path I copy from has to be relative from the build context (the . in docker build .). It cannot be an arbitrary path on the system outside of the build context.
我想通过使用 .
我会在我的构建上下文 D:\Programs>
的上下文 D:\
中,但我可能不会.我的 COPY 命令应该是什么样的?
I thought by using .
I'd be in context D:\
from my build context D:\Programs>
, but I guess not. What should my COPY command look like?
我已经在这里检查过:
- https://forums.docker.com/t/copy-files-from-windows-host-to-ubuntu-container/28757
- 从 Windows 复制 Dockerfile文件系统到 docker 容器
- Dockerfile:将目录从 Windows 主机复制到 Docker 容器
更新 1
我将 test
文件夹放在 D:\Programs
中,我的 Dockerfile 也驻留在那里,所以我现在有了结构:
I placed the test
folder in D:\Programs
where my Dockerfile also resides, so I now have structure:
D:\Programs
\Test
Dockerfile
然后我再次运行构建命令,我尝试了 COPY test to/root/test
和 COPY ./test to/root/test
,但都失败了同样的上述错误.
I then ran the build command again where I tried COPY test to /root/test
and COPY ./test to /root/test
, but both fail with the same aforementioned error.
推荐答案
我假设您只在 COPY D:/test to/root/test
中添加了 to
缩写,但我建议无论如何都要检查一下.
I assume you've only added to
in COPY D:/test to /root/test
for abbreviation but I recommend checking that anyway.
如果您确实需要 D:/Programs
中的内容并且无法将其移动到当前构建上下文中的文件夹,那么我认为有两个常规选项:
If you really need what's in D:/Programs
and can't move it to a folder inside the current build context there are two general options as I see it:
更改构建上下文 - 我建议将构建上下文设置为
D:/
并在D:/
目录中添加除Programs
到.dockerignore
文件,然后您可以从D:/
某处的某个子目录引用 Dockerfile,并在构建时复制您的文件想要
changing the build context - I'd suggest setting the build context as
D:/
and adding every folder in theD:/
directory other thanPrograms
to the.dockerignore
file, then you can reference the Dockerfile from some subdirectory somewhere inD:/
and copy your files at build time as you wanted
在运行时创建绑定挂载 - 您可以在运行时将 D:/Programs
目录映射到容器内的文件夹 (/my-programs
)然后将该文件夹中的文件复制到 /root/test
或仅使用绑定安装文件夹 (/my-programs
)
creating a bind mount in runtime - you can map the D:/Programs
directory to a folder inside the container (/my-programs
) at runtime then copy the files from that folder to /root/test
or just use the bind mount folder (/my-programs
)
如果将来可以在构建时使用绑定安装,您将能够在创建 docker 映像时使用第二个选项
If in the future it will be available to use bind mounts in build time you'd be able to use the second option while creating the docker image
这篇关于使用 Docker 文件中的 COPY 将文件夹从 Windows 主机操作系统复制到 Docker 映像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!