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
config.conf
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:~$
vagrant@vagrant:~$ docker exec -it testContainer1 /bin/bash
root@17657edd1a5d:~# cd config/
root@17657edd1a5d:~# touch test
vagrant@vagrant:~$
vagrant@vagrant:~$ docker exec -it testContainer2 /bin/bash
root@6d43c2551b0c:~# cd config/
root@6d43c2551b0c:~# ls
test config.conf
vagrant@vagrant:~$ docker run -dit --volumes-from DataContainer:ro --name testContainer3 ubuntu:16.04
vagrant@vagrant:~$ docker inspect --format='{{.Mounts}}' dataContainer
[{volume 001bc251c3afd31c3be6f587bf14ef4885f20da9bb133653c949816a75db8531
/var/lib/docker/volumes/001bc251c3afd31c3be6f587bf14ef4885f20da9bb133653c949816a75db8531/_data /config local true }]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Jatin Gupta
Jatin is a DevOps trainee. He ha deep interest in python and cloud technologies. He likes to read about science/history and fiction, listening to music and explore new places.