virtualbox – Local folder mounted as empty on Ubuntu with docker-machine

It appeared that docker-machine mounted my local /home to /hosthome by default.

Also using -v or --volume option for docker run has a difference to --mount option: local folder is automatically created if it doesn’t exist.

That’s why when calling docker run -v /home/a/b/c:/xxx, an empty folder was created at path /home/a/b/c inside virtual machine first, and then mounted to container at path /xxx.

First takeaway was to stop using -v option and start using --mount instead: docker run --mount type=bind,source=/home/a/b/c,target=/xxx. In this case the command will fail fast with error as the source path doesn’t exist in virtual machine.

Second step was to remove default shared folder from the virtual machine (my driver was virtualbox) and replace it with proper one with matching path. Performing these operations on stopped vm:

docker-machine stop
vboxmanage sharedfolder remove default --name hosthome
vboxmanage sharedfolder add default --name $HOME --hostpath $HOME --automount
docker-machine start