In the last post we saw that any file system change inside a docker container is lost if not committed to the image.

What if we want to share data between containers?

We can use Docker’s Data Volumes and Data Volume Containers .

Data Volumes or Data Volume Containers?

Data Volumes feature allows us to mount a local directory from the host inside a container. That looks good but somehow it breaks the concept of isolation between host and container.

Data Volume Containers create a new container that will store data between container, maintaining the isolation between host and containers.

For the sake of simplicity I’ll use Data Volumes in the next examples even if I agree Data Volume Containers are a better way to manage containers data (READ THIS ).

Use Data Volumes

To use Data Volumes just add a few options when starting the container:

mkdir dockervolume
echo "test123" > ./dockervolume/test.txt

docker run -i -t -v /root/dockervolume:/volume ubuntuplus /bin/bash

Verify the directory is accessible from inside the container:

root@f4dddb24eb83:/# ls /volume/
test.txt
root@f4dddb24eb83:/# cat /volume/test.txt 
test123
root@f4dddb24eb83:/#

Apply some changes to the file:

root@f4dddb24eb83:/# echo "test456" >> /volume/test.txt                                                      
root@f4dddb24eb83:/# cat /volume/test.txt                                                                    
test123
test456

Now leave the container with CTRL+P CTRL+Q and verify the changes were applied to the host filesystem:

root@ubuntusrv:~# cat ./dockervolume/test.txt 
test123
test456

Great! When can now move on to the next post about Docker networking . Stay tuned!