36

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.

Can I do this?

mj41
  • 2,459
  • 2
  • 16
  • 10
Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78

4 Answers4

30

Rene's answer works, but you could share data without using the host's directory (container1 ==> container2):

docker run -v /data/myfolder --name container1 image-name-1
docker run --volumes-from container1 image-name-2
Manuel J. Garrido
  • 2,244
  • 1
  • 13
  • 6
  • Hi @Manuel, Thanks for your answer. Could you please elaborate how will I access those file inside container2? – Ganesh Satpute Oct 21 '15 at 06:16
  • 2
    Just an update Manuel. I tried this out and this works great! But the problem it's mounting at the same location as source container. Can I mount it at some point else? – Ganesh Satpute Oct 21 '15 at 06:44
  • Hi, that's not supported, There was [a proposal related to that](https://github.com/docker/docker/issues/11261) but at this moment it's not possible to change the location of the mount point. – Manuel J. Garrido Oct 21 '15 at 07:21
18

Oracle had a an example on their website in 2015 (which is not available any more). Based on this i created

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

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
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • 1
    This link 404's for me: https://docs.oracle.com/cd/E52668_01/E54669/html/section_ffp_yt4_gp.html That's too bad. I'd like to see the original example. – Wyck Oct 01 '18 at 20:46
8

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
Rene M.
  • 2,660
  • 15
  • 24
  • 13
    Thanks for your answer. I have an application running inside one container which will generate some files in directory. Your solution is host ==> container1 and host ==> container. What I want is container1 ==> container2 – Ganesh Satpute Oct 19 '15 at 09:27
0

For me, I just use the --volumes-from to mount 2 or more volumes from one container to one another 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

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. For the second container, you just need to issue this attribute --volumes-from <first container>. This method means that the second container will mount 2 mountpoint from the first container to the second container.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ky Nguyen
  • 45
  • 1
  • 6