How Storage Works in Docker

Mounting persistent storage in docker

Planted .

All files created inside a container are stored on a writable container layer that sits on top os an immutable image layer.

So, when you modify something in the image layer, docker creates a copy in the read-write container layer. This is where all changes happen. This is called copy-on-write.

Data written to container layer does not persist when the container is destroyed.

So, if you want the data to persist, we can mount a volume to it. You can either create a new volume or use an existing file or directory (in which case you need to give full path). This second option is called bind mount.

# Create a volume
docker volume create volume_name

## Use --volume or --mount option with docker run
## --mount is the preferred method
docker run --mount type=volume,src=<volume-name>,dst=<mount-path>
docker run --volume <volume-name>:<mount-path>
Updated . Thoughts? Email me.