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:
- Named Volumes: These are managed by Docker and can be created using the
docker volume createcommand. 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.
- 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- 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):
- Open Docker Desktop and go to Settings.
- Navigate to the Resources section and select File Sharing.
- Add the directory you want to use as a bind mount by clicking on the "+" button and selecting the folder.
- Apply and restart Docker Desktop if prompted.
Here’s the clear breakdown:
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 IMAGEThis works immediately as long as permissions allow it.
On Windows and macOS, Docker runs inside a Linux virtual machine. The VM cannot automatically access your host filesystem, so Docker Desktop requires:
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 myimageDocker Desktop needs to “share” /Users/myfolder into the Linux VM first.
| 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 |
If you want, I can create a clean explanation for your GitHub README breaking this down clearly for users on Linux, macOS, and Windows.
- 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-datain 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 - Ensure that the host directory (
-
Combinding and merging different types of volumes:
- You can combine named volumes and bind mounts in a single
docker runcommand. 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
feedbackto/app/feedbackin the container and the host directory/home/user/configto/app/configin 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"]
- You can combine named volumes and bind mounts in a single
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 pruneThis command will remove all unused volumes, freeing up space on your system.