Docker: Mount directory from one container to another

前端 未结 3 1542
执笔经年
执笔经年 2020-12-15 03:42

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

相关标签:
3条回答
  • 2020-12-15 04:20

    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
    
    0 讨论(0)
  • 2020-12-15 04:26

    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
    
    0 讨论(0)
  • 2020-12-15 04:31

    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
    
    0 讨论(0)
提交回复
热议问题