Docker Storage

Docker Storage #

File System #

When installed locally, it creates a folder structure at /var/lib/docker. This is where Docker stores all the data by default (containers, images, volumes, etc.)

Layered Architecture #

When Docker builds images, it builds in layers, going down sequentially the Dockerfile.

^ This enables re-use and lightweight rebuilds - super efficient and lightweight.

Volumes #

Persistent volumes allows for data to last beyond the duration of the life of a container.

docker volume create <volume name>

This creates a folder at /var/lib/docker/volumes/<volume name>

Volume Mounting #

Then run the Docker container with a pointer to the volume (volume mounting):

docker run -v <volume name>:<path within container where data is stored> <image name>

If docker run -v <volume name>:... is run with a <volume name> that hasn’t already been created, running this command will create the volume as if docker volume create were run.

Bind Mounting #

To connect pre-existing data located at a different path on the Docker host:

docker run -v <path to data>:<path within container where data is stored> <image name>

Alternatively, more verbosely:

docker run \
    --mount type=bind, source=<path on host>, target=<path within container> <image>