Skip
Arish's avatar

18. Volume Commands


Creating Volumes

bash
1# Create named volume
2docker volume create app-data
3
4# Create with options
5docker volume create --driver local \
6  --opt type=nfs \
7  --opt o=addr=192.168.1.1,rw \
8  --opt device=:/path/to/dir \
9  nfs-data

Using Volumes

bash
1# Named volume
2docker run -v app-data:/var/lib/postgresql/data postgres
3
4# Anonymous volume
5docker run -v /var/lib/postgresql/data postgres
6
7# Read-only
8docker run -v app-data:/app/data:ro nginx

Managing Volumes

bash
1# List volumes
2docker volume ls
3
4# Inspect
5docker volume inspect app-data
6
7# Remove unused
8docker volume prune
9
10# Remove specific
11docker volume rm app-data
12
13# Remove all
14docker volume rm $(docker volume ls -q)

Backup and Restore

bash
1# Backup
2docker run --rm -v app-data:/data -v $(pwd):/backup \
3  alpine tar cvf /backup/backup.tar /data
4
5# Restore
6docker run --rm -v app-data:/data -v $(pwd):/backup \
7  alpine tar xvf /backup/backup.tar -C /