Docker - Bind mount

> Virtualization - (Virtual Machine|Container) > Docker

1 - 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

Advertising

3 - Management

3.1 - 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.

3.1.1 - --mount

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

3.1.2 - -v or --volume

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

  • the first field is the path to the file or directory on the host machine ( for a volume mount, it would be the volume name)
  • the second field is the path where the file or directory is mounted in the container.
  • the third field is optional, and is a comma-separated list of options, such as ro, consistent, delegated, cached, z, and Z.

Example:

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

3.2 - List

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

This shows that:

  • the type of mount,
  • the source directory
  • the destination directory
  • the mode for optional options Example:
  • the mount is read-write. Not Read only
  • the propagation is set to rprivate. The propagation settings control whether a mount on /tmp/a would also be available on /mnt/a. See propagation

4 - Support

4.1 - 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
Advertising

4.2 - 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

4.3 - 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

5 - Documentation / Reference