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
