Docker Volume Mount

Docker provides two ways of data persistence: 1) named volume and 2) bind mount. Named volume is a recommended way because Docker manages the volume and the volume can be reused in different services and in multiple containers. Bind mount is basically mounting a path of a host machine. The advantage of the bind mount is that changes in the host machine is instantly reflected into a docker container. I experienced that bind mount is very hard in Windows host machine.

Named Volume
Docker volume is created inside the host docker machine.

Linux container: /var/lib/docker/volumes/volume-name
Windows container: C:\ProgramData\Docker\volumes\volume-name

  • docker volume create
  • docker volume ls
  • docker volume inspect
  • docker volume rm

docker run -d -it --name devtest -v myvol:/app nginx:latest

docker run -d -it --name devtest --mount source=myvol2,target=/app nginx:lates

Bind Mount
A local path or directory is mapped to a docker container.

docker run -d -it --name devtest -v $(pwd)/target:/app nginx:latest

docker run -d -it --name devtest --mount type=bind,source=$(pwd)/target,target=/app nginx:latest