Understanding Docker volumes

Docker volumes was one of the last things I understood of Docker. As you can get started with Docker quite conveniently without using any volumes I never took the time to really understand them. But using volumes gives you the opportunity to work with data in Docker. Primarily, volumes offer persistent storage and shared state. So it’s high time to have a look at Docker volumes.

Volumes are used to persist data like database files, log files or any other container-independent data outside of a Docker container. Volumes map directories in a container to a defined directory on the host.

There exist two types of Docker volumes, bind-mount volume and managed volume. The former maps any user-specified directory or file on the host operating system. The latter uses internal Docker locations that are managed by Docker itself.

A bind-mount volume is created via the v-flag in the docker run command as follows:

docker run -it -v ~/shared:/tmp/data ubuntu /bin/bash

This will map the /tmp/data directory inside the container to the shared directory inside the local users directory on the host (~ is the shortcut for the local users directory in windows). All locations must be specified with absolute paths. This can be checked via:

docker inspect --format='{{json .Mounts}}'

Once the container is started, you can navigate to the /tmp/data directory and all files contained in the ~/shared directory on the host will be displayed. In the same way, single files can be mapped as well. Read-only volumes can be specified by appending :ro to the docker volume.

Bind-mount volumes have the disadvantage that they make the container not portable to any other host and volumes of different containers can conflict with each other. Thus, its best to avoid these volumes in generalized platforms and use Docker-managed volumes instead. Managed volumes are created via the v-flag as well, but without specifying a directory inside the container:

docker run -it -v ~/shared: /bin/bash

Besides persistent storage, volumes are used to share data between different containers. This can be achieved in two ways. Either each container has a bind-mount volume to the same location or managed volumes are shared between them. The former is quite straightforward whereas for the latter the —–volumes-from flag is used. This flag is used to copy a managed volume by a so called volume container into each container. A volume container is a single container that only defines a volume. This looks as follows:

docker run -d --volumes-from vc_shared ubuntu

Docker can’t delete bind-mount volumes because this is outside of its scope. However, care must be taken to always remove managed volumed when a container is deleted. This is done via the v-flag:

docker rm -v ubuntu

Using this v-flag option is really important. If deleting a container without this option, the managed volume becomes orphan and subsequently requires any manual steps to be removed.

Werbung

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s