docker run
has an option to let you mount a host directory or file into a container with --mount=type=bind
. This is fine and dandy but what if you need to exclude a subdirectory from you mounted directory or file? Well, there is no built-in option to do exactly that with docker run
, but you can override a subdirectory using another --mount=type=bind
:
$ mkdir -p thedirectory/thesubdirectory
$ touch thedirectory/thefile.txt
$ touch thedirectory/thesubdirectory/theotherfile.txt
$ docker run \
--mount="type=bind,src=$PWD,dst=/home/ubuntu" \
--mount="type=volume,dst=/home/ubuntu/thedirectory/thesubdirectory" \
ubuntu \
ls -la /home/ubuntu/thedirectory/thesubdirectory
total 8
drwxr-xr-x 2 root root 4096 Nov 5 09:45 .
drwxr-xr-x 3 root root 4096 Nov 5 09:44 ..
As you can see, the subdirectory content was not mounted into the container. Another way to do this is to create or use an empty directory:
$ mkdir -p thedirectory/thesubdirectory
$ touch thedirectory/thefile.txt
$ touch thedirectory/thesubdirectory/theotherfile.txt
$ mdkir /private/tmp/empty
$ docker run \
--mount="type=bind,src=$PWD,dst=/home/ubuntu" \
--mount="type=bind,src=/private/tmp/empty,dst=/home/ubuntu/thedirectory/thesubdirectory" \
ubuntu \
ls -la /home/ubuntu/thedirectory/thesubdirectory
total 8
drwxr-xr-x 2 root root 4096 Nov 5 09:45 .
drwxr-xr-x 3 root root 4096 Nov 5 09:44 ..