Docker data storage: Docker Volumes vs. Host Folders

 

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!

Questions / Comments


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