Concept Of Data Container In Docker And How To Use It

Posted By : Jatin Gupta | 24-Jan-2018

Docker, by default, doesn't come with persistent storage, which is sometimes an issue for clients that want to run in containers. In any case, there are approaches to achieve persistent capacity while using Docker containers. There are myriad approaches to accomplish the same.

One ywa is to use Data Containers. This blog will introduce you how to use Data Containers and it's advantages.


Data Containers are containers whose sole responsibility is to be a place to store/manage data. It can also be used as a shared volume for Docker containers.

Create Container
To create a Data Container we first create a container with a name for future reference. We are using ubuntu image for this case


vagrant@vagrant:~$ docker create -v /config --name dataContainer ubuntu:16.04 


Copy Files

With the data container in place, we can now copy files from local folder into the docker. To copy files into a data container the command docker cp is used. The following command will copy the config file into the dataContainer in the config folder.


vagrant@vagrant:~$ docker cp config dataContainer:/config/ 


Mount Volume from Data Container

Now our Data Container has our config.conf, we can share the container when we launch other secondary containers demanding the same configuration file.

By using the --volumes-from <container> parameter we can use the mounted volume from data container inside the secondary container being launched. Here, we'll launch an Ubuntu 16.04 container which has reference to our Data Container. On listing the config folder, it will display the files from the relied data container.

vagrant@vagrant:~$ docker run --volumes-from dataContainer ubuntu ls /config 
By any chance if /config directory is already existing then, the volumes-from will overwrite the volume. We can also map numerous volumes to a single container.
Mount the same volume in multiple containers and use the data container as a shared volume.
Example : 

 vagrant@vagrant:~$ docker run -dit --volumes-from DataContainer --name testContainer1 ubuntu:16.04          
 vagrant@vagrant:~$ docker run -dit --volumes-from DataContainer --name testContainer2 ubuntu:16.04          
 vagrant@vagrant:~$ docker exec -it testContainer1 /bin/bash                                                                              
 root@17657edd1a5d:~# cd config/                                                                                                                       
 root@17657edd1a5d:~# touch test                                                                                                                       
 vagrant@vagrant:~$ docker exec -it testContainer2 /bin/bash                                                          
 root@6d43c2551b0c:~# cd config/                                                                                                       
 root@6d43c2551b0c:~# ls                                                                                                                    
 test  config.conf                                                                                                                                                        
This shows that test file has been shared between both dockers.
Mount the volume providing limited access : 
You can also limit the access of data container's volume in certain docker containers.


 vagrant@vagrant:~$ docker run -dit --volumes-from DataContainer:ro --name testContainer3 ubuntu:16.04 
This will allow testContainer3 to only read the contents of the volume.
To see where the data container's volume is created on host machine : 
 vagrant@vagrant:~$ docker inspect --format='{{.Mounts}}' dataContainer                                               
 [{volume 001bc251c3afd31c3be6f587bf14ef4885f20da9bb133653c949816a75db8531
 /var/lib/docker/volumes/001bc251c3afd31c3be6f587bf14ef4885f20da9bb133653c949816a75db8531/_data /config local  true }] 
Hence, the volume is created at
 /var/lib/docker/volumes/001bc251c3afd31c3be6f587bf14ef4885f20da9bb133653c949816a75db8531/_data /config
Export / Import Data Container :
The main advantage of using Data Container is we can export it to any other system easily when necessary.
We can export it to a .tar file.
docker export dataContainer
The command  docker import dataContainer.tar
will import the data container back into docker.
Conclusion :
In this blog we learnt about :
1. The concept of Data Containers in docker.
 2. How to use that Data Container as a shared volume.
3. How to restrict access to volume from some containers.
4. How to export/import the volume.  
Request for Proposal

Recaptcha is required.

Sending message..