Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ravinduchathuranga/Docker-Volume-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DOCKER VOLUMES

Docker volumes are a powerful feature that allows you to persist data generated by and used by Docker containers. They provide a way to store data outside of the container's filesystem, ensuring that the data remains intact even if the container is removed or recreated.

There are two main types of Docker volumes:

  1. Named Volumes: These are managed by Docker and can be created using the docker volume create command. Named volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). They are easy to use and are the preferred way to persist data in Docker.

How to manage named volumes:

  • Create a volume:
     docker volume create my_volume
  • List volumes:
     docker volume ls   
  • Inspect a volume:
     docker volume inspect my_volume
  • Remove a volume:
     docker volume rm my_volume
  • Create a volume alongside with docker run:
    docker run -d -v my_volume:/path/in/container my_image
  • Example of running a container with a named volume:
    docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback feedback-node:volume
  1. Bind Mounts: These allow you to mount a file or directory from the host filesystem into a container. Bind mounts can be useful for development purposes, as they allow you to directly access and modify files on the host system from within the container.
  • There are some considerations when using bind mounts:

    • The file or directory on the host must exist before you can mount it into a container.
    • Changes made to the files in the bind mount are reflected on both the host and the container.
    • Bind mounts can pose security risks if sensitive files from the host are exposed to the container.
  • How to add your host directory as a bind mount in Docker resource settings (Docker Desktop):

    1. Open Docker Desktop and go to Settings.
    2. Navigate to the Resources section and select File Sharing.
    3. Add the directory you want to use as a bind mount by clicking on the "+" button and selecting the folder.
    4. Apply and restart Docker Desktop if prompted.

Do we need Virtual file shares update to manage bind volumes ?

No — bind mounts do not depend on Docker Desktop or Virtual File Shares.

Here’s the clear breakdown:


Bind mounts work natively on Linux

On Linux, Docker runs directly on the host OS. This means:

  • There is no VM
  • There is no “file sharing” layer
  • There is no requirement for Virtual File Shares

You can bind-mount any host directory directly:

docker run -v /host/path:/container/path IMAGE

This works immediately as long as permissions allow it.


🖥️ Bind mounts require "file sharing" only on Docker Desktop (Mac/Windows)

On Windows and macOS, Docker runs inside a Linux virtual machine. The VM cannot automatically access your host filesystem, so Docker Desktop requires:

✔ File Sharing (macOS)

✔ Resources → File Sharing (Windows)

These settings create Virtual File Shares between your host OS and the internal Linux VM.

This is why bind mounts on Docker Desktop require configuration:

docker run -v /Users/myfolder:/data myimage

Docker Desktop needs to “share” /Users/myfolder into the Linux VM first.


🧠 Key concept

Platform Bind mount works natively? Requires Virtual File Share config?
Linux ✅ Yes ❌ No
Docker Desktop on macOS ⚠ Works only after File Sharing config ✅ Yes
Docker Desktop on Windows ⚠ Works only after File Sharing config ✅ Yes

📌 So the answer:

👉 Bind mounts only require Virtual File Shares on Docker Desktop (Windows/macOS).

👉 On Linux, bind mounts work natively and do not use Virtual File Shares at all.


If you want, I can create a clean explanation for your GitHub README breaking this down clearly for users on Linux, macOS, and Windows.

How to manage bind mounts:

  • Create a bind mount alongside with docker run:
    docker run -d -v /path/on/host:/path/in/container my_image
  • Example of running a container with a bind mount:
    docker run -d -p 3000:80 --rm --name feedback-app -v /home/user/feedback-data:/app/feedback feedback-node:volume
  • Extra point here

    • Ensure that the host directory (/home/user/feedback-data in the example) exists before running the container.
    • Covering up with double quotes in case there are spaces in the path:
    docker run -d -p 3000:80 --rm --name feedback-app -v "/home/user/feedback-data:/app/feedback" feedback-node:volume
  • Combinding and merging different types of volumes:

    • You can combine named volumes and bind mounts in a single docker run command. For example, to use a named volume for feedback data and a bind mount for configuration files, you can run:
        docker run -d --rm -p 3000:80 --name feedback-app -v feedback:/app/feedback -v "/Projects/Docker/academic/data-volumes-01-starting-setup:/app" -v /app/node_modules feedback-node:volume
    • This command mounts the named volume feedback to /app/feedback in the container and the host directory /home/user/config to /app/config in the container.
    • This flexibility allows you to choose the best storage solution for different types of data within the same container.
    • Or you and mention the Dockerfile way of creating multiple volumes as well.
        ## Create a volume to persist node_modules
        VOLUME ["/app/node_modules"]

Cleaning up unused volumes

Over time, you may accumulate unused volumes that are no longer needed. To clean up these unused volumes, you can use the following command:

docker volume prune

This command will remove all unused volumes, freeing up space on your system.

Note: Be cautious when using this command, as it will permanently delete the volumes and their data.

About

A collection of examples and guides demonstrating how to create, manage, and use Docker volumes for persistent storage in containerized applications

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors