6.3: Named Volumes (my_data:/var/www/html): Let Docker manage storage

Now that we’ve covered Bind Mounts, let’s move on to the second method of data persistence: Named Volumes.

Unlike Bind Mounts,Docker handles all storage management. You don’t need to create or manage folders on your machine:Docker creates and organizes everything automatically. This makes your project more portable and secure, especially in production.

How does it work?

A Named Volume is simply a volumeDockerwith a specific name.
You mount it in a container to store data in a specific location.

Example with an Apache/PHP web container:

docker run -d \
--name mon-site \
-v my_data:/var/www/html \
php:8.2-apache

Here:

  • Dockermy_data is the name of the volume.
  • /var/www/html is the folder inside the container where your web files are located.
  • Docker creates the volumemy_dataif it hasn’t already been created, and ensures that everything written to it/var/www/htmlremains available even if the container is deleted.

Simplified diagram:

CONTENEUR : /var/www/html


VOLUME NOMMÉ : my_data
(stockage géré par Docker)

✅ Benefits of Named Volumes

  • Docker Automatically manages the volume’s location and structure.
  • Data remains persistent even after the container is deleted or updated.
  • More portable and secure: you can move your containers to another server without worrying about local paths.
  • Ideal for production, where manual management of host directories can be problematic.

⚠️ Things to know

  • You do not have direct access to the files as you would with a Bind Mount.
  • To inspect or back up files, you’ll need to useDockerthe CLI or create a temporary container: docker run --rm -v my_data:/data busybox ls /data
  • Volumes take up disk space, butDocker handles everything.
  • ⚠️ Be careful withDockerCompose:
    The commanddocker-compose down -vdeletes all volumes associated with the project, so all your persistent data will be lost. Use it with caution, especially in production.

Practical example

Creating a WordPress container with a Named Volume for file persistence:

docker run -d \
--name wordpress \
-v wordpress_data:/var/www/html \
-e WORDPRESS_DB_HOST=db \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=secret \
wordpress:latest

Even if the container is deleted and recreated:

docker rm -f wordpress
docker run -d \
--name wordpress \
-v wordpress_data:/var/www/html \
-e WORDPRESS_DB_HOST=db \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=secret \
wordpress:latest

✅ Files, themes, and plugins remain intact, as everything is stored in the named volume.


🧠 Key takeaway

  • Named Volumes are managed byDocker and are persistent.
  • They are ideal for production and multi-container environments.
  • For rapid development and direct file access, Bind Mounts remain convenient.
  • ⚠️ Compose Alert:docker-compose down -v permanently deletes all associated volumes.