Understand Docker Bind Mounts using your Windows machine and ASP.NET Core Web API project.
1. What is a Bind Mount?
A Bind Mount allows us to connect a folder or file from our host machine (Windows) to a folder or file inside a Docker container.
In simple words:
You have a folder on your Windows computer. You want your Docker container to access that same folder and its files.
Instead of copying files into the container, Docker connects the host folder directly to the container.
Real-world example
Suppose you have an ASP.NET Core project:
D:\Docker Practice\DockerDemo\DockerDemo.API
Inside this project, you have:
DockerDemo.API
|
+---- Controllers
+---- Models
+---- Program.cs
+---- appsettings.json
You want your Docker container to access these files.
You can use a Bind Mount.
Windows Host
|
|
+---- D:\Docker Practice\MyApp
|
| Bind Mount
|
V
Docker Container
|
+---- /app
|
+---- Program.cs
+---- appsettings.json
Both locations refer to the same underlying files.
2. Why do we need Bind Mounts?
Consider these scenarios:
|
Scenario |
Why Bind Mount? |
|---|---|
|
Development |
Edit source code on Windows and access it inside the container |
|
Configuration |
Share appsettings.json with a container |
|
Logging |
Store application logs directly on Windows |
|
File uploads |
Save uploaded files to a host directory |
|
Testing |
Share test files between host and container |
Most common usage: During development, developers use Bind Mounts to share source code between their computers and Docker containers.
3. Practical Example – Step by Step
Let's perform a simple experiment.
We will:
-
Create a folder on Windows.
-
Create a file inside that folder.
-
Mount the folder into a Docker container.
-
Read the file inside the container.
-
Modify the file inside the container.
-
Verify the modification on Windows.
Step 1: Create a folder on Windows
Open CMD:
mkdir D:\DockerPractice\BindMountDemo
Create a file:
echo Hello from Windows > D:\DockerPractice\BindMountDemo\message.txt
Your folder structure:
D:\DockerPractice\BindMountDemo
|
+---- message.txt
File content:
Hello from Windows
Step 2: Create a Docker container with Bind Mount
Execute:
docker run -it --name bind-demo -v D:\DockerPractice\BindMountDemo:/app/data ubuntu bash
Let's understand the command:
|
Command |
Explanation |
|---|---|
|
|
Create and start a container |
|
|
Interactive terminal |
|
|
Container name |
|
|
Specify storage mount |
|
|
Windows host folder |
|
|
Container folder |
|
|
Docker image |
|
|
Open Linux terminal |
Note: Docker Desktop must be running.
Step 3: Read the Windows file inside Docker
You are now inside the Ubuntu container.
Execute:
cat /app/data/message.txt
Output:
Hello from Windows
How is this possible?
Because Docker connected your Windows folder to /app/data inside the container.
Step 4: Modify the file inside Docker
Execute this inside the container:
echo "Hello from Docker Container" >> /app/data/message.txt
Read the file again:
cat /app/data/message.txt
Output:
Hello from Windows
Hello from Docker Container
Step 5: Verify the file on Windows
Open another Windows CMD window.
Execute:
type D:\DockerPractice\BindMountDemo\message.txt
Output:
Hello from Windows
Hello from Docker Container
You modified the file inside the Docker container, but the actual Windows file was also modified.
Why? Both locations point to the same underlying host directory.
4. What happens when we delete the container?
Let's test persistence.
Exit the container:
exit
Delete the container:
docker rm bind-demo
Now check your Windows folder:
type D:\DockerPractice\BindMountDemo\message.txt
Output:
Hello from Windows
Hello from Docker Container
The file still exists!
Reason: The file belongs to your Windows folder, not to the container's writable layer.
Even after deleting the container, the host directory remains unchanged.
5. Important: Bind Mount vs Docker Volume
This is where many developers get confused.
Both provide persistent storage, but their storage management is different.
-
Docker manages the storage location.
-
You don't need to create a specific host folder.
-
Suitable for database storage.
docker run -v mydata:/app/data ubuntu
-
You specify the host folder.
-
You can directly access files from Windows.
-
Suitable for development and file sharing.
docker run -v D:\MyApp:/app ubuntu
|
Feature |
Docker Volume |
Bind Mount |
|---|---|---|
|
Managed by |
Docker |
User / Host OS |
|
Host folder |
Docker-managed |
User specifies |
|
Data persistence |
Yes |
Yes |
|
Access files from Windows |
Not directly convenient |
Yes |
|
Development usage |
Less common |
Very common |
|
Database storage |
Recommended |
Possible |
6. Read-only Bind Mount
Sometimes, you want a container to read files but should not be allowed to modify them.
For this, use readonly or ro.
Example:
docker run -it --name bind-readonly -v D:\DockerPractice\BindMountDemo:/app/data:ro ubuntu bash
Now execute:
cat /app/data/message.txt
You can read the file.
But try modifying it:
echo "New message" >> /app/data/message.txt
You will get a permission error because the mount is read-only.
Use case: Share configuration files with a container without allowing the application to modify them.
7. Modern Syntax – Using --mount
Docker supports two syntaxes for mounting directories.
Using -v:
docker run -it -v D:\DockerPractice\BindMountDemo:/app/data ubuntu bash
Using --mount:
docker run -it --mount "type=bind,source=D:\DockerPractice\BindMountDemo,target=/app/data" ubuntu bash
Both achieve the same result.
--mount is more explicit and easier to understand when working with multiple mount options.
8. ASP.NET Core Practical Use Case
Consider your ASP.NET Core Web API project.
You have an application running inside Docker:
Windows
|
+---- D:\DockerPractice\MyApi\Logs
|
+---- Docker Container
|
+---- /app
|
+---- /app/Logs
You can map the Logs folder:
docker run -d --name myapi -p 8080:8080 -v D:\DockerPractice\MyApi\Logs:/app/Logs myapi:v1
Now, whenever your ASP.NET Core application writes logs to /app/Logs, the files will be available in:
D:\DockerPractice\MyApi\Logs
This is useful when you want to inspect application logs directly from Windows.
9. Important Interview Questions
|
Question |
Key Answer |
|---|---|
|
What is a Bind Mount? |
Connects a host file or directory to a container path. |
|
Why do we use Bind Mounts? |
To share files between host and container. |
|
Does Bind Mount data survive container deletion? |
Yes, because data resides on the host. |
|
Can multiple containers share a Bind Mount? |
Yes. |
|
Can a Bind Mount be read-only? |
Yes, using |
|
What is the difference between Volume and Bind Mount? |
Docker manages volumes; users specify bind mount paths. |
|
What happens if the host folder does not exist? |
|
10. Test Your Understanding
Let's check whether you understood Bind Mounts.
1. You modify a file inside a container using a Bind Mount. Where will the modification appear?
Answer C: Both locations refer to the same underlying host files.
2. You delete a container that uses a Bind Mount. What happens to the host files?
Answer B: Deleting the container does not delete the host directory or its files.
3. Which option makes a Bind Mount read-only?
Answer C: The :ro option makes the mounted directory read-only inside the container.