Create a named Docker bind mount volume
- 1 min read
Sometimes, when creating tooling for Docker it’s necessary to create a bind mount as a named Docker volume.
A coworker and I spent far too long today trying to figure out the syntax needed for creating a named bind mount Docker volume. We searched and searched the documentation but could not find any documentation stating the exact arguments needed.
Finally I stumbled across the answer:
docker volume create \
--driver local \
-o o=bind \
-o type=none \
-o device="$YOUR_DIRECTORY" \
$VOLUME_NAME
To create a volume named “example-volume” that uses the current directory you’d run:
docker volume create \
--driver local \
-o o=bind \
-o type=none \
-o device="${PWD}" \
example-volume
Note, I use ${PWD}
for cross platform support, you’d likely normally see `pwd`
or $(pwd)
in bash.
Once you have your volume, you may run a container with it attached like normal (again using “example-volume” as the volume name):
docker run --rm -it --init -v example-volume:/test alpine sh