Creating and mounting data volumes
All meaningful applications consume or produce data. Yet containers are preferably meant to be stateless. How are we going to deal with this? One way is to use Docker volumes. Volumes allow containers to consume, produce, and modify state. Volumes have a life cycle that goes beyond the life cycle of containers. When a container that uses a volume dies, the volume continues to exist. This is great for the durability of state.
Modifying the container layer
Before we dive into volumes, let's first discuss what's happening if an application in a container changes something in the filesystem of the container. In this case, the changes are all happening in the writable container layer. Let's quickly demonstrate this by running a container and execute a script in it that is creating a new file:
$ docker container run --name demo \ alpine /bin/sh -c 'echo "This is a test" > sample.txt'
The preceding command creates a container named demo
and inside this container...