# Docker: Two Types of External Data Storage
###### tags: `Docker` `Docker Volume` `Docker Bind Mounts`
[TOC]
# Volume
## What are Volumes?
They are **folders on your host machine** hard drive which are mounted("made available", mapped) **into containers**.
**NOTE:** Volumes are managed by Docker. But can help with data storage specially when container removal happens.
## Volume Types
### Anonymous Volumes
These are deleted when container is shut down, because they are recreated when container is created. These are closely attached to one specific container.
A countainer **can write** data and **read** data from container.
**NOTE:** Add Volume by putting **VOLUME "/app/feedback"** (VOLUME "/path") in Dockerfile
### Named Volumes
These are not deleted when container is shut down. These are not attached to specific container.
```=
//-v = volume
//docker run -d -p 3000:80 --rm --name feedbackapp -v feedback:/app/feedback feedback:volume
docker run -d -p localPort:exposePort --rm --name containerName -v volumeName:/volumePath imgName
```
It is **IMPORTANT** to know that we have to restart using the same -v /path in order to see the same volume information.
## Remove Volumes
```=
docker volume rm VOL_NAME or docker volume prune.
```
# Bind Mounts
**NOTE:** We define the path/folder in our host machine, unlike volumes which are set by Docker.
**NOTE:** Usually it will be used during development.
**Advantage:** Great for persistent, editable data.

```=
docker run -d -p 3000:80 --name feedbackapp -v feedback:/app/feedback -v "C:/Users/Claudia/Downloads/data-volumes-01-starting-setup/data-volumes-01-starting-setup:/app" -v /app/node_modules feedback:volume
```
docker run -d -p 3000:80 --name feedbackapp -v feedback:/app/feedback **-v "C:/Users/Claudia/Downloads/data-volumes-01-starting-setup/data-volumes-01-starting-setup:/app"** -v /app/node_modules feedback:volume
If we separate them, it would be:
1. docker run
2. -d ***== run in detached mode***
3. -p 3000:80 ***== port localPort:exposePort***
4. --name feedbackapp ***== name container***
5. -v namedVolume
6. -v "AbsolutePathToHostMachine(folder/path):/WORKDIR" ***== bindMount the folder you want to relate on host machine***
7. -v anonymousVolume ***== the folder you want as exception from bindMount***
8. feedback:volume == imgName:tag
>To use bindMount **you must set the path in Docker Setting Resources(File Sharing)**. Can be Parent Folder or Specific Folder:
[color=purple]
**Why we have an anonymous volume after bind mount?**
That is because after bind Mount the files are overriden by the path we have in local host (in this case we put the whole project). This means that the node_modules created by **npm install** in our DockerFile created in Container will be gone. In order to keep it up we wrote an anonymous volume. Sometimes anonymous volume is used like these cases.

## Make Volumes Read-Only
Avoid Docker write in folder or sub folder of file.
-v "C:/Users/Claudia/Downloads/data-volumes-01-starting-setup/data-volumes-01-starting-setup:/app**:ro**"
> :ro > read only
## Manage Volumes

**Note:** Removing a volume will delete all information, even if same volume name is created, data will be gone forever.
# Comparisons

**NOTE:** Anonymous Volume is **useful for locking in certain data**, which already exists inside a module. It can avoid data gets overwritten by another module.
