A Bind Mount in Docker is a storage mechanism that allows mounting a file or directory from the host filesystem, establishing a bidirectional and real-time synchronization bridge.
In the previous article we saw Volumes, which are great for storing databases because Docker manages them in a hidden safe. But, for example in development, they aren’t entirely practical.
Let’s look at the problem. Imagine you’re creating a website. You have your index.html on your Desktop. If you use what we’ve learned so far, to see your changes you would have to:
- Edit the
index.html. - Build a new image (
docker build). - Delete the old container.
- Launch the new one.
Having to do all this just to see how a button’s color changes… isn’t very practical. We need a way to tell the system: “Don’t use the files packaged in the image; read and directly execute what’s in this folder on my computer.”
Today we’re going to learn how to use Bind Mounts to inject our source code into running containers 👇
What is a Bind Mount?
Unlike a Volume (which lives in “Docker’s world”), a Bind Mount is a direct link to any folder on your host computer.
You map C:\MyProjects\Web (Host) ➡️ /var/www/html (Container).
- If you create a file on Windows, it magically appears in Linux inside the container.
- If the container deletes a file, it gets deleted from your Windows (be careful with this!).
Bind Mounts vs Volumes
| Feature | Volume | Bind Mount |
|---|---|---|
| Host Location | Hidden (/var/lib/docker/…) | Wherever you want (Desktop, Documents…) |
| Management | Docker CLI (docker volume …) | You (File Explorer, Git…) |
| Security | Isolated | Lower (The container can touch your personal files) |
| Performance | Native | Depends (On Windows/WSL it can be slower crossing filesystems) |
| Primary use | Databases, Backups | Source code, Development configuration |
The syntax of the -v flag
We use the same -v flag as with volumes, but the difference is that the left side is now an absolute path.
docker run -v /absolute/path/on/my/pc:/path/in/container ...
- Volume:
-v my-data:/data(Doesn’t start with/orC:, it’s a name). - Bind Mount:
-v C:\Users\Luis\Web:/data(It’s a physical path).
Example: “Hot Reload” with Nginx
Let’s test it out. You’ll see how we turn a static web server into a dynamic development environment.
Prepare the terrain
Create a new folder on your computer called docker-test. Inside it, create an index.html file with this content:
<h1>Hello from my PC</h1>
Launch the container with a Bind Mount
Open the terminal inside that folder and run:
docker run -d -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx
(We are replacing Nginx’s default folder with OUR folder).
Verify
Open your browser at http://localhost:8080. You’ll see “Hello from my PC”.
Live edit
Without stopping the container, open the index.html in your favorite text editor (Notepad, VS Code…). Change the text to:
<h1>Hello from my PC... Edited live!</h1>
Save the file.
Refresh the browser
Go back to Chrome, press F5. The change appears instantly! 🎉
You’ve just set up a development environment. The Nginx server is reading your file directly from the hard drive. You didn’t have to rebuild anything.
