Docker Bind Mount
What are two differences between a docker volume and a bind mount?
- Docker Volumes: Stored outside of the container filesystem, Docker volumes are controlled by Docker. Docker handles managing and maintaining these volumes; they have a specific placement on the host machine. Volumes are perfect for distributing data across containers and for storing data between container restarts.
- Bind Mounts: In contrast, bind mounts establish a direct connection between a particular file or directory on the host computer and a directory within the container. This indicates that Docker does not directly manage the data; instead, it is stored on the host disc. Although they may need more manual maintenance, bind mounts offer a simple mechanism for the host and the container to communicate files and directories.
Can I mount a file in Docker?
Yes, you can mount a file into a Docker container using both Docker volumes and bind mounts.
docker run -v /path/on/host:/path/in/container -it your_image touch /path/in/container/your_file.txt
How To Use Bind Mount In Docker?
Bind mounts and the host filesystem are tightly related. The host filesystem is immediately impacted by changes made to a bind mount, and vice versa. When you use a bind mount to remove a container, the related data stays on the host.