docker-bind-mounts

Bind Mounts in Docker

  • 4 min

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:

  1. Edit the index.html.
  2. Build a new image (docker build).
  3. Delete the old container.
  4. 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

FeatureVolumeBind Mount
Host LocationHidden (/var/lib/docker/…)Wherever you want (Desktop, Documents…)
ManagementDocker CLI (docker volume …)You (File Explorer, Git…)
SecurityIsolatedLower (The container can touch your personal files)
PerformanceNativeDepends (On Windows/WSL it can be slower crossing filesystems)
Primary useDatabases, BackupsSource 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 ...
Copied!
  • Volume: -v my-data:/data (Doesn’t start with / or C:, 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>
Copied!

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
Copied!

(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>
Copied!

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.