Ce contenu n’est pas encore disponible dans votre langue.
In Aspire, containers follow a typical lifecycle where they’re created when the AppHost starts and destroyed when it stops. However, you can specify that you want to use persistent containers, which deviate from this standard lifecycle. Persistent containers are created and started by the Aspire orchestrator but aren’t destroyed when the AppHost stops, allowing them to persist between runs.
This feature is particularly beneficial for containers that have long startup times, such as databases, as it eliminates the need to wait for these services to initialize on every AppHost restart.
In the preceding example, the PostgreSQL container is configured to persist between AppHost runs, and WithDataVolume() ensures database data is stored in a named volume that survives container recreation. The inventory project references the database as normal.
Persistent containers are automatically recreated when the AppHost detects meaningful configuration changes. Aspire tracks a hash of the configuration used to create each container and compares it to the current configuration on subsequent runs. If the configuration differs, the container is recreated with the new settings.
This mechanism ensures that persistent containers stay synchronized with your AppHost configuration without requiring manual intervention.
By default, persistent containers use a naming pattern that combines:
The service name you specify in your AppHost.
A postfix based on a hash of the AppHost project path.
This naming scheme ensures that persistent containers are unique to each AppHost project, preventing conflicts when multiple Aspire projects use the same service names.
For example, if you have a service named "postgres" in an AppHost project located at /path/to/MyApp.AppHost, the container name might be postgres-abc123def where abc123def is derived from the project path hash.
When you specify a custom container name, Aspire first checks if a container with that name already exists. If a container with that name exists and was previously created by Aspire, it follows the normal persistent container behavior and can be automatically recreated if the configuration changes. If a container with that name exists but wasn’t created by Aspire, it won’t be managed or recreated by the AppHost. If no container with the custom name exists, Aspire creates a new one.
ContainerLifetime.Persistent and WithDataVolume() serve different purposes and are often used together. The following table summarizes the behavior of each combination:
Configuration
Container behavior
Data behavior
Neither (default)
Created on start, destroyed on stop
Lost every time the AppHost stops
WithLifetime(ContainerLifetime.Persistent) only
Stays running between AppHost runs
Survives AppHost restarts, but lost if the container is recreated (config change, pruning, image update)
WithDataVolume() only
Created on start, destroyed on stop
Persists in a named volume—survives container recreation
Both (recommended for databases)
Stays running between AppHost runs
Persists in a named volume—survives container recreation
For databases and other stateful services, use both APIs together so you get fast startup (the container stays running) and data safety (a volume protects data even if the container is recreated):
For caches or other ephemeral state, WithLifetime(ContainerLifetime.Persistent) alone may be sufficient because losing data on container recreation is acceptable.