简体   繁体   English

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

[英]Docker: Mount directory from one container to another

I have two docker images.我有两个泊坞窗图像。 One of the docker image (from first container), when ran, generates some files, which needs to be consumed by the another container.其中一个 docker 镜像(来自第一个容器)在运行时会生成一些文件,这些文件需要由另一个容器使用。

Can I do this?我可以这样做吗?

Rene's answer works, but you could share data without using the host's directory (container1 ==> container2): Rene 的回答有效,但您可以在不使用主机目录(container1 ==> container2)的情况下共享数据:

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

Oracle had a an example on their website in 2015 (which is not available any more). 2015 年,Oracle 在其网站上提供了一个示例(不再提供)。 Based on this i created基于此我创建

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

Dockerfile.data 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

for the data image and对于数据图像和

Dockerfile文件

# Ubuntu image
FROM ubuntu:14.04

for the image to test the use of the other data only volume.用于图像测试使用其他数据仅卷。

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

builds these images构建这些图像

and they both show in my images list now:它们现在都显示在我的图像列表中:

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

running and testing is done with运行和测试完成

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

which shows:这表现了:

0ebb78f209169fb7d281bb6b06851b33af7a98488c3a38cf25ac92fe983fff43
file1.html
file2.html
index.html

It's very simple.这很简单。 You have to share one directory to two different containers, then have both access to the same data in that directory.您必须将一个目录共享给两个不同的容器,然后两者都可以访问该目录中的相同数据。

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

For me, I just use the --volumes-from to mount 2 or more volumes from one container to one another Dockerfile1对我来说,我只是使用--volumes-from将 2 个或更多卷从一个容器安装到另一个 Dockerfile1

FROM alpine:3.7

MAINTAINER john "john@gmail.com"

ENV REFRESHED_AT=2021-02-01 \

VOLUME ["/mount1", "/mount2"]

CMD ["tail", "-f", "/dev/null"]

EXPOSE 80 

Dockerfile2 Dockerfile2

FROM ubuntu:16.04

MAINTAINER john "john@gmail.com"

ENV REFRESHED_AT=2021-02-01 \

VOLUME ["/mount1", "/mount2"]

CMD ["tail", "-f", "/dev/null"]

EXPOSE 80 

You build 2 of them, then you create the first container by creating a folder in or outside the Dockerfile.您构建了其中的 2 个,然后通过在 Dockerfile 内部或外部创建一个文件夹来创建第一个容器。 For the second container, you just need to issue this attribute --volumes-from <first container> .对于第二个容器,您只需要发出此属性--volumes-from <first container> This method means that the second container will mount 2 mountpoint from the first container to the second container.这种方法意味着第二个容器将从第一个容器挂载 2 个挂载点到第二个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM