ContainerResourceBuilderExtensions.WithContainerFiles Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
WithContainerFiles<T>(IResourceBuilder<T>, String, IEnumerable<ContainerFileSystemItem>, Nullable<Int32>, Nullable<Int32>, Nullable<UnixFileMode>) |
Creates or updates files and/or folders at the destination path in the container. |
WithContainerFiles<T>(IResourceBuilder<T>, String, Func<ContainerFileSystemCallbackContext, CancellationToken,Task<IEnumerable<ContainerFileSystemItem>>>, Nullable<Int32>, Nullable<Int32>, Nullable<UnixFileMode>) |
Creates or updates files and/or folders at the destination path in the container. Receives a callback that will be invoked when the container is started to allow the files to be created based on other resources in the application model. |
WithContainerFiles<T>(IResourceBuilder<T>, String, IEnumerable<ContainerFileSystemItem>, Nullable<Int32>, Nullable<Int32>, Nullable<UnixFileMode>)
Creates or updates files and/or folders at the destination path in the container.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithContainerFiles<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, string destinationPath, System.Collections.Generic.IEnumerable<Aspire.Hosting.ApplicationModel.ContainerFileSystemItem> entries, int? defaultOwner = default, int? defaultGroup = default, System.IO.UnixFileMode? umask = default) where T : Aspire.Hosting.ApplicationModel.ContainerResource;
static member WithContainerFiles : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> * string * seq<Aspire.Hosting.ApplicationModel.ContainerFileSystemItem> * Nullable<int> * Nullable<int> * Nullable<System.IO.UnixFileMode> -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)
<Extension()>
Public Function WithContainerFiles(Of T As ContainerResource) (builder As IResourceBuilder(Of T), destinationPath As String, entries As IEnumerable(Of ContainerFileSystemItem), Optional defaultOwner As Nullable(Of Integer) = Nothing, Optional defaultGroup As Nullable(Of Integer) = Nothing, Optional umask As Nullable(Of UnixFileMode) = Nothing) As IResourceBuilder(Of T)
Type Parameters
- T
The type of container resource.
Parameters
- builder
- IResourceBuilder<T>
The resource builder for the container resource.
- destinationPath
- String
The destination (absolute) path in the container.
- entries
- IEnumerable<ContainerFileSystemItem>
The file system entries to create.
The default owner UID for the created or updated file system. Defaults to 0 for root if not set.
The default group ID for the created or updated file system. Defaults to 0 for root if not set.
- umask
- Nullable<UnixFileMode>
The umask UnixFileMode permissions to exclude from the default file and folder permissions. This takes away (rather than granting) default permissions to files and folders without an explicit mode permission set.
Returns
The IResourceBuilder<T>.
Examples
Create a directory called custom-entry
in the container's file system at the path /usr/data
and create a file called entrypoint.sh
inside it with the content echo hello world
.
The default permissions for these files will be for the user or group to be able to read and write to the files, but not execute them. entrypoint.sh will be created with execution permissions for the owner.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithContainerFiles("/usr/data", [
new ContainerDirectory
{
Name = "custom-entry",
Entries = [
new ContainerFile
{
Name = "entrypoint.sh",
Contents = "echo hello world",
Mode = UnixFileMode.UserExecute | UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.GroupWrite,
},
],
},
],
defaultOwner: 1000);
Remarks
For containers with a Persistent lifetime, changing the contents of create file entries will result in the container being recreated. Make sure any data being written to containers is idempotent for a given app model configuration. Specifically, be careful not to include any data that will be unique on a per-run basis.
custom-entry
in the container's file system at the path /usr/data
and create a file called entrypoint.sh
inside it with the content echo hello world
. The default permissions for these files will be for the user or group to be able to read and write to the files, but not execute them. entrypoint.sh will be created with execution permissions for the owner.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithContainerFiles("/usr/data", [
new ContainerDirectory
{
Name = "custom-entry",
Entries = [
new ContainerFile
{
Name = "entrypoint.sh",
Contents = "echo hello world",
Mode = UnixFileMode.UserExecute | UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.GroupWrite,
},
],
},
],
defaultOwner: 1000);
Applies to
WithContainerFiles<T>(IResourceBuilder<T>, String, Func<ContainerFileSystemCallbackContext, CancellationToken,Task<IEnumerable<ContainerFileSystemItem>>>, Nullable<Int32>, Nullable<Int32>, Nullable<UnixFileMode>)
Creates or updates files and/or folders at the destination path in the container. Receives a callback that will be invoked when the container is started to allow the files to be created based on other resources in the application model.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithContainerFiles<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, string destinationPath, Func<Aspire.Hosting.ApplicationModel.ContainerFileSystemCallbackContext,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Aspire.Hosting.ApplicationModel.ContainerFileSystemItem>>> callback, int? defaultOwner = default, int? defaultGroup = default, System.IO.UnixFileMode? umask = default) where T : Aspire.Hosting.ApplicationModel.ContainerResource;
static member WithContainerFiles : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> * string * Func<Aspire.Hosting.ApplicationModel.ContainerFileSystemCallbackContext, System.Threading.CancellationToken, System.Threading.Tasks.Task<seq<Aspire.Hosting.ApplicationModel.ContainerFileSystemItem>>> * Nullable<int> * Nullable<int> * Nullable<System.IO.UnixFileMode> -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)
<Extension()>
Public Function WithContainerFiles(Of T As ContainerResource) (builder As IResourceBuilder(Of T), destinationPath As String, callback As Func(Of ContainerFileSystemCallbackContext, CancellationToken, Task(Of IEnumerable(Of ContainerFileSystemItem))), Optional defaultOwner As Nullable(Of Integer) = Nothing, Optional defaultGroup As Nullable(Of Integer) = Nothing, Optional umask As Nullable(Of UnixFileMode) = Nothing) As IResourceBuilder(Of T)
Type Parameters
- T
The type of container resource.
Parameters
- builder
- IResourceBuilder<T>
The resource builder for the container resource.
- destinationPath
- String
The destination (absolute) path in the container.
- callback
- Func<ContainerFileSystemCallbackContext,CancellationToken,Task<IEnumerable<ContainerFileSystemItem>>>
The callback that will be invoked when the resource is being created.
The default owner UID for the created or updated file system. Defaults to 0 for root if not set.
The default group ID for the created or updated file system. Defaults to 0 for root if not set.
- umask
- Nullable<UnixFileMode>
The umask UnixFileMode permissions to exclude from the default file and folder permissions. This takes away (rather than granting) default permissions to files and folders without an explicit mode permission set.
Returns
The IResourceBuilder<T>.
Examples
Create a configuration file for every Postgres instance in the application model.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithContainerFiles("/", (context, cancellationToken) =>
{
var appModel = context.ServiceProvider.GetRequiredService{DistributedApplicationModel}();
var postgresInstances = appModel.Resources.OfType{PostgresDatabaseResource}();
return [
new ContainerDirectory
{
Name = ".pgweb",
Entries = [
new ContainerDirectory
{
Name = "bookmarks",
Entries = postgresInstances.Select(instance =>
new ContainerFile
{
Name = $"{instance.Name}.toml",
Contents = instance.ToPgWebBookmark(),
Owner = defaultOwner,
Group = defaultGroup,
}),
},
],
},
];
});
Remarks
For containers with a Persistent lifetime, changing the contents of create file entries will result in the container being recreated. Make sure any data being written to containers is idempotent for a given app model configuration. Specifically, be careful not to include any data that will be unique on a per-run basis.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithContainerFiles("/", (context, cancellationToken) =>
{
var appModel = context.ServiceProvider.GetRequiredService{DistributedApplicationModel}();
var postgresInstances = appModel.Resources.OfType{PostgresDatabaseResource}();
return [
new ContainerDirectory
{
Name = ".pgweb",
Entries = [
new ContainerDirectory
{
Name = "bookmarks",
Entries = postgresInstances.Select(instance =>
new ContainerFile
{
Name = $"{instance.Name}.toml",
Contents = instance.ToPgWebBookmark(),
Owner = defaultOwner,
Group = defaultGroup,
}),
},
],
},
];
});