Skip to content

Commit 1ec461e

Browse files
authored
Merge pull request #327 from alexwolfmsft/persist-data-volume-mount
volume mount
2 parents 01d6c0f + f595bb9 commit 1ec461e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Persist data with .NET Aspire using volume mounts
3+
description: Learn about .NET Aspire volume configurations.
4+
ms.date: 12/08/2023
5+
ms.topic: how-to
6+
---
7+
8+
# Persist .NET Aspire app data using volumes
9+
10+
In this article, you learn how to configure .NET Aspire apps to persist data across app launches using volumes. A continuous set of data during local development is useful in many scenarios. Various .NET Aspire resource container types are able to leverage volume storage, such as PostreSQL, Redis and Azure Storage.
11+
12+
## When to use volumes
13+
14+
By default, every time you start and stop a .NET Aspire app, the app also creates and destroys the app resource containers. This setup creates problems when you want to persist data in a database or storage services between app launches for testing or debugging. For example, you may want to handle the following scenarios:
15+
16+
- Work with a continuous set of data in a database during an extended development session.
17+
- Test or debug a changing set of files in an Azure Blob Storage emulator.
18+
- Maintain cached data or messages in a Redis instance across app launches.
19+
20+
These goals can all be accomplished using volumes. With volumes, you decide which services retain data between launches of your .NET Aspire app.
21+
22+
## Understand volumes
23+
24+
Volumes are the recommended way to persist data generated by containers and supported on both Windows and Linux. Volumes can store data from multiple containers at a time, offer high performance and are easy to back up or migrate. With .NET Aspire, you configure a volume for each resource container using the <xref:Aspire.Hosting.ContainerResourceBuilderExtensions.WithVolumeMount%2A?displayProperty=nameWithType> method, which accepts three parameters:
25+
26+
- **Source**: The source path of the volume, which is the physical location on the host.
27+
- **Target**: The target path in the container of the data you want to persist.
28+
- **VolumeMountType**: There are two types of volume mounts available:
29+
- **Named mounts**: Storage managed by docker that your containers can use to persist data.
30+
- **Bind mounts**: Files mounted from your host machine onto your container.
31+
32+
Consider the following volume configuration code from a _Program.cs_ file in a sample AppHost project:
33+
34+
```csharp
35+
var sql = builder.AddSqlServerContainer("sql", sqlpassword)
36+
.WithVolumeMount("VolumeMount.sqlserver.data", "/var/opt/mssql", VolumeMountType.Named)
37+
.AddDatabase("sqldb");
38+
```
39+
40+
In this example:
41+
42+
- `VolumeMount.sqlserver.data` sets where the volume will be stored on the host.
43+
- `/var/opt/mssql` sets the path to the database files in the container.
44+
- <xref:Aspire.Hosting.ApplicationModel.VolumeMountType.Named?displayProperty=nameWithType> sets a named volume that will persist across container life cycles.
45+
46+
## Create a persistent password
47+
48+
Named volumes require a consistent password between app launches. Run the following command in your project directory to set a local password in your .NET user secrets:
49+
50+
```dotnetcli
51+
dotnet user-secrets set samplepassword <password>
52+
```
53+
54+
Retrieve this password in your app using the following code:
55+
56+
```csharp
57+
// Retrieve the password you created from user secrets
58+
var password = builder.Configuration["samplepassword"];
59+
```
60+
61+
## Configure volumes using the AppHost
62+
63+
Volumes are configured in the _Program.cs_ file in the **.AppHost** project of your .NET Aspire apps. The following code demonstrates how to configure a volume for various .NET Aspire resources:
64+
65+
:::code source="~/aspire-samples/samples/VolumeMount/VolumeMount.AppHost/Program.cs":::
66+
67+
## Next steps
68+
69+
You can apply the volume concepts in the preceding code to a variety of services, including seeding a database with data that will persist across app launches. Try combining these techniques with the resource implementations demonstrated in the following tutorials:
70+
71+
- [Tutorial: Connect an ASP.NET Core app to .NET Aspire storage components](../storage/azure-storage-components.md)
72+
- [Tutorial: Connect an ASP.NET Core app to SQL Server using .NET Aspire and Entity Framework Core](../database/sql-server-components.md)
73+
- [.NET Aspire orchestration overview](../fundamentals/app-host-overview.md)

docs/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ items:
4040
href: service-discovery/overview.md
4141
- name: Service defaults
4242
href: fundamentals/service-defaults.md
43+
- name: Persist data using volumes
44+
href: fundamentals/persist-data-volumes.md
4345
- name: Health checks
4446
href: fundamentals/health-checks.md
4547
- name: Telemetry

0 commit comments

Comments
 (0)