Docker Volume Persistence

Jinna Balu
2 min readDec 26, 2020

The container uses and generates data folders that are available for the container but not to the host machine. When the container gets restarted or crashed the data generated inside is deleted permanently. Docker volume is a process of saving the data to the host machine from the container.

Docker Volume COPY | Docker data one time copy

Get the folder copied from container to host machine, this option useful for one-time copy, but not recommended for the databases which need the stateful behavior. Using docker cp we can copy the docker container volume to the host machine.

example:

NOTE: The issue with the above approach is, it will not persists the volume or file or dir, as you remove the container it will be lost. This is not recommended for volume persistency dynamically.

Volume Mounting | Docker Volume

Docker volume is a process of mounting the container volume to the host directory. This can be done in three ways.

  1. Anonymous Volumes
  2. Named Volumes
  3. Named Bind Volumes Or Host Volumes

1. Anonymous Volumes

The location of the data mounted is managed by docker. It is very difficult to refer to the same volume. We can identify the volumes used by containers by using container inspection. To create an anonymous docker volume run

2. Named Volumes

Named volumes are defined by the user but the location of the data is managed by docker. Referring to named volumes is very easy compared to anonymous volumes.

We can do this in two ways creating a docker volume and referring to it while running the container with the docker run command.

We can achieve both above commands within one docker-compose.yml

Note : Deleting the named volumes will delete the data as the data location is managed by docker.

3. Named Bind Volumes Or Host Volumes

Named volumes binding or mounting to the custom directory path of the host machine. Docker volume location is defined by the user.

Volumes help save data across restarts of your docker containers. If you need to use a volume, consider the difference between the various kinds before starting development.

Originally published at https://jinnabalu.tech on December 26, 2020.

--

--