A named volume is Docker storage with an explicit name, such as todo-list. Its lifecycle is independent of the containers using it, so data remains available when an application container is replaced. section 6.2
Use a named volume to:
- Create storage before starting an application container. section 6.2
- Attach the same storage to different application versions. section 6.2
- Avoid relying on automatically generated volume identifiers. section 6.2
Preserving to-do data during an upgrade
Create the volume with `docker volume create todo-list`, then run version 1 with `-v todo-list:$target`. After adding data, remove the first container and run version 2 with the same `-v todo-list:$target` attachment. The application keeps its data while the container changes. section 6.2
# save the target file path in a variable: target='/data' # for Linux containers $target='c:\data' # for Windows containers # create a volume to store the data: docker volume create todo-list # run the v1 app, using the volume for app storage: docker container run -d -p 8011:8080 -v todo-list:$target ` --name todo-v1 diamol/ch06-todo-list:2e # add some data using the web app at http:/./localhost:8011 # remove the v1 app container: docker container rm -f todo-v1 # and run a v2 container using the same volume for storage: docker container run -d -p 8011:8080 -v todo-list:$target ` --name todo-v2 diamol/ch06-todo-list:2e-v2
Follow the complete Linux or Windows command sequence for creating, attaching, replacing, and reusing a named volume. section 6.2
