Skip to content
GitHubXDiscord

Volume

The Volume resource allows you to create and manage persistent Docker volumes using Alchemy.

import * as docker from "alchemy/docker";
const myVolume = await docker.Volume("data-volume", {
name: "app-data",
driver: "local"
});
NameTypeRequiredDescription
namestringYesDocker volume name
driverstringNoVolume driver to use (defaults to “local”)
driverOptsRecord<string, string>NoDriver-specific options
labelsVolumeLabel[] | Record<string, string>NoCustom metadata labels for the volume

The VolumeLabel interface has the following structure:

interface VolumeLabel {
name: string; // Label name
value: string; // Label value
}
NameTypeDescription
idstringVolume ID (same as name for Docker volumes)
mountpointstringVolume mountpoint path on the host
createdAtnumberTime when the volume was created
import * as docker from "alchemy/docker";
// Create a simple Docker volume for persistent data
const dataVolume = await docker.Volume("data-volume", {
name: "postgres-data"
});
// Create a Docker volume with custom driver options
const dbVolume = await docker.Volume("db-data", {
name: "mysql-data",
driver: "local",
driverOpts: {
"type": "nfs",
"o": "addr=10.0.0.1,rw",
"device": ":/path/to/dir"
}
});
// Create a volume with labels (array format)
const logsVolume = await docker.Volume("logs-volume", {
name: "app-logs",
labels: [
{ name: "com.example.environment", value: "production" },
{ name: "com.example.created-by", value: "alchemy" }
]
});
// Create a volume with labels (record format)
const configVolume = await docker.Volume("config-volume", {
name: "app-config",
labels: {
"com.example.environment": "staging",
"com.example.created-by": "alchemy"
}
});
// Use volumes with a container
const dbContainer = await docker.Container("database", {
image: "postgres:14",
name: "postgres",
volumes: [
{
hostPath: dataVolume.name, // Reference the volume by name
containerPath: "/var/lib/postgresql/data"
},
{
hostPath: logsVolume.name,
containerPath: "/var/log/postgresql",
readOnly: false
}
],
environment: {
POSTGRES_PASSWORD: "secret"
},
restart: "always",
start: true
});

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Their benefits include:

  1. Data Persistence: Data stored in volumes persists even when containers are stopped or removed
  2. Performance: Better performance than bind mounts, especially on Windows and macOS
  3. Portability: Volumes can be easily backed up, restored, and migrated
  4. Driver Support: Support for various storage backends through volume drivers

When using Docker volumes with Alchemy, it’s a common pattern to:

  1. Create volumes with meaningful names
  2. Assign metadata using labels
  3. Reference volumes in containers by name
  4. Configure volume permissions with the readOnly flag when mounting