Docker Volumes and Bind Mounts

Rajesh GurajalaRajesh Gurajala
5 min read

🧩 I. PROBLEMS BEFORE DOCKER VOLUMES & BIND MOUNTS

Before volumes and bind mounts were introduced, people faced several critical limitations when working with Docker containers.

❌ 1. Data Loss on Container Deletion

  • Containers are ephemeral.

  • Any file written inside the container's filesystem was destroyed when the container was removed.

  • For example, an Nginx server writing logs, or a MySQL container storing data — all gone once docker rm is executed.

❌ 2. No Host-Container File Sharing

  • There was no way to access or modify files inside a running container from the host.

  • Debugging or live-editing a config file or codebase was not possible without committing a new image or rebuilding

🔧 The core issue: Containers had isolated, temporary filesystems that couldn’t interact with the host or persist data reliably.

📁 II. WHAT ARE BIND MOUNTS?

✅ Concept:

Bind mounts allow you to directly map a host directory or file into a container’s filesystem.

📌 Technical Details:

  • You specify the full path on the host.

  • Used heavily in development environments for real-time code or config updates.

  • Changes made on the host are instantly reflected inside the container and vice versa.

🧪 Example:

docker run -v /home/user/code:/app nginx

This maps the host folder /home/user/code into the container at /app.

✅ You can now live-edit /home/user/code/index.html, and Nginx in the container will serve the updated file immediately.

📦 III. WHAT ARE DOCKER VOLUMES?

✅ Concept:

Docker Volumes are managed storage areas outside the container's filesystem but inside the Docker environment. They’re used to persist and share data between containers and between container and host — without being tied to the container lifecycle.

📌 Technical Details:

  • Created and managed by Docker (docker volume create).

  • Stored under:

        /var/lib/docker/volumes/
    
  • Decoupled from container lifecycle — deleting the container doesn't remove the volume.

  • Can be backed up, restored, and shared between multiple containers.

🧪 Example:

docker volume create mydata

docker run -v mydata:/data ubuntu

This mounts the named volume mydata to the container's /data directory.

✅ Even if the container is deleted, data in mydata remains safe.


✅ Why should you use Docker volumes over bind mounts — especially in production?

Reason

Why Volumes Are Better Than Bind Mounts

🧹 Managed by Docker

Clean, consistent, portable storage managed internally

🔒 Better Security

Docker controls access; less risky than exposing host paths

⚙️ More Portable

Volumes work across OSes & hosts; bind mounts depend on host

🧠 Abstracted Storage

Docker can use local or remote drivers (e.g., NFS, S3, etc.)

📈 Better Performance

Optimized, especially on Linux/overlayfs

📦 Backup/Restore Tools

Native volume snapshot/backup support

🔁 Reusability

Volumes can be shared across multiple containers cleanly


Playing around with Bind Mounts

  1. Installing docker:
sudo apt update
sudo apt install docker.io -y
sudo usermod -aG docker ubuntu

i a) ubuntu in 3rd command is username of host machine on which u install docker. To exactly determine the username of ur host, run whoami command.

i b) Restart the host machine to apply changes.

  1. Now make a folder bind_mounts and navigate into it.

  2. Now write a sample index.html and run chmod -R a+rX $(pwd) command.

  3. Now run a httpd:2.4 docker container with

docker run -d --name my_site -p 8000:80 --mount type=bind,source=$(pwd),target=/usr/local/apache2/htdocs httpd:2.4
  1. Now in ur terminal visit port 8000 by curl localhost:8000 . U can see ur index.html code.

  2. If u edit index.html file one more time and visit curl localhost:8000 u can see the changes reflected.

  3. Now even though ur container is deleted, the file still remains in the host directory.


Playing around with Docker volumes:

  1. Some important commands :

docker volume create rajesh        # To create a volume with name as rajesh

docker volume inspect rajesh        # To know info about rajesh volume

docker volume rm rajesh suresh mahesh  # To delete multiple volumes 

docker volume ls                         # To list volumes

docker run -d --mount source=rajesh,target=/app nginix:latest 

# To attach the volume " rajesh " which stores data of /app of nginix container

# Remember if u want to delete a volume, first stop running containers associated with it, 
# and only then u can delete the volume.
  1. Let’s try:

      docker run --name=db -e POSTGRES_PASSWORD=abcd -d -v raj:/var/lib/postgresql/data postgres
    
       # This will start the database in the background, configure it with a password, 
      #   and attach a volume or creates new volume raj if not present.  
    
      docker exec -ti db psql -U postgres  # login into postgres cli of db container
    
      CREATE TABLE tasks (
          id SERIAL PRIMARY KEY,
          description VARCHAR(100)
      );
      INSERT INTO tasks (description) VALUES ('Finish work'), ('Have fun');
    
      # This will create a "tasks" table and inserts 2 records into it
    
      select * from tasks;  # u can list those 2 entries of tasks table with this command
    
      docker stop db
      docker rm db     # stop and delete db container
    
      docker run --name=new-db -e  -d -v raj:/var/lib/postgresql/data postgres
    
      # a new container new-db will the attached the raj volume, but notice
      # env variable not used because that variable is only used when bootstrapping a new db.
    
      select * from tasks; # now it will list same entries as old container db.
    
1
Subscribe to my newsletter

Read articles from Rajesh Gurajala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajesh Gurajala
Rajesh Gurajala