将内容从主机操作系统复制到 Docker 映像中而不重建映像 [英] Copy contents from host OS into Docker image without rebuilding image
问题描述
我正在构建一个新映像并将主机操作系统文件夹 D:Programsscrapy 中的内容复制到其中,如下所示: docker build .-t scrapy
I'm building a new image and copy contents from host OS folder D:Programsscrapy into it like so: docker build . -t scrapy
Dockerfile
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir scrapy
COPY scrapy to /root/scrapy
现在,当我将新内容添加到主机操作系统文件夹D:Programsscrapy"时我还想将它添加到图像文件夹root/scrapy",但我不想构建一个全新的图像(这需要很长时间).那么如何保留现有图像并覆盖图像文件夹root/scrapy"的内容.
另外:我不想在每次运行容器时都复制新内容(所以不是在运行时),我只想有一个单独的命令来向现有图像添加更多文件然后在另一个时间基于该图像运行一个新容器.
Now when I add new contents to the host OS folder "D:Programsscrapy" I want to also add it to image folder "root/scrapy", but I DON'T want to build a completely new image (it takes quite a while).
So how can I keep the existing image and just overwrite the contents of the image folder "root/scrapy".
Also: I don't want to copy the new contents EACH time I run the container (so NOT at run-time), I just want to have a SEPARATE command to add more files to an existing image and then run a new container based on that image at another time.
我在这里检查:如何在不重建图像的情况下更新源代码(但不确定 OP 是否会尝试和我做同样的事情)
I checked here: How to update source code without rebuilding image (but not sure if OP tries to do the same as me)
更新 1
检查Dockerfile中VOLUME的目的是什么和 docker --windows 的卷格式
我尝试了以下命令,都导致错误:
I tried the commands below, all resulting in error:
docker:来自守护程序的错误响应:无效的卷规范:''.请参阅docker run --help".
docker: Error response from daemon: invalid volume specification: ''. See 'docker run --help'.
其中 <pathiused>
是例如 D:/Programs/scrapy:/root/scrapy
docker run -v //D/Programs/scrapy:/root/scrapy scrapy
docker run -v scrapy:/root/scrapy scrapy
docker run -it -v //D/Programs/scrapy:/root/scrapy scrapy
docker run -it -v scrapy:/root/scrapy scrapy
根据@Makariy 的反馈使用 cp
命令更新
UPDATE WITH cp
command based on @Makariy's feedback
docker images -a
给出:
REPOSITORY TAG IMAGE ID CREATED SIZE
scrapy latest e35e03c8cbbd 29 hours ago 5.71GB
<none> <none> 2089ad178feb 29 hours ago 5.71GB
<none> <none> 6162a0bec2fc 29 hours ago 5.7GB
<none> <none> 116a0c593544 29 hours ago 5.7GB
mcr.microsoft.com/windows/servercore ltsc2019 d1724c2d9a84 5 weeks ago 5.7GB
我运行 docker run -it scrapy
然后 docker container ls
给出:
I run docker run -it scrapy
and then docker container ls
which gives:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fcda458a14c scrapy "c:\windows\system32…" About a minute ago Up About a minute thirsty_bassi
如果我运行 docker cp D:Programsscrapy scrapy:/root/scrapy
我会得到:
If I run docker cp D:Programsscrapy scrapy:/root/scrapy
I get:
错误:没有这样的容器:路径:scrapy: oot
Error: No such container:path: scrapy: oot
所以在一个单独的 PowerShell 实例中,我然后运行 docker cp D:Programsscrapy thirsty_bassi:/root/scrapy
在 PowerShell 中没有显示任何输出,所以我认为它应该做点什么.
So in a separate PowerShell instance I then run docker cp D:Programsscrapy thirsty_bassi:/root/scrapy
whichs show no output in PowerShell whatsoever, so I think it should've done something.
但是在我的容器实例中,当我转到/root/scrapy 文件夹时,我只能看到构建映像时已经添加的文件,而不是我想要添加的新文件.
But then in my container instance when I goto /root/scrapy folder I only see the files that were already added when the image was built, not the new ones I wanted to add.
另外,我想我在这里将文件添加到容器中,但是没有办法将它添加到图像中吗?不重建整个图像?
Also, I think I'm adding files to the container here, but is there no way to add it to the image instead? Without rebuilding the whole image?
更新 2
我的文件夹结构:
D:Programs
Dockerfile
image_addons
Dockerfile
scrapy
PS D:Programs>docker build .-t scrapybase
成功搭建95676d084e28
成功标记 scrapybase:latest
Successfully built 95676d084e28
Successfully tagged scrapybase:latest
PS D:Programsimage_addons>docker build -t scrapy .
第 2/2 步:将 scrapy 复制到/root/scrapyCOPY 失败:在构建上下文中找不到文件或被 .dockerignore 排除:stat to:文件不存在
Step 2/2 : COPY scrapy to /root/scrapy COPY failed: file not found in build context or excluded by .dockerignore: stat to: file does not exist
Dockerfile A
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
WORKDIR /root/scrapy
Dockerfile B
FROM scrapybase
COPY scrapy to /root/scrapy
推荐答案
如果不(至少部分地)重建 Docker 映像,就无法更改它.但您不必重建全部,只需重建复制您的 scrapy
内容的图层即可.
There's no way you can change a Docker image without (at least partially) rebuilding it. But you don't have to rebuild all of it, you can just rebuild the layer copying your scrapy
content.
您可以优化您的构建以拥有两个图像:
You can optimize your build to have two images:
- 第一张图片是您不想每次都重新构建的静态图片.我们称之为
scrapy-base
. - 第二张也是最后一张图片基于第一张图片
scrapy-base
,并且仅用于复制您的动态scrapy
内容
- First image is your static image you don't want to rebuild each time. Let's call it
scrapy-base
. - Second and final image is based on first image
scrapy-base
and will only exist for the purpose of copying your dynamicscrapy
content
scrapy-base
的 Dockerfile:
scrapy-base
's Dockerfile:
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir scrapy
然后像这样构建它:
docker build -t scrapy-base .
这个命令只需要运行一次.如果您只更改本地 scrapy
文件夹的内容,则无需构建此映像.(如您所见,构建根本不使用它)
This command only needs to be run once. You won't have to build this image if you only change the content of local scrapy
folder. (as you can see, the build does not use it at all)
scrapy
的 Dockerfile:
scrapy
's Dockerfile:
FROM scrapy-base
COPY scrapy /root/scrapy
使用构建命令:
docker build -t scrapy .
第二个构建命令将重新使用之前的静态图像,并且只复制内容,而无需重新构建整个图像.即使有很多文件,它也应该很快.你不需要有一个正在运行的容器.
This second build command will re-use the previous static image and only copy content without having to rebuild the entire image. Even with lots of files it should be pretty quick. You don't need to have a running container.
这篇关于将内容从主机操作系统复制到 Docker 映像中而不重建映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!