How do Docker bind mounts work?
In a nutshell
Suppose you had the following bind mount in a docker-compose.yml
file:
What does the ./:/var/www/html
entry actually mean?
Assuming your current directory (where the docker-compose.yml
file is located) is /Users/sajad/sites/wordpress/sajadtorkamani
, you’re telling Docker to “bind” the /Users/sajad/sites/wordpress/sajadtorkamani
on your host/local machine to the /var/www/html
directory inside the php
container.
Now, any changes made to the files inside the /Users/sajad/sites/wordpress/sajadtorkamani
directory on the host machine is immediately reflected on the container file system at /var/www/html
, and vice-versa.
So if you were to add a new file with touch hello.txt
at /Users/sajad/sites/wordpress/sajadtorkamani/hello.txt
, the hello.txt
file would also get created at /var/www/html/hello.txt
.
This ensures that the host filesystem at /Users/sajad/sites/wordpress/sajadtorkamani
and container filesystem at /var/www/html
for are always in sync.
Example use cases
Development
If you make changes to the source code in your host filesystem, you want it to sync with the container filesystem automatically without needing to rebuild the container.
Configuration
If you’re using an Nginx container, you want your changes at a docker/nginx.conf
file to be synced with a /usr/local/etc/nginc.conf
file inside the Nginx container.
Data access
You want changes made by a MySQL container at /var/lib/mysql
to be bound to an external named volume so that the data is persisted even if the MySQL container is stopped.
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment