# Bind Mount persistence in Docker Containers

In 
Published 2022-12-03

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&nbsp; &#127801; !</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.