#
Bind Mount persistence in Docker Containers
This tutorial explains how we can use a bind mount with Docker containers.
Bind mounts have been around since the early days of Docker.
Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.
Prerequisites:
- We are using Docker on Windows
- We have created D:\docker\htdocs folder which contains index.html file.
index.html file has the following content:
<html>
<head>
<title>This is a page from my container</title>
</head>
<body>
<p>Hello World !</p>
<p>This message is from my server 🌹 !</p>
</body>
</html>
Now we need to run a container which runs a http server which serves index.html file.
for this,
Start a container with a volume:
docker run -d --name my-container -p 8080:80 -v /d/docker/htdocs:/usr/local/apache2/htdocs httpd:latest
or
docker run -d --name my-container -p 8080:80 --mount type=bind,source=/d/docker/htdocs,target=/usr/local/apache2/htdocs httpd:latest
When you put http://localhost:8080
into the browser you will get the index.html defined above.
Info
- As you can see the initial content of
/usr/local/apache2/htdocs
directory is replaced by the content from the source directory. (The one from Windows in my case). - Also,
D:\docker\htdocs
from Windows becomes/d/docker/htdocs
in Docker. - The source must use always the full path location.