Docker data storage: Docker Volumes vs. Host Folders

Diese Seite gibt es auch in Deutsch

I run a handful of Docker containers on a single Linux host. Docker containers are known to be replaced when they are rebuilt or swapped to a newer image version, losing all changed data. To permanently store certain folders of the container, they need to be swapped out via volumes or bind mounts. I usually have specific paths of the containers in defined folders connected to the host for this purpose (bind mounts). One of the reasons for using bind mounts instead of volumes is that I can more easily copy, backup or transfer the folders to another host via the host. But is this even the right approach? Would I be better off using Docker volumes? What is the advantage of Docker volumes?

Bind mounts

As mentioned earlier, bind mounts are defined folders of the host machine that are joined into specific directories of the Docker container. The host folder fits seamlessly in the Docker container's file system and behaves like any other folder in the container's file system. Data can be read and modified at the host folder.

Docker Volumes

Volumes are the recommended way to store Docker container data. Volumes are folders that are fully managed by Docker, which makes the storage independent of the underlying host and more secure. Docker volumes can be managed using Docker CLI commands or the Docker API.

Docker volumes vs. bind mounts

Advantages of volumes

  • Volumes are managed by Docker and are thus, just like containers: operating system independent.
  • Volumes can be managed using Docker CLI commands or the Docker API.
  • When using Docker Desktop on Windows or MAC, volumes deliver better performance.
  • The potential use of volume drivers and the associated options to store volumes on remote hosts or cloud providers and encrypt the content or add other functionality.
  • The content of Docker volumes can be pre-populated
  • According to the official Docker documentation, volumes are easier to back up or migrate than bind mounts:
    Volumes are easier to back up or migrate than bind mounts.
    Source: https://docs.docker.com/storage/volumes/ (retrieval date: 09/06/2022)
    In my opinion, this statement does not apply to every setup. When using volumes, access should no longer be directly through the host, making it difficult to use host commands such as rsync or another backup installed on the host. 

Disadvantages

Use Docker volumes

When starting a container with docker run, volumes can be specified with the -v or -mount parameter:

docker run -v volumename:/app nginx:latest

When using --mount as a parameter, the properties are specified in a bit more detail:

docker run --mount source=volumename,target=/app nginx:latest

Docker Compose allows volumes to be defined in the volumes pane and specified in services as follows:

docker-compose.yml

version: '3.1'

services:
  service1:
    image: test
    ...
    volumes:
      - volumename4service1:/var/www/html

  service1:
    image: test2
    ...
    volumes:
      - volumename4service2:/var/www/html

volumes:
  volumename4service1:
  volumename4service2:
...

For comparison: Use bind mounts

To use bind mounts instead of volumes, simply use a path in the local file system instead of the volume name. The path can be an absolute path or a relative path. Absolute paths start with "/" and are independent of the current folder in which the docker-compose file resides. Relative paths can be used with "./folder" name as a subfolder relative to the docker-compose.yml file:

File: docker-compose.yml

version: '3.1'

services:
  service1:
    image: test
    ...
    volumes:
      - /var/absolutefolderpath:/var/www/html

  service1:
    image: test2
    ...
    volumes:
      - ./relativefolderpath:/var/www/html
...

Viewing volumes of a container: docker inspect

To find out which volumes or mounts a container uses, the command "docker inspect containername" can be used:

docker inspect wordpress

FAQ

Where are Docker volumes located?

Volumes are stored in a folder in the host file system and managed by Docker (for Linux: /var/lib/docker/volumes/).

What is the difference between Docker volumes and bind mounts.

Both: Docker volumes and bind mounts link specific folders of a container to a folder on the host operating system. Bind mounts are defined folders on the host operating system, but for Docker Volumes, the path is managed by Docker. Docker volumes are also located in a folder on the host operating system, but the difference is that Docker predefines and manages the folder and enables the use of Docker CLI commands or the Docker API.

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Updated: 2022-12-26 von Bernhard 🔔


Top articles in this section


Nextcloud Server Docker | Setup + https: Let's Encrypt [ssl]
To synchronize contacts, appointments, and photos of my NAS, I tested Nextcloud and thus turned my back on other cloud providers for my private data. Thanks to Docker, the installation is easier and more flexible than ever, allowing Nextcloud to run on almost any hardware.

Running Bitwarden in Docker - Setup step by step
Bitwarden is a web-based password manager, similar to LastPass, but open source and the ability to run (host) it yourself. How Bitwarden compares to other password managers, I have considered on the following page: Password Managers Secure? KeePass vs LastPass vs Bitwarden. Bitwarden consists of several services, which can be provided via different containers. The relatively complex setup has been simplified with "Bitwarden Unified" especially for self-hosting by packing all services into one co...

Commissioning Zigbee2MQTT in Docker - step by step
Zigbee2MQTT is an open source Zigbee bridge which can be easily integrated into existing smart home solutions thanks to the MQTT network protocol. As an example, Zigbee2MQTT combined with MQTT broker Mosquitto and Home Assistant can collect, display, record and control data from Zigbee devices. The setup described here uses Docker as a base. Manufacturer's website: https://www.zigbee2mqtt.io

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details