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.


Recommended Articles

Second Tutorial: Deploy Applications with Docker Files for WildFly

Learn how to deploy applications using Dockerfiles in this second tutorial on running WildFly with Docker. #Docker #WildFly #Java #CloudNative

Effortlessly Manage PostgreSQL Database with Docker Containers - A Comprehensive Guide

Learn how to quickly start and manage a PostgreSQL database using Docker containers in this comprehensive guide.

Master Docker Compose for Efficient Enterprise Java Middleware Management

Learn how to use Docker compose to manage multiple containers with ease. #DockerCompose #JavaMiddleware #CloudNative

Master Docker Volumes with Ease: A Comprehensive Tutorial

Discover how to create, manage and remove Docker volumes for persistent data storage in containers. Learn essential commands and best practices.