0
0
Dockerdevops~5 mins

Named volumes for data sharing in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you want to save data from a container or share data between containers. Named volumes let you store data outside the container so it stays safe even if the container stops or is removed.
When you want to keep a database's data safe even if the database container is deleted.
When you want two or more containers to access the same files or folders.
When you want to back up or inspect data created by a container easily.
When you want to avoid losing important files after container updates or crashes.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  app:
    image: nginx:1.23
    volumes:
      - shared-data:/usr/share/nginx/html
  backup:
    image: busybox
    command: sleep 3600
    volumes:
      - shared-data:/backup
volumes:
  shared-data:

This file defines two services: app and backup. Both use the same named volume shared-data. The app service uses it to serve files, and the backup service can access the same files for backup. The volumes section declares the named volume shared-data so Docker manages it outside containers.

Commands
This command starts the services defined in the docker-compose.yml file in the background. It creates the named volume and attaches it to both containers so they can share data.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "shared-data" with default driver Creating app ... done Creating backup ... done
-d - Run containers in detached mode (in the background)
This command lists all Docker volumes on your system, showing that the named volume 'shared-data' was created.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local shared-data
This command lists the files inside the shared volume folder in the 'app' container to verify the volume is mounted and accessible.
Terminal
docker-compose exec app ls /usr/share/nginx/html
Expected OutputExpected
index.html
This command stops and removes the containers and network but keeps the named volume so data is not lost.
Terminal
docker-compose down
Expected OutputExpected
Stopping app ... done Stopping backup ... done Removing app ... done Removing backup ... done Removing network default
Key Concept

Named volumes store data outside containers so it persists and can be shared safely between containers.

Common Mistakes
Using bind mounts instead of named volumes for sharing data.
Bind mounts depend on host paths and can cause permission or portability issues.
Use named volumes declared in Docker Compose or Docker CLI for safer, managed data sharing.
Removing containers with 'docker rm -f' without preserving volumes.
This deletes containers but not volumes, which can cause confusion about leftover data.
Use 'docker-compose down' to stop containers but keep volumes, or explicitly remove volumes if needed.
Summary
Named volumes keep data safe outside containers and allow sharing between containers.
Docker Compose can declare and use named volumes easily in the 'volumes' section.
Use 'docker-compose up -d' to start containers with volumes and 'docker-compose down' to stop without losing data.