Docker Volume Example

Docker Volume Example

In this tutorial we will learn volumes in docker.

Volume is a tool that shares data across containers,so its scope is not limited to single container.It uses modularity concept so it can easily be reused.Images hold static data while volumes holds dynamic data.

Using volumes we can inject different behavior for example our image is using database. Database has same interface but with changing data we can inject different behavior to container.

For demo purpose we will use Cassandra database.

We will create volume and a container will link that volume.After linking is done we will modify in database from db client.As we have modified so we can see that changes from DB client.To check that volume are across container.We will remove container that linked to volume and will create another container and will check that do we have same modification in this container as well.

Follow below steps for demo

  • Add command to create volume container as shown in picture.Here we are using managed volume.
  • docker run -d --volume /var/lib/cassandra/data --name cassandra_shared alpine echo Data Container


  • We will create one more container that will pull volume definition from volume container created above
  • docker run -d --volumes-from cassandra_shared --name volume_connect_1 cassandra:latest



  • Now to modify in volume we need a client so that we can modify in DB
  • Create client for that
  • docker run --rm -it --link volume_connect_1:cass cassandra:latest cqlsh cass

  • It will open prompt


  • On prompt we can execute any cassandra command.
  • We will create one keyspace but first we will see how many already there.
  • describe keyspaces;



  • Now we will create one keyspace
  • create keyspace preparationforinterview_keyspace with replication = { 'class' : 'SimpleStrategy', 'replication_factor': 1 };
  • It will create one keyspace with name preparationforinterview_keyspace
  • describe keyspaces;



  • quit the client prompt
  • quit

  • Now to check that created volume will be across other container,we will delete container volume_connect_1
  • docker stop volume_connect_1
    docker rm -vf volume_connect_1
  • Now we will create one more container


  • docker run -d --volumes-from cassandra_shared --name volume_connect_2 cassandra:latest


    docker run --rm -it --link volume_connect_2:cass cassandra:latest cqlsh cass
    describe keyspaces;


  • It shows that volume is same for other container as well.



Visit Others Links

Docker Introduction
Docker Features
Docker Installation On Window
Docker Hello World Example On Window
Software Download In Docker Using command line
Software Search From Docker Hub Web Console
Docker Image Install from File System
Docker Volume Example
Installation Of Mysql in Docker With Example
Docker Frequently Used Commands
Top Docker Interview Question
© 2019 PreparationForInterview.com