Docker Volumes

Docker volumes are a way to persistently store and share data between Docker containers and the host system. When a container is created, it runs in an isolated environment with its own file system. However, any changes made to the container's file system are typically lost when the container is stopped or removed. Volumes provide a solution to this problem by allowing you to create a special location on the host system or within another container, which can be mounted into a container as a directory. This directory acts as a shared storage space that persists even when the container is removed or restarted. Volumes can be used for various purposes such as storing application data, configuration files, and database files. There are different types of volumes in Docker:

Named volumes: Named volumes are the simplest type of volume. You can create a named volume using the docker volume create command or it can be created automatically when you run a container and specify a volume using the -v or --mount flag. Named volumes have a unique name assigned by Docker and are managed by Docker internally. They are easy to use and provide good isolation.

characteristics of named volumes:

Data in named volumes will survive even if the container stops.

Named volumes must be explicitly named and can be used as a reference in multiple containers.

However, if you remove the container associated with a named volume, the data remains, but the reference to the data is lost.

Bind Mounts (Host-mounted volumes) : Host-mounted volumes allow you to mount a directory from the host system into a container. With host-mounted volumes, the data is stored directly on the host's file system, and any changes made inside the container are immediately visible on the host, and vice versa. Host-mounted volumes are created using the -v or -mount flag followed by the host directory path and the container directory path.

Anonymous volumes: Anonymous volumes are similar to named volumes, but they are not given an explicit name. They are created automatically by Docker when a container is run and a new volume is specified without providing a name. Anonymous volumes are useful when you don't need to manage or reuse the volume explicitly and just want a disposable storage space for a specific container. Volumes can be used with Docker CLI commands, such as docker run, docker create, and docker-compose, by specifying the volume configurations in the command arguments or the container configuration files. The exact locations of named volumes on the host system depend on the storage driver being used by Docker.

Local driver: The local driver is the default storage driver for most Docker installations. For Linux, the named volumes are stored in the /var/lib/docker/volumes directory on the host system.