Docker Lab, Excercise & Assignment – 3 – Docker Volume

Simple Flow to Create Volume /john1


$ docker run -it -v /john1 busybox
$ cd john1
$ ls
$ touch file1
$ ls
CTRL + P + Q
$ docker ps

The volume is exist even you restarts.


$ docker restart cont_id
$ docker exec cont_id ls /john1

Lets build a image with predefined Volume


$ vi Dockerfile
FROM ubuntu
ADD file1 /john2/file
VOLUME /john2
CMD ["/bin/bash"]

$ docker build myimage1 .
$ docker images
$ docker run -it myimage1
$ ls

Mapping between Host Volume and Containers


$ docker run -it -v /home/vagrant/john3:/john3 myimage1
$ cd john3
$ ls 
$ touch file5
$ exit
$ cd john3
$ ls

Senario 1 – Read only File Systems


$ docker run -it -v /home/vagrant/john3:/john3 myimage1
$ cd /john3
$ vi filex.txt # Try to save it and observe the output

Senario 2 – Map the Specific files between Host and Containers


$ docker run -it -v ~/.bash_history:/.bash_history myimage1

Lets clean up containers for clean next lab


$ docker kill $(docker ps -q)
$ docker rm $(docker ps -aq)

Senario 3 – Lets run the container with volume from another container volume


$ docker run -it -d --name cont1 -v /VolX myimage1
$ docker run -it --name cont2 --volumes-from cont myimage1

Locating a volume


docker inspect image_name # Look for mount section

More referencehttps://docs.docker.com/engine/tutorials/dockervolumes/

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)