Modal volumes explained
Quick answer
In
Modal, volumes are persistent storage units that allow you to save and share data across function invocations and containers. They are mounted as directories inside your container, enabling durable file storage beyond ephemeral runtime environments.PREREQUISITES
Python 3.8+Modal account and CLI installedpip install modal
Setup
Install the modal Python package and log in to your Modal account using the CLI. Define a volume in your Modal app to enable persistent storage.
pip install modal
modal login output
Collecting modal Downloading modal-1.0.0-py3-none-any.whl (50 kB) Installing collected packages: modal Successfully installed modal-1.0.0 Logging in to Modal... Successfully logged in as user@example.com
Step by step
This example shows how to create a Modal volume, write a file to it, and read the file back in a function. The volume persists data across runs.
import modal
# Define a volume named 'my-volume'
volume = modal.Volume("my-volume")
app = modal.App()
@modal.function(
volumes={"/data": volume}
)
def write_and_read():
path = "/data/example.txt"
# Write to the volume
with open(path, "w") as f:
f.write("Hello from Modal volume!")
# Read from the volume
with open(path, "r") as f:
return f.read()
if __name__ == "__main__":
with app.run():
result = write_and_read()
print("Read from volume:", result) output
Read from volume: Hello from Modal volume!
Common variations
You can mount multiple volumes, use volumes with different paths, or share volumes between multiple functions. Volumes can also be used with modal.Stub for local development.
import modal
volume1 = modal.Volume("volume1")
volume2 = modal.Volume("volume2")
app = modal.App()
@modal.function(
volumes={"/data1": volume1, "/data2": volume2}
)
def multi_volume_access():
with open("/data1/file1.txt", "w") as f:
f.write("Data in volume1")
with open("/data2/file2.txt", "w") as f:
f.write("Data in volume2")
with open("/data1/file1.txt", "r") as f1, open("/data2/file2.txt", "r") as f2:
return f1.read(), f2.read()
if __name__ == "__main__":
with app.run():
data1, data2 = multi_volume_access()
print(f"Volume1: {data1}")
print(f"Volume2: {data2}") output
Volume1: Data in volume1 Volume2: Data in volume2
Troubleshooting
- If you get a
VolumeNotFoundError, ensure the volume name is created in your Modal dashboard or CLI before running your app. - Check that the volume mount path inside the container matches the path used in your code.
- Remember volumes persist data beyond container lifetimes, but deleting a volume from Modal dashboard will remove stored data.
Key Takeaways
- Use
modal.Volumeto create persistent storage shared across function runs. - Mount volumes inside your function with the
volumesparameter to access files. - Volumes persist data beyond ephemeral container lifetimes, enabling durable state.
- Always create volumes in Modal dashboard or CLI before referencing them in code.
- Multiple volumes can be mounted simultaneously at different paths for complex apps.