Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can be modified at any time. You can create a file on the host system and attach it to the container where you want to maintain the state of the Docker. You can also use the bind mount for stateful applications.
Use of Bind Mounts:
Bind mounts are appropriate for the following types of use case:
- Sharing source code or build artifacts between a development environment on the Docker host and a container.
- When you want to create or generate files in a container and persist the files onto the host's filesystem.
- Sharing configuration files from the host machine to containers. This is how Docker provides DNS resolution to containers by default, by mounting /etc/resolv.conf from the host machine into each container.
Bind mounts are also available for builds: you can bind mount source code from the host into the build container to test, lint, or compile a project.
What is Docker Volume?
Applications can be run independently using Docker containers. By default, when a container is deleted or recreated, all of the modifications (data) within it are lost. Docker volumes can be useful if we wish to save data in between runs. In order to protect data created by the running container, Docker volumes are file systems mounted on Docker containers.
Implimentation volume in container:
First step is to create the docker volume using following command:
after creating the volume, now we can dedicate this volume to one or multiple containers
to get the details information about volume, we can below command:
If you want to delete the volume which you created then run:
Docker volume rm abhishek
now I want to mount the volume on a container. so for that, Create the docker image to creating the docker image, we have to build docker file.
so first build docker file:
my docker file is in same directory so I use dot instead of filename, but If your docker file is in different directory then use filename instead of dot.
Now I mount volume in container
after mounting the volume, we have to check the running the containers using
docker ps
32a9d862a19a
after that check the container information, is volume is proporly mount on container or not, to check the use
docker inspect 32a9d862a19a
It will show the volume details in mount section using below.
*Note:To delete the volume we have to stop the running container then delete the container then we can delete the volume *
📝 Vlog Summary:
In this vlog, I’ll walk you through the concept of Docker Volumes — what they are, why they matter, and how to use them to persist data in Docker containers. We’ll start by creating a Docker volume, learn how to mount it to a container, and inspect the container to verify the mount. I’ll also demonstrate how to build a Docker image and run a container with volume mapping. Finally, we’ll cover how to properly clean up volumes and containers. If you’re new to Docker or want to manage data persistency better, this one’s for you!
Top comments (0)