Mount a host directory in a Docker container
Use this simple code on the command line to mount multiple host directories within a Docker container.
Once a Docker container has been created and run, it may need to access local (host) directories. These directories are not automatically available, so they need to be mapped. There are several ways to do this, the simplest being via the -v
or --volume
flag.
As an example, let’s say we are running the adamdimech/plantcv:4.5.1 container, and want to map /home/amdimech/stuff
to the Docker container. The syntax is in the form host_directory:container_directory, so the correct code would be:
docker run -it -v /home/amdimech/stuff/:/home/amdimech/stuff/ adamdimech/plantcv:4.5.1 /bin/bash
Here’s the result, when ls -lh
is entered into the console:
root@e42fe9eb17a3:/# ls -lh
total 52K
lrwxrwxrwx 1 root root 7 Jun 30 00:00 bin -> usr/bin
drwxr-xr-x 2 root root 4.0K May 9 14:50 boot
drwxr-xr-x 5 root root 360 Jul 3 01:18 dev
drwxr-xr-x 1 root root 4.0K Jul 3 01:18 etc
drwxr-xr-x 1 root root 4.0K Jul 3 01:18 home
lrwxrwxrwx 1 root root 7 Jun 30 00:00 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Jun 30 00:00 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 media
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 mnt
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 opt
dr-xr-xr-x 445 root root 0 Jul 3 01:18 proc
drwx------ 1 root root 4.0K Jul 2 23:46 root
drwxr-xr-x 1 root root 4.0K Jul 1 03:19 run
lrwxrwxrwx 1 root root 8 Jun 30 00:00 sbin -> usr/sbin
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 srv
dr-xr-xr-x 13 root root 0 Jul 3 01:18 sys
drwxr-xr-x 1 root root 4.0K Jun 30 00:00 usr
drwxr-xr-x 1 root root 4.0K Jun 30 00:00 var
Notice how the /home/ directory is mapped exactly. If you want to shorten the path within the container, you can do this by shorting the container directory:
docker run -it -v /home/amdimech/stuff/:/stuff/ adamdimech/plantcv:4.5.1 /bin/bash
Now the result is changed:
root@a87ff90a9189:/# ls -lh
total 56K
lrwxrwxrwx 1 root root 7 Jun 30 00:00 bin -> usr/bin
drwxr-xr-x 2 root root 4.0K May 9 14:50 boot
drwxr-xr-x 5 root root 360 Jul 3 01:21 dev
drwxr-xr-x 1 root root 4.0K Jul 3 01:21 etc
drwxr-xr-x 2 root root 4.0K May 9 14:50 home
lrwxrwxrwx 1 root root 7 Jun 30 00:00 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Jun 30 00:00 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 media
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 mnt
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 opt
dr-xr-xr-x 454 root root 0 Jul 3 01:21 proc
drwx------ 1 root root 4.0K Jul 2 23:46 root
drwxr-xr-x 1 root root 4.0K Jul 1 03:19 run
lrwxrwxrwx 1 root root 8 Jun 30 00:00 sbin -> usr/sbin
drwxr-xr-x 2 root root 4.0K Jun 30 00:00 srv
drwxr-xr-x 11 1000 1000 4.0K May 5 04:48 stuff
dr-xr-xr-x 13 root root 0 Jul 3 01:21 sys
drwxr-xr-x 1 root root 4.0K Jun 30 00:00 usr
drwxr-xr-x 1 root root 4.0K Jun 30 00:00 var
If you need to mount multiple volumes, you can do so by repeating use of the -v
flag:
docker run -it -v /home/amdimech/stuff/:/stuff/ -v /mnt/d/Downloads/:/downloads/ adamdimech/plantcv:4.5.1 /bin/bash
You can also use the --mount
flag, which is more verbose but more versatile:
docker run -it --mount src=/home/amdimech/stuff/,target=/stuff/,type=bind adamdimech/plantcv:4.5.1 /bin/bash
The type
value can either be bind
, volume
or tmpfs
. Further information is available on DockerDocs.
Comments
No comments have yet been submitted. Be the first!