Docker:将目录从一个容器挂载到另一个容器

我有两个docker图像。 其中一个Docker镜像(来自第一个容器)运行时会生成一些文件,这些文件需要被另一个容器使用。

我可以这样做吗?

Rene的答案有效,但是你可以在不使用主机目录(container1 ==> container2)的情况下共享数据:

docker run -v /data/myfolder --name container1 image-name-1 docker run --volumes-from container1 image-name-2 

这很简单。 您必须将一个目录共享到两个不同的容器,然后同时访问该目录中的相同数据。

 docker run -v myfolder:/data/myfolder image-name-1 docker run -v myfolder:/data/myfolder image-name-2 

https://docs.oracle.com/cd/E52668_01/E54669/html/section_ffp_yt4_gp.html

显示一个例子。 基于这个我创build

https://github.com/BITPlan/docker-stackoverflowanswers/tree/master/33232991

Dockerfile.data

 # Dockerfile that modifies ubuntu to create a data volume container FROM ubuntu:14.04 RUN mkdir -p /var/www/html RUN echo "This is the content for file1.html" > /var/www/html/file1.html RUN echo "This is the content for file2.html" > /var/www/html/file2.html RUN echo "This is the content for index.html" > /var/www/html/index.html VOLUME /var/www/html ENTRYPOINT /usr/bin/tail -f /dev/null 

为数据图像和

Dockerfile

 # Ubuntu image FROM ubuntu:14.04 

为图像testing其他数据的使用量。

 docker build -t bitplan/dataonly:0.0.1 -f Dockerfile.data . docker build -t bitplan/dataexample:0.0.1 . 

build立这些图像

现在他们都显示在我的图片列表中:

 docker images | grep data wf@mars:~/source/docker/stackoverflow2> docker images | grep data bitplan/dataonly 0.0.1 aa6aeb923f55 9 minutes ago 188.4 MB bitplan/dataexample 0.0.1 a005e6b7dd01 7 days ago 188.4 MB 

运行和testing完成

 docker run -d --name html bitplan/dataonly:0.0.1 docker run --volumes-from html bitplan/dataexample:0.0.1 ls /var/www/html 

这表明:

 0ebb78f209169fb7d281bb6b06851b33af7a98488c3a38cf25ac92fe983fff43 file1.html file2.html index.html