# Types of data
# Container and Image Data
Note for layers: when the state of an Image in a .dockerfile changes - a new layer is created.
# Container Data
Data can be stored inside containers as files, just as you would normally do on your local development machine.
However, once a container is removed, the data will be lost.
Stopping and restarting a container will not delete your data within that container.
# Volumes
-
Volumes are folders on your host machine hard drive which are mounted (“made available”, mapped) into containers.
-
Volumes persist if a container shuts down. If a container (re-)starts and mounts a volume, any data inside of that volume is available in the container. Not only for restarting - when removing a container the data persists.
-
A container can write data into a volume and read data from it.
# Defining Volumes
The VOLUME command specifies a path inside the container that will be treated as a volume. The mapping to the host’s file system is defined by the run command when starting a container.
# Volumes & Bind Mounts
- Anonymous Volume - we don’t know where the volume in the container is
- hardly used
- Named Volume - docker automatically allocates the data at a specific destination in the container
- mostly used
- Bind Mount - specifies the destination on your host-machine, which will also be the volume for the container
- often used
# Volumes vs. Bind Mounts
# Container / Volume Interaction
# 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 written data is lost if the container is removed.
- Volumes are folders on the host machine, managed by Docker, which are mounted into the container.
- Named Volumes survive container removal and can therefore be used to store persistent data.
- Anonymous Volumes are attached to a container - they can be used to save (temporary) data inside the container.
- Bind Mounts are folders on the host machine which are specified by the user and mounted into containers - like Named Volumes.