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

Author

Cody Craven enjoys sharing tidbits of information that he learns through his life-long journey developing websites and configuring web servers.

When at work, Cody can often be found banging his head against his keyboard for hours days weeks-on-end trying to solve technical issues that would otherwise prevent his teammates from creating Member value at AAA.

All content that Cody contributes to this site are his own ideas and do not necessarily represent AAA's positions or opinions.