A stateful application retains data from earlier interactions or executions, so later behavior depends on what happened before. Unlike stateless applications, its data cannot be treated as disposable with each container. Docker supports stateful apps by storing application data in volumes that survive container replacement. section 6.0 section 6.2
A Docker volume makes statefulness manageable:
- Create or declare a volume and attach it to the application container. section 6.2
- Expose the volume as a normal directory, such as /data or C:\data. section 6.2
- Let the application read and write normally while Docker stores the data independently of the container. section 6.2
- Attach the same volume to a replacement container so previously stored data remains available after an update. section 6.2
Keeping a to-do list through an update
Run a to-do application with a named volume mounted at its data directory, add items through the web interface, remove the first container, and start a newer container with the same volume. The updated application can still access the original list. 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/ .
The Dockerfile declares VOLUME /data, giving the stateful application a persistent storage location when its container starts. section 6.2













