Docker: How to access files on the host from within a container

Docker exposes the host directory or file mounting facility through the -v option of the docker run command. Let’s see how to use it in this tutorial.

Docker uses the -v option in three different formats:
1. -v <container mount path>
2. -v <host path>/<container mount path>
3. -v <host path>/<container mount path>:<read write mode>

The following example demonstrates how to make available to the container the folder “/home/francesco” contained on the host machine.

docker run -it -v /home/francesco/:/home/:rw fedora /bin/bash

If /home/francesco is not found on the Docker host, the Docker engine will create the directory itself. However, the system-generated directory cannot be deleted using the -v option of the docker rm subcommand.

The :ro modifier specifies that a container mounts a file or file system read-only. To mount a file or file system read-writable, specify the :rw modifier instead or omit the modifier altogether.

You can check that any changes made to the host shared directory, will be visible to the container.