How to do it...

Perform the following the steps:

  1. Let's begin by creating a named volume using the docker volume create command, as shown in the following code:
    $ docker volume create datavol 
datavol
  1. List the volume using the docker volume ls command, as shown in the following code:
    $ docker volume create ls 
DRIVER VOLUME NAME
local datavol
  1. Launch an interactive container using the docker container run command and -v option to mount the datavol volume to the container, as shown in the following code:
    $ docker container run -it --rm -v datavol:/data alpine / #
  1. Now, let's create a new file at the /data directory of the container and write some text to it:
/ # echo "This is a named volume demo" > /data/demo.txt / #
  1. Exit from the preceding container and remove the container using the docker container rm command.
  1. Now, launch a new container by mounting the datavol volume again and print the content of the demo.txt, as shown in the following code. Here, we are intentionally choosing an ubuntu image just to stress that the Docker volume functions the same way irrespective of the Docker image from which the container is created:

$ docker container run --rm  -v datavol:/data ubuntu cat /data/demo.txt 

This is a named volume demo. Evidently, the text we wrote from the first container is persisted in the datavol volume, and later it is retrieved by the second container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.91.248