#
Bind Mount persistence in Docker Compose
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 🌹 !</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.
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 could use either the full path or the relative path location (
source: ./htdocs
in my case).