Overview

There are two ways to persist data in Docker, volume and bind mount, but there are other ways, such as tmpfs in Linux and named pipe in windows, but they are not universal, so I won’t go into them here. This article mainly introduces the differences between volume and bind mount and the usage scenarios, and takes out some details about them.

Data management diagram

Figure 1: Common ways to manage data in Docker
From: https://docs.docker.com/storage/volumes/

Getting Started

volume Use

[[email protected]]# docker volume create redis
[[email protected]]# docker run -v redis:/data redis:6.0 redis-server

bind mount Use

[[email protected]]# docker run -v /myredis/conf:/usr/local/etc/redis redis redis-server

Persistence method selection

volume usage scenarios

Volumes are the preferred way to persist data in Docker containers and services. Some usage scenarios for volumes include.

bind mount usage scenarios

In general, you should use volume whenever possible. bind mount is suitable for the following scenarios.

Usage

Either volume or bind mount can be used with -v/--volume or -mount, the difference between -v and -mount being.

Additional tips

container directory is not empty

host directory/volume does not exist

If you specify volume or the host’s directory with -v, but the directory or volume does not exist, then docker will create one for you.

Ref