# DockerfileBuilder Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [DockerfileBuilder](/reference/api/csharp/aspire.hosting/dockerfilebuilder.md)
- Kind: `Methods`
- Members: `5`

Builder for creating Dockerfiles programmatically.

## Arg(string)

> **Experimental:** ASPIREDOCKERFILEBUILDER001 - [Learn more](/diagnostics/aspiredockerfilebuilder001/)

- Name: `Arg(string)`
- Returns: [DockerfileBuilder](/reference/api/csharp/aspire.hosting/dockerfilebuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileBuilder.cs#L42-L45)

Adds a global ARG statement to define a build-time variable before any stages.

```csharp
public class DockerfileBuilder
{
    public DockerfileBuilder Arg(
        string name)
    {
        // ...
    }
}
```

## Parameters

- `name` (`string`)
  The name of the build argument.

## Returns

[DockerfileBuilder](/reference/api/csharp/aspire.hosting/dockerfilebuilder.md) -- The current DockerfileBuilder instance for method chaining.

## Remarks

Global ARG statements appear before the first FROM statement and can be used to parameterize the base image selection.

## Arg(string, string)

> **Experimental:** ASPIREDOCKERFILEBUILDER001 - [Learn more](/diagnostics/aspiredockerfilebuilder001/)

- Name: `Arg(string, string)`
- Returns: [DockerfileBuilder](/reference/api/csharp/aspire.hosting/dockerfilebuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileBuilder.cs#L61-L65)

Adds a global ARG statement to define a build-time variable with a default value before any stages.

```csharp
public class DockerfileBuilder
{
    public DockerfileBuilder Arg(
        string name,
        string defaultValue)
    {
        // ...
    }
}
```

## Parameters

- `name` (`string`)
  The name of the build argument.
- `defaultValue` (`string`)
  The default value for the build argument.

## Returns

[DockerfileBuilder](/reference/api/csharp/aspire.hosting/dockerfilebuilder.md) -- The current DockerfileBuilder instance for method chaining.

## Remarks

Global ARG statements appear before the first FROM statement and can be used to parameterize the base image selection.

## From(string, string)

> **Experimental:** ASPIREDOCKERFILEBUILDER001 - [Learn more](/diagnostics/aspiredockerfilebuilder001/)

- Name: `From(string, string)`
- Returns: [DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileBuilder.cs#L77-L82)

Adds a FROM statement to start a new named stage.

```csharp
public class DockerfileBuilder
{
    public DockerfileStage From(
        string image,
        string stageName)
    {
        // ...
    }
}
```

## Parameters

- `image` (`string`)
  The image reference (e.g., 'node:18' or 'alpine:latest').
- `stageName` (`string`)
  The stage name for multi-stage builds.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- A stage builder for the new stage.

## From(string)

> **Experimental:** ASPIREDOCKERFILEBUILDER001 - [Learn more](/diagnostics/aspiredockerfilebuilder001/)

- Name: `From(string)`
- Returns: [DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileBuilder.cs#L93-L97)

Adds a FROM statement to start a new stage.

```csharp
public class DockerfileBuilder
{
    public DockerfileStage From(
        string image)
    {
        // ...
    }
}
```

## Parameters

- `image` (`string`)
  The image reference (e.g., 'node:18' or 'alpine:latest').

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- A stage builder for the new stage.

## WriteAsync(StreamWriter, CancellationToken)

- Name: `WriteAsync(StreamWriter, CancellationToken)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileBuilder.cs#L108-L134)

Writes the Dockerfile content to the specified `IO.StreamWriter`.

```csharp
public class DockerfileBuilder
{
    public Task WriteAsync(
        StreamWriter writer,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `writer` (`StreamWriter`)
  The `IO.StreamWriter` to write to.
- `cancellationToken` (`CancellationToken`) `optional`
  A cancellation token.

## Returns

`Task` -- A task representing the asynchronous write operation.
