Let's see a small practical example of how Docker volumes work. We will run a small Ubuntu container:
docker run -it -v /Users/your-username/Desktop/data:/test/data ubuntu-v is the volume flag
/Users/your-username/Desktop/data is the host machine path (our local machine's folder)
/test/data is the container path (where it gets mounted inside the container)This creates a mapping (bind mount) between:
/Users/your-username/Desktop/data/test/dataSo whatever data is stored inside the container at /test/data is also reflected on the host machine.
Inside the container, create two files:
cd /test/data
touch index.html
touch server.jsNow check your host machine (Desktop folder):
/Desktop/dataYou will see:
index.htmlserver.jsEven though these files were created inside the container, they are now visible on the host machine as well.
exitStart the container again:
docker run -it -v /Users/your-username/Desktop/data:/test/data ubuntuYou will still see the same files inside /test/data.
Even if we completely delete the container:
docker rm <container-id>Check the host machine folder:
/Desktop/dataThe files are still present:
index.htmlserver.jsEven after:
The data still persists on the host system.
Because Docker volumes (bind mounts in this case) store data outside the container filesystem.
So:
Container data → mapped to → Host machine directory-v flag is used to create a volume (bind mount)