- Data volume
- Container data volume
Read-only based file system, every time you write to a new layer with the file, it will have a new copy for the file and keep the old file in the previous layer. So, never write keys into the container.
Because docker uses the union file system, when it needs a mutable state of something. Volume comes to help.
Two ways to initialize volumes.
docker run -it --name <something> -v /data <image> <command>
, -v means volumeVOLUME
in the docker file
In order to inspect the volumes, run docker inspect -f {{.Volumes}} <name>
.
- Starts a volume container with the normal data volume.
- Then start the application which needs a volume using
--volumes-from <target>
to starts the application.
┌───────────────────────────────────────────────────────────────┐
│┌────────────────┐ │
││ HOST MACHINE │ │
│└────────────────┘ │
│┌────────────────────────────┐ ┌─────────────────────────────┐│
││ ┌──────────┐ │ │ ┌──────────┐ ││
││ │postgresql│ │ │ │ pg-data │ ││
││ └──────────┘ │ │ └──────────┘ ││
││┌─────────────────────────┐ │ │ ┌─────────────────────────┐ ││
│││/var/lib/postgresql/data │◀┼──┼▶│/var/lib/postgresql/data │ ││
││└─────────────────────────┘ │ │ └─────────────────────────┘ ││
││ │ │ ▲ ││
│└────────────────────────────┘ └──────────────┼──────────────┘│
│ ┌───────────────┘ │
│ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ /var/lib/docker/vfs/dir/<sha> │ │
│ └──────────────────────────────────────┘ │
└───────────────────────────────────────────────────────────────┘
Anything after the VOLUME instructions in a Dockerfile will not able to make changes to the volume.
FROM debian:wheezy
RUN useradd foo
VOLUME /data
RUN touch /data/x
RUN chown -R foo:foo /data
The code above will not work. Instead, it need to be look like:
FROM debian:wheezy
RUN useradd foo
RUN mkdir /data; touch /data/x; chown -R foo:foo /data
VOLUME /data
The volume will only get deleted when running docker rm -v
, so the -v
is
always required. The other way is running the docker container with -rm
.
But, if some other container is linking to the volume, even if we runs the rm
with -v
flag, it will also not able to delete the volume.
When running out of space with a lot of zombie volumes, we can use Docker Volume Manager to clean up.