ContainerResourceBuilderExtensions.WithDockerfileFactory 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
| WithDockerfileFactory<T>(IResourceBuilder<T>, String, Func<DockerfileFactoryContext,String>, String) |
Builds the specified container image from a Dockerfile generated by a synchronous factory function. |
| WithDockerfileFactory<T>(IResourceBuilder<T>, String, Func<DockerfileFactoryContext,Task<String>>, String) |
Builds the specified container image from a Dockerfile generated by an asynchronous factory function. |
WithDockerfileFactory<T>(IResourceBuilder<T>, String, Func<DockerfileFactoryContext,String>, String)
Builds the specified container image from a Dockerfile generated by a synchronous factory function.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithDockerfileFactory<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, string contextPath, Func<Aspire.Hosting.ApplicationModel.DockerfileFactoryContext,string> dockerfileFactory, string? stage = default) where T : Aspire.Hosting.ApplicationModel.ContainerResource;
static member WithDockerfileFactory : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> * string * Func<Aspire.Hosting.ApplicationModel.DockerfileFactoryContext, string> * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)
<Extension()>
Public Function WithDockerfileFactory(Of T As ContainerResource) (builder As IResourceBuilder(Of T), contextPath As String, dockerfileFactory As Func(Of DockerfileFactoryContext, String), Optional stage As String = Nothing) As IResourceBuilder(Of T)
Type Parameters
- T
Type parameter specifying any type derived from ContainerResource.
Parameters
- builder
- IResourceBuilder<T>
The IResourceBuilder<T>.
- contextPath
- String
Path to be used as the context for the container image build.
- dockerfileFactory
- Func<DockerfileFactoryContext,String>
A synchronous function that returns the Dockerfile content as a string.
- stage
- String
The stage representing the image to be published in a multi-stage Dockerfile.
Returns
The IResourceBuilder<T>.
Remarks
When this method is called, an annotation is added to the ContainerResource that specifies the context path and a factory function that generates Dockerfile content. The factory is invoked at build time to produce the Dockerfile, which is then written to a temporary file and used by the orchestrator to build the container image.
The contextPath is relative to the AppHost directory unless it is fully qualified.
The factory function is invoked once during the build process to generate the Dockerfile content. The output is trusted and not validated.
mycontainer with a dynamically generated Dockerfile.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithDockerfileFactory("path/to/context", context =>
{
return "FROM alpine:latest\nRUN echo 'Hello World'";
});
builder.Build().Run();
Applies to
WithDockerfileFactory<T>(IResourceBuilder<T>, String, Func<DockerfileFactoryContext,Task<String>>, String)
Builds the specified container image from a Dockerfile generated by an asynchronous factory function.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithDockerfileFactory<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, string contextPath, Func<Aspire.Hosting.ApplicationModel.DockerfileFactoryContext,System.Threading.Tasks.Task<string>> dockerfileFactory, string? stage = default) where T : Aspire.Hosting.ApplicationModel.ContainerResource;
static member WithDockerfileFactory : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> * string * Func<Aspire.Hosting.ApplicationModel.DockerfileFactoryContext, System.Threading.Tasks.Task<string>> * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.ContainerResource)
<Extension()>
Public Function WithDockerfileFactory(Of T As ContainerResource) (builder As IResourceBuilder(Of T), contextPath As String, dockerfileFactory As Func(Of DockerfileFactoryContext, Task(Of String)), Optional stage As String = Nothing) As IResourceBuilder(Of T)
Type Parameters
- T
Type parameter specifying any type derived from ContainerResource.
Parameters
- builder
- IResourceBuilder<T>
The IResourceBuilder<T>.
- contextPath
- String
Path to be used as the context for the container image build.
- dockerfileFactory
- Func<DockerfileFactoryContext,Task<String>>
An asynchronous function that returns the Dockerfile content as a string.
- stage
- String
The stage representing the image to be published in a multi-stage Dockerfile.
Returns
The IResourceBuilder<T>.
Remarks
When this method is called, an annotation is added to the ContainerResource that specifies the context path and a factory function that generates Dockerfile content. The factory is invoked at build time to produce the Dockerfile, which is then written to a temporary file and used by the orchestrator to build the container image.
The contextPath is relative to the AppHost directory unless it is fully qualified.
The factory function is invoked once during the build process to generate the Dockerfile content. The output is trusted and not validated.
mycontainer with a dynamically generated Dockerfile.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithDockerfileFactory("path/to/context", async context =>
{
var template = await File.ReadAllTextAsync("template.dockerfile", context.CancellationToken);
return template.Replace("{{VERSION}}", "1.0");
});
builder.Build().Run();