🎓 A1Learner
🔍
0 of 50 lessons completed

Bind Mounts

A bind mount mounts a host folder into a container. Editing a file on the host shows up inside the container at once, which makes bind mounts the tool for live development.

Mount a folder

This command mounts the host folder into nginx at the web root.

docker run -d --name g5-web -p 8101:80 -v ~/site:/usr/share/nginx/html:ro nginx:1.27-alpine

Edit on the host

Change the file on the host, and the container serves the new text.

echo "<h1>Hello from a bind mount</h1>" > ~/site/index.html
curl -s http://localhost:8101/

Example

mkdir -p ~/site
echo "<h1>Hello from a bind mount</h1>" > ~/site/index.html
docker run -d --name g5-web -p 8101:80 -v ~/site:/usr/share/nginx/html:ro nginx:1.27-alpine >/dev/null
curl -s http://localhost:8101/

Key points

  • Bind mount: a host folder mounted into a container at a path.
  • Mount a folder with -v /host/path:/container/path.
  • Edit a file on the host, and the container sees the new text at once.
  • Use bind mounts for code you edit while the app runs.
  • Use volumes for data the app owns, like a database.

In this example, a host folder is mounted into nginx at its web root. The file is written on the host, and curl reads it through the container. The heading proves the container serves the host file live.

Note

A bind mount shows host files inside the container, so a wrong path can hide the container's own files.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What is a bind mount best used for?