# ContainerResource.WithDockerfileFactory

- Module: [Aspire.Hosting](/reference/api/go/aspire.hosting.md)
- Defined on: [ContainerResource](/reference/api/go/aspire.hosting/containerresource.md)
- Version: `17.0.0`
- Kind: `method`

Builds the specified container image from a Dockerfile generated by an asynchronous factory function.

## 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.
Creates a container called `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();
```

## Definition

```go
type ContainerResource interface {
    // ... omitted for brevity
    WithDockerfileFactory(
        contextPath string,
        dockerfileFactory func(arg DockerfileFactoryContext) string,
        options ...*WithDockerfileFactoryOptions,
    ) ContainerResource
}
```

## Signature

```go
WithDockerfileFactory(
    contextPath string,
    dockerfileFactory func(arg DockerfileFactoryContext) string,
    options ...*WithDockerfileFactoryOptions,
) ContainerResource
```

## Parameters

- `contextPath` (`string`) -- Path to be used as the context for the container image build.
- `dockerfileFactory` (`func(arg DockerfileFactoryContext) string`) -- An asynchronous function that returns the Dockerfile content as a string.
- `options` (`...*WithDockerfileFactoryOptions`) -- optional settings, see [Options](#options)

## Options

Optional parameters are passed in a trailing `options ...*WithDockerfileFactoryOptions` argument.

- `Stage` (`*string`) -- The stage representing the image to be published in a multi-stage Dockerfile.

## Returns

[`ContainerResource`](/reference/api/go/aspire.hosting/containerresource.md) -- The resource builder.

Returns a handle; failures surface later via `Err()`.

## Defined on

- [ContainerResource](/reference/api/go/aspire.hosting/containerresource.md) -- `handle`
