liveBook · Manning

Volume instruction

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

The VOLUME instruction is a Dockerfile directive that declares a directory as a mount point backed by a volume when a container is created.

The VOLUME instruction declares a directory as a mount point backed by a Docker volume when a container starts. The application reads and writes the directory normally, while the data survives container removal. section 6.2

Use the instruction to make container storage persistent:

  • Write VOLUME followed by the target directory, such as VOLUME /data. section 6.2
  • Starting the image automatically creates and attaches a volume at that directory. section 6.2
  • Reuse the volume with a replacement container so application data remains available across updates. section 6.2

Persisting a to-do list

A Dockerfile can declare VOLUME /data for an application that stores its data there. Run the image with docker container run --name todo1 -d -p 8010:8080 diamol/ch06-todo-list:2e, add items through the web app, and inspect the attached mount with docker container inspect --format '{{json .Mounts}}' todo1. Removing the container does not remove the volume. section 6.2

Listing 6.1 Part of a multi-stage Dockerfile using a volume
FROM diamol/dotnet-aspnet:2e
WORKDIR /app
ENTRYPOINT ["dotnet", "ToDoList.dll"]

VOLUME /data
COPY --from=builder /out/ .

View the Dockerfile fragment that declares /data as a volume in a multi-stage image. section 6.2

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

How deep it goes
Explains syntax, automatic attachment, persistence, inspection, and reuse, but does not detail volume-driver internals.
Examples
Uses a multi-stage to-do-list image, /data, container inspection, web-entered items, and replacement containers.
What it assumes
Assumes Dockerfile, image, and container familiarity while demonstrating volume-backed persistence through a runnable application.
What you can do after
Operate/configure a Docker image that creates persistent storage automatically and verify its attached volume.
How it connects
Connects Dockerfiles, images, containers, Docker volumes, persistent storage, and stateful applications.

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.