Docker Volume Management
Implementation Reference
Source Files:
packages/core/src/docker/volume.ts
- Volume management operationspackages/core/src/docker/docker-client.ts
- Docker client implementationpackages/core/src/operations/file.ts
- File operationsapps/xec/src/commands/copy.ts
- Copy command implementation
Key Functions:
VolumeManager.create()
- Create volumesVolumeManager.remove()
- Remove volumesVolumeManager.list()
- List volumesVolumeManager.inspect()
- Get volume detailsDockerClient.copyToContainer()
- Copy files to containerDockerClient.copyFromContainer()
- Copy files from container
Overview
Xec provides comprehensive Docker volume management capabilities, enabling persistent data storage, file sharing between containers, and efficient data transfer between host and containers.
Volume Types
Named Volumes
Docker-managed volumes with persistent storage:
import { $ } from '@xec-sh/core';
// Create named volume
const volume = await $.docker.volume.create('app-data');
// Create with options
const volume = await $.docker.volume.create('db-data', {
driver: 'local',
driverOpts: {
type: 'nfs',
o: 'addr=10.0.0.1,rw',
device: ':/data'
},
labels: {
app: 'myapp',
env: 'production'
}
});
// Use in container
await $.docker.run('postgres', {
volumes: ['db-data:/var/lib/postgresql/data']
});
Bind Mounts
Host directory mounts for development:
// Mount host directory
await $.docker.run('node:18', {
volumes: [
'./src:/app/src:ro', // Read-only
'./config:/app/config:rw', // Read-write (default)
'./logs:/app/logs:delegated' // Performance mode
]
});
// Absolute paths
await $.docker.run('nginx', {
volumes: [
'/etc/ssl/certs:/etc/nginx/certs:ro',
'/var/www/html:/usr/share/nginx/html'
]
});
Tmpfs Mounts
In-memory temporary storage:
// Create tmpfs mount
await $.docker.run('alpine', {
tmpfs: {
'/tmp': 'size=100m',
'/run': 'size=10m,mode=0755'
}
});
// Or using mount syntax
await $.docker.run('alpine', {
mounts: [{
type: 'tmpfs',
target: '/app/cache',
tmpfsOptions: {
size: 104857600, // 100MB in bytes
mode: 0755
}
}]
});
Volume Operations
Creating Volumes
Create and configure volumes:
// Create simple volume
await $.docker.volume.create('my-volume');
// Create with configuration
const volume = await $.docker.volume.create('backup-volume', {
driver: 'local',
driverOpts: {
type: 'btrfs',
device: '/dev/sdb1'
}
});
// Create from another volume (clone)
await $.docker.volume.clone('source-volume', 'dest-volume');
Listing Volumes
List and filter volumes:
// List all volumes
const volumes = await $.docker.volume.list();
// Filter volumes
const appVolumes = await $.docker.volume.list({
filters: {
label: ['app=myapp'],
name: ['app-*']
}
});
// Get volume details
volumes.forEach(vol => {
console.log({
name: vol.Name,
driver: vol.Driver,
mountpoint: vol.Mountpoint,
size: vol.UsageData?.Size,
refCount: vol.UsageData?.RefCount
});
});
Inspecting Volumes
Get detailed volume information:
// Inspect volume
const info = await $.docker.volume.inspect('my-volume');
console.log({
name: info.Name,
driver: info.Driver,
mountpoint: info.Mountpoint,
createdAt: info.CreatedAt,
options: info.Options,
labels: info.Labels,
scope: info.Scope
});
// Check if volume exists
const exists = await $.docker.volume.exists('my-volume');
Removing Volumes
Remove unused volumes:
// Remove specific volume
await $.docker.volume.remove('old-volume');
// Force remove (even if in use)
await $.docker.volume.remove('data-volume', { force: true });
// Remove multiple volumes
const volumes = ['vol1', 'vol2', 'vol3'];
await Promise.all(
volumes.map(v => $.docker.volume.remove(v))
);
// Prune unused volumes
const pruned = await $.docker.volume.prune();
console.log(`Removed ${pruned.SpaceReclaimed} bytes`);