liveBook · Manning

Named volume

As explained in Learn Docker in a Month of Lunches, Second Edition

A named volume is a Docker volume identified by an explicitly assigned name rather than an automatically generated identifier.

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

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains creation, attachment, lifecycle, and upgrade reuse, but does not detail volume-driver internals.
Examples
Demonstrates a to-do application upgraded from version 1 to version 2 while retaining data in todo-list.
What it assumes
Assumes familiarity with running Docker containers and mounting paths inside containers.
What you can do after
Operate Docker commands to create, attach, inspect, and reuse named storage across application containers.
How it connects
Connects Docker volumes with containers, Dockerfiles, persistent storage, and application upgrades.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.