用docker run覆盖文件 [英] Overwrite files with `docker run`
问题描述
也许在阅读文档时会丢失此信息,但是在发出 docker run
命令时是否有一种方法可以覆盖容器文件系统上的文件?
Maybe I'm missing this when reading the docs, but is there a way to overwrite files on the container's file system when issuing a docker run
command?
类似于 Dockerfile
COPY
命令?这里的主要需求是能够获取特定的 Docker映像
,并旋转同一张映像中的几个,但使用不同的配置文件。 (我宁愿使用环境变量来执行此操作,但我正在进行Dockerizing的应用程序并不属于此。)
Something akin to the Dockerfile
COPY
command? The key desire here is to be able to take a particular Docker image
, and spin several of the same image up, but with different configuration files. (I'd prefer to do this with environment variables, but the application that I'm Dockerizing is not partial to that.)
推荐答案
您有几种选择。使用 docker-compose
之类的东西,您可以使用基本映像作为模板为每个容器自动构建唯一映像。例如,如果您有一个 docker-compose.yml
看起来像:
You have a few options. Using something like docker-compose
, you could automatically build a unique image for each container using your base image as a template. For example, if you had a docker-compose.yml
that look liked:
container0:
build: container0
container1:
build: container1
然后在 container0 / Dockerfile
中,您有:
FROM larsks/thttpd
COPY index.html /index.html
在 container0 / index.html
内,您拥有
想要的任何内容,然后运行 docker-compose build
为每个条目生成唯一的
图像(并运行 docker-compose up
将启动
一切启动)。
And inside container0/index.html
you had whatever content you
wanted, then running docker-compose build
would generate unique
images for each entry (and running docker-compose up
would start
everything up).
我在上述
此处。
仅使用Docker命令行,就可以使用主机卷挂载
,它允许您将文件挂载到一个容器以及
目录。再次以我的 thttpd
为例,可以使用 -v
参数后面的
覆盖 /index.html
在容器
中,其中包含您选择的内容:
Using just the Docker command line, you can use host volume mounts,
which allow you to mount files into a container as well as
directories. Using my thttpd
as an example again, you could use the
following -v
argument to override /index.html
in the container
with the content of your choice:
docker run -v index.html:/index.html larsks/thttpd
您可以通过
volume
条目通过 docker-compose
完成相同的操作:
And you could accomplish the same thing with docker-compose
via the
volume
entry:
container0:
image: larsks/thttpd
volumes:
- ./container0/index.html:/index.html
container1:
image: larsks/thttpd
volumes:
- ./container1/index.html:/index.html
我建议使用 build
机制更有意义覆盖许多文件,而使用卷则适合一个或两个文件。
I would suggest that using the build
mechanism makes more sense if you are trying to override many files, while using volumes is fine for one or two files.
这两种机制之间的主要区别在于,在构建映像时,每个容器都会有一个复制中的文件,在使用卷挂载时,在映像中对该文件所做的更改将在主机文件系统上反映 。
A key difference between the two mechanisms is that when building images, each container will have a copy of the files, while using volume mounts, changes made to the file within the image will be reflected on the host filesystem.
这篇关于用docker run覆盖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!