Skip to content

Docker Volume ​

The filesystem and data inside a container disappear when the container is removed. An app that needs persistent data, such as a database, or files from your laptop, such as source code, should use a volume or a bind mount.

Types of volumes ​

Docker has two types of volume:

  • Anonymous volume: a volume without a name that you choose. Docker creates its name or identifier automatically.
  • Named volume: a volume that you name yourself, such as pgdata.

Anonymous volume ​

Example of an anonymous volume:

sh
docker run -d \
  -v /var/lib/postgresql/data \
  postgres:16

In this example, Docker creates the volume without you giving it a name. Docker still manages the volume, but it is harder to recognize and reuse because its name is not clear.

Named volume ​

Example of a named volume:

sh
docker run -d \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

pgdata is a name you choose. You can use any clear name that fits your needs, such as project-db-data or postgres16-data.

Both anonymous and named volumes are stored on your laptop or server, but Docker manages their storage location. You do not need to choose or change that location directly.

This lesson uses named volumes more often because their names are easy to remember, reuse, and manage through Docker CLI.

About paths ​

A path such as /var/lib/postgresql/data is a path inside the container, not a path on your laptop. When a volume is mounted, Docker connects the volume to that path:

Loading diagram...

Named volume mount format:

sh
-v <volume-name>:<path-inside-container>

Example:

sh
docker run -d --name db \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

In this example:

  • pgdata is the name of the named volume managed by Docker.
  • /var/lib/postgresql/data is PostgreSQL's data location inside the container.
  • Data written by PostgreSQL to this path is stored in pgdata.
  • If you remove the db container, the pgdata volume remains.

Volume information ​

To view volume information, including the location used by Docker:

sh
docker volume inspect pgdata

Use this information when debugging. Do not edit files directly in Docker's storage location. Manage volumes through Docker CLI.

Bind mount vs named volume ​

Both can provide data or files to a container. The main difference is how their storage is managed and what they are used for.

Bind mount

  • Description: a folder or file from your laptop is mounted at a path inside the container.
  • Storage location: you choose and know the folder on your laptop, such as ./html.
  • Use case: source code during development, or a configuration file that you need to edit directly on your laptop.

Named volume

  • Description: a named volume managed by Docker.
  • Storage location: Docker storage. You do not need to know its physical path on the laptop or server.
  • Use case: database or application data that must remain after the container is removed.

Use a bind mount when you need to access and change files directly from your laptop. Use a named volume when Docker should manage the data.

Bind mount example ​

Run Nginx with a page from an html folder on your laptop:

sh
mkdir -p html
echo '<h1>From the laptop</h1>' > html/index.html

docker run -d --name web -p 8080:80 \
  -v "$(pwd)/html:/usr/share/nginx/html:ro" \
  nginx:1.27-alpine

-v laptop:container connects a laptop folder to a folder inside the container. :ro means read-only inside the container. This is useful for static files.

Change html/index.html, then refresh the browser. You do not need to rebuild the image.

On Windows PowerShell, the path format is different. Use an absolute path or Docker Desktop, which supports bind mounts from project folders.

Named volume example ​

sh
docker volume create pgdata

docker run -d --name db -e POSTGRES_PASSWORD=secret \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

To prove that a named volume keeps data after its container is removed, remove the db container:

sh
docker rm -f db

Then list the volumes:

sh
docker volume ls

The data is still in the pgdata volume. A new container with -v pgdata:/var/lib/postgresql/data can see the same data.

TIP

docker rm -f db only stops and removes the container named db. It does not remove the pgdata named volume, so the data in the volume remains.

To remove the volume and its data, run docker volume rm pgdata separately. Do not run that command on a work machine unless you are sure the data can be deleted.