我有两个Docker镜像。其中一个Docker镜像(来自第一个容器)在运行时会生成一些文件,需要被另一个容器消费。
我可以这样做吗?
我有两个Docker镜像。其中一个Docker镜像(来自第一个容器)在运行时会生成一些文件,需要被另一个容器消费。
我可以这样做吗?
Rene 的回答是可行的,但你可以不使用主机目录(container1 ==> container2)来共享数据:
docker run -v /data/myfolder --name container1 image-name-1
docker run --volumes-from container1 image-name-2
Oracle在2015年的网站上有一个示例(现已不再提供)。基于这个示例,我创建了:
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
为了测试图像,仅使用其他数据来进行卷积。
docker build -t bitplan/dataonly:0.0.1 -f Dockerfile.data .
docker build -t bitplan/dataexample:0.0.1 .
构建这些镜像
它们现在都显示在我的镜像列表中:
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
运行和测试是通过以下方式完成的
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
非常简单。您需要将一个目录共享给两个不同的容器,然后两者都可以访问该目录中的相同数据。
docker run -v myfolder:/data/myfolder image-name-1
docker run -v myfolder:/data/myfolder image-name-2
--volumes-from
将一个容器中的2个或多个卷挂载到另一个容器中。 Dockerfile1FROM 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
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
您需要构建两个容器,然后通过在Dockerfile内或外创建文件夹来创建第一个容器。对于第二个容器,您只需要使用--volumes-from <first container>
属性即可。这种方法意味着第二个容器将从第一个容器挂载2个挂载点到第二个容器。