Table of Contents

Docker - Bind mount

About

bind mount is type of mount. It's one of the three way on how you can persist data from a container. See Docker - Data Persistence

Management

Mount

The file or directory is referenced by its full or relative path on the host machine. The file or directory does not need to exist on the Docker host already.

–mount

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

-v or –volume

The -v or --volume options when mounting a bind mount, consists of three fields, separated by colon characters :

Example:

docker run -it --rm -v /c/tmp:/pathInContainer/  ubuntu bash
docker run ^
    --name containerName ^
    -v %cd%:/var/www/html/ ^
    imageName

List

docker inspect containerName
"Mounts": [
	{
		"Type": "bind",
		"Source": "/host_mnt/c/code/bdm",
		"Destination": "/ansible/playbooks",
		"Mode": "",
		"RW": true,
		"Propagation": "rprivate"
	}
],

This shows that:

Support

Mount path must be absolute

I got this error using MGINX. Below is a workaround

SOURCE_MOUNT_POINT=$(cygpath -u $(readlink -f ../myRelativePath))

# then  mount
docker run \
    --mount type=bind,source=/$SOURCE_MOUNT_POINT,target=/var/www/html \
   repo/image:tag

Error while creating mount source path: file exists

Error response from daemon: error while creating mount source path '/host_mnt/d/dokuwiki': mkdir /host_mnt/d: file exists

Just restart docker

Mount denied: The source path doesn't exist and is not known to Docker

On Windows 10

docker: Error response from daemon: Mount denied:
The source path "D:/tmp"
doesn't exist and is not known to Docker.
See 'docker run --help'.

Solution: The credentials for the mount have expired, reset them

Docker Shared Drive New Credentials

Documentation / Reference