<p>A <strong>Data Volume Container</strong> is the perfect way to share some persistent data between containers. Let's see how to create one to share the persistent storage for postgres database.</p>
<hr id="system-readmore" />
<p>We will create a new named container with a volume to share. Even if this container doesn’t run an application, it reuses the training/postgres image so that all containers are using layers in common, saving disk space. Start my creating the container,exposing the folder "dbdata":</p>
<pre class="brush:bash">$ docker create -v /dbdata --name dbdata postgres /bin/true</pre>
<p>Now you can use the <strong>--volumes-from</strong> flag to mount the /dbdata volume in another Docker container.</p>
<pre class="brush:bash">$ docker run -d --volumes-from dbdata --name data1 postgres</pre>
<p>And you can add more:</p>
<pre class="brush:bash">$ docker run -d --volumes-from dbdata --name data2 postgres</pre>
<p>What happens if your postgres image contains already a directory called /dbdata ? Then mounting the volumes from the dbdata container hides the /dbdata files from the postgres image. As a result only the files from the dbdata container are visible.<br /><br />You can even use multiple --volumes-from parameters to chain together multiple data volumes from several containers. You can also extend the chain by mounting the volume that came from the dbdata container in yet another container via the data1 or data2 containers.</p>
<pre class="brush:bash">$ docker run -d --name data3 --volumes-from data1 postgres</pre>

0
0
0
s2smodern

W

i

l

d

F

l

y

 

c

h

e

a

t

s

h

e

e

t