A Guide to Docker Bind Mounts vs Volumes
Bind Mounts:
In Docker, bind mounts are a method for sharing files or directories between the host machine and a Docker container. This allows changes to the files or directories on the host to be reflected inside the container and vice versa.
Definition: A bind mount allows you to mount a specific file or directory from the host into a container. Unlike Docker volumes, which are managed by Docker and stored in a specific location on the host, bind mounts map directly to a specific path on the host filesystem.
Use Case: Bind mounts are useful for scenarios where you need to share files between the host and the container, such as when developing code, working with configuration files, or storing logs.
How to Use Bind Mounts:
docker run -v /host/path:/container/path image
Key Points to Remember:
File and Directory Synchronization: Changes made to files in the bind-mounted directory on the host are immediately reflected in the container, and changes made inside the container are reflected back to the host.
Permissions: The container processes have the same permissions on the mounted files as the host processes. Ensure that the permissions are set appropriately to avoid access issues.
Absolute Paths: Bind mounts require absolute paths on the host. Relative paths are not supported.
Security Considerations: Be cautious with bind mounts, as they expose parts of the host filesystem to the container. Ensure that only necessary directories are mounted and that appropriate security measures are in place.
Docker Volumes:
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:
Volumes are easier to back up or migrate than bind mounts.
You can manage volumes using Docker CLI commands or the Docker API.
Volumes work on both Linux and Windows containers.
Volumes can be more safely shared among multiple containers.
Volume drivers let you store volumes on remote hosts or cloud providers, encrypt the contents of volumes, or add other functionality.
New volumes can have their content pre-populated by a container.
Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.
In addition, volumes are often a better choice than persisting data in a container's writable layer, because a volume doesn't increase the size of the containers using it, and the volume's contents exist outside the lifecycle of a given container.
Volumes can be created and managed using the docker volume command. You can create a new volume using the following command:
docker volume create <volume_name>
e.g
docker volume create v1
to check details of vloume:
docker volume inspect v1
Once a volume is created, you can mount it to a container using the -v or --mount option when running a docker run command.
For example:
docker run -it -v <volume_name>:/data <image_name> /bin/bash
This command will mount the volume <volume_name> to the /data directory in the container. Any data written to the /data directory inside the container will be persisted in the volume on the host file system.
e.g
docker run -d --mount source=v1,target=/app nginx:latest
To check the container details and find a mounted volumes then use below command:
docker inspect <container ID >
docker inspect 8f751b39f54d
Note: If you want to delete a volume then first stop a container and then delete it otherwise you will face an issue that volume is in use.
Subscribe to my newsletter
Read articles from ashwini purwat directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by