# Bind Mount persistence in Docker Compose

In 
Published 2022-12-03

This tutorial explains how we can use a bind mount with Docker Compose.

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 Compose 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. This container will be started using Docker Compose.

for this, we create a file docker-compose.yaml in D:\dcompose\http directory which contains:

version: '4.15'
services:
  apache:
    image: httpd:latest
    container_name: my-new-container
    ports:
    - '8080:80'
    volumes:
      - type: bind
        source: /d/docker/htdocs
        target: /usr/local/apache2/htdocs

In order to start the container, run docker compose up.

When you put http://localhost:8080 into the browser you will get the index.html defined above.