Share directories between two containers

adamkdean

Adam K Dean

Posted on June 1, 2015

Share directories between two containers

Originally posted on June 1st, 2015 (more info)

Note: this was posted back in 2015 during the infancy of docker and modern containerisation. These days, we're able to simply use volumes. For more information on the modern way to do this, see docker volumes.

Create data volume container:

docker run -d \
    -v /var/test/ \
    --name test-data \
    busybox
Enter fullscreen mode Exit fullscreen mode

Start one container using it:

docker run -d \
    --name test-1 \
    --volumes-from test-data \
    adamkdean/baseimage bash -c "while true; do echo 'ping'; sleep 5; done"
Enter fullscreen mode Exit fullscreen mode

Start another using it:

docker run -d \
    --name test-2 \
    --volumes-from test-data \
    adamkdean/baseimage bash -c "while true; do echo 'ping'; sleep 5; done"
Enter fullscreen mode Exit fullscreen mode

Attach to the first:

docker exec -ti test-1 bash
root@7bfff33a2309:/# cd /var/test/
root@7bfff33a2309:/var/test# ls
Enter fullscreen mode Exit fullscreen mode

Attach to the second:

docker exec -ti test-2 bash
root@7bfff33a2309:/# cd /var/test/
root@69e9a3cc34a2:/var/test# ls
Enter fullscreen mode Exit fullscreen mode

Create a file in the first one:

root@7bfff33a2309:/var/test# touch test-file
Enter fullscreen mode Exit fullscreen mode

Check in the second one:

root@69e9a3cc34a2:/var/test# ls
test-file
Enter fullscreen mode Exit fullscreen mode

Both test-1 and test-2 are sharing the data on the data volume container test-data.

💖 💪 🙅 🚩
adamkdean
Adam K Dean

Posted on June 1, 2015

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related