All-Things-Docker-and-Kubernetes

Persisting Data



Recall that a docker image has different layers, with the first layer as the base image that the image will use and the layers on top as packages being installed.

The last layer is a writeable layer which applications will use. If a container is started without defining a specific storage option, any data written to the default storage by an application running in a container will be removed as soon as it is stopped.

For this scenario, Docker provides three storage options.

Bind mounts

Bind mounts work by mounting a directory (that’s on the host) to the container. This is a good storage option since the data lives on a directory outside of the container. When the container is stopped or terminated, the data is perfectly safe and intact in the directory residing on the host.

It is important to note that you will need the fully qualified path of the directory on the host to mount it inside the directory.

Use cases:

Volumes

Another option is to use volumes which is similar to bindmounts but docker manages the storage on the host. This means you don’t need to know the directory path on the host since this is being managed by Docker itself.

Volumes also allow you to use external storage mechanisms using different drivers, which means you are not limited to the local volume.

In addition,

Use cases:

To add a local volume to a container:

$ docker run -d \
    -v <name>:</path/on/container> \ 
    --name <name> \
    <image>

To mount existing directory to a container:

$ docker run -d \
    -mount type=bind, source=</path/on/node>, target=<name> \
    --name <name> \
    <image>

tmpfs (Temporary filesystem)

This is an in-memory filesystem, which is basically inside the container. This isn’t persistent and data stored here are only accesible as long as the container is running.


Back to first page