Data?
Application Temporary App Data Permanent App Data
(Code + Environment) (e.g. entered user input) (e.g. user accounts)
Written & provided by Fetched / Produced in Fetched / Produced in
you (= the developer) running container running container
Added to image and Stored in memory or Stored in files or a
container in build phase temporary files database
“Fixed”: Can’t be changed Dynamic and changing, Must not be lost if
once image is built but cleared regularly container stops / restarts
Read + write, Read + write,
Read-only, hence stored
temporary, hence stored permanent, stored with
in Images
in Containers Containers & Volumes
A Container Is Based On An Image
Container Layer (read-write)
Container Instruction #3: Image Layer 3
Read-write Image
Instruction #2: Image Layer 2
Read-only
Instruction #1: Image Layer 1
Understanding Volumes
Volumes are folders on your host machine hard drive which are
mounted (“made available”, mapped) into containers
Host (Your Computer)
/some-path /app/user-data
Volumes persist if a
container shuts down. If a
A container can write data
container (re-)starts and
into a volume and read data
mounts a volume, any data
from it.
inside of that volume is
available in the container.
Two Types of External Data Storages
Volumes Bind Mounts
(Managed by Docker) (Managed by you)
Anonymous Volumes Named Volumes
Docker sets up a folder / path on your host machine,
You define a folder / path on
exact location is unknown to you (= dev).
your host machine.
Managed via docker volume commands.
A defined path in the container is mapped to the created volume / mount.
e.g. /some-path on your hosting machine is mapped to /app/data
Great for data which
Great for persistent, editable
should be persistent
(by you) data
but which you don’t
(e.g. source code).
need to edit directly.
Understanding Container / Volume Interaction
Volumes are
Volume mounted into
Container
/some-path Container
[Link] /app/data
[Link]
[Link]
Container
data is [Link]
Bind Mount stored in
/some-other-path volume
/app/code
[Link] Volume data
[Link]
is accessible
in container
Volumes & Bind Mounts – Quick Overview
docker run –v /app/data … Anonymous Volume
docker run –v data:/app/data … Named Volume
docker run –v /path/to/code:/app/code … Bind Mount
Volumes – Comparison
Anonymous Volumes Named Volumes Bind Mounts
Created in general – not Location on host file
Created specifically for a
tied to any specific system, not tied to any
single container
container specific container
Survives container Survives container Survives container
shutdown / restart unless shutdown / restart – shutdown / restart –
--rm is used removal via Docker CLI removal on host fs
Can not be shared across Can be shared across Can be shared across
containers containers containers
Since it’s anonymous, it
Can be re-used for same Can be re-used for same
can’t be re-used (even on
container (across restarts) container (across restarts)
same image)
ARGuments & ENVironment Variables
Docker supports build-time ARGuments and
runtime ENVironment variables
ARG ENV
Available inside of Dockerfile, NOT
Available inside of Dockerfile & in
accessible in CMD or any application
application code
code
Set on image build (docker build) Set via ENV in Dockerfile or via --env
via --build-arg on docker run
Module Summary
Containers can read + write data. Volumes can
help with data storage, Bind Mounts can help with
direct container interaction.
Containers can read + write data, but Volumes are folders on the host machine,
written data is lost if the container is managed by Docker, which are mounted
removed into the Container
Named Volumes survive container Anonymous Volumes are attached to a
removal and can therefore be used to container – they can be used to save
store persistent data (temporary) data inside the container
Bind Mounts are folders on the host Build ARGuments and Runtime
machine which are specified by the user ENVironment variables can be used to
and mounted into containers – like make images and containers more
Named Volumes dynamic / configurable
Read-Only, Read-Write & Volumes
Images Containers
Read-only Read & Write
Once created, you need to re-build A running container can store data
them to change something (e.g. incoming user data)
Application data (e.g. user data) is But: Data is lost when a container
NOT stored in images stops
Solution for persistent data: Volumes