用`docker run`覆盖文件
也许我在阅读文档时错过了这个,但是在发出docker run
命令时有没有办法覆盖容器文件系统上的文件呢?
类似于Dockerfile
COPY
命令的东西? 这里的主要愿望是能够获取特定的Docker image
,并向上旋转几个相同的镜像,但使用不同的configuration文件。 (我更喜欢用环境variables来做这件事,但是我是Dockerizing的应用程序并不偏向于此)。
你有几个select。 使用像docker-compose
这样的东西,你可以使用基本图像作为模板自动为每个容器构build一个独特的图像。 例如,如果你有一个看起来喜欢docker-compose.yml
:
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
会启动一切)。
我已经把上面的例子放在这里了 。
只使用Docker命令行,您可以使用主机卷挂载,这允许您将文件挂载到容器以及目录中。 再次以我的thttpd
为例,可以使用下面的-v
参数来覆盖容器中的/index.html
,其中包含您所select的内容:
docker run -v index.html:/index.html larsks/thttpd
你可以用docker-compose
通过volume
条目完成同样的事情:
container0: image: larsks/thttpd volumes: - ./container0/index.html:/index.html container1: image: larsks/thttpd volumes: - ./container1/index.html:/index.html
我build议使用build
机制更有意义,如果您要覆盖很多文件,而使用卷对一个或两个文件罚款。
这两种机制之间的一个主要区别在于,在构build映像时,每个容器都将具有这些文件的副本 ,而在使用卷装载时,映像内文件的更改将反映在主机文件系统上 。