How to attach a volume to a Docker container running Ubuntu on Mac?

  docker, ubuntu

I am running a docker container with Ubuntu Xenial image on my macOS Big Sur using the command sudo docker run -it ubuntu:xenial and it works fine, giving me a Linux terminal.

However, I want to attach a volume to my container so that I can save the work that I do within the ubuntu terminal that I get from within the container on my Mac’s filesystem. For that, I tried to run the following command:

sudo docker run -it ubuntu:xenial -v /Users/myname/Documents/UbuntuWorkspace

(I had previously created the directory named UbuntuWorkspace)

But I encountered the following error when I tried to run the above command:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-v": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled 

So, what is the right way to attach a directory on my Mac’s filesystem to the Ubuntu docker container that I run, so that I can save my work and also view it on the finder app?

Source: Docker Questions