# DockerfileStage Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md)
- Kind: `Methods`
- Members: `17`

Represents a stage within a multi-stage Dockerfile.

## Arg(string)

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

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

Adds an ARG statement to define a build-time variable.

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

## Parameters

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

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Arg(string, string)

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

- Name: `Arg(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/DockerfileStage.cs#L62-L65)

Adds an ARG statement to define a build-time variable with a default value.

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

## Parameters

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

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Cmd(string[])

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

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

Adds a CMD statement to set the default command.

```csharp
public class DockerfileStage
{
    public DockerfileStage Cmd(
        string[] command)
    {
        // ...
    }
}
```

## Parameters

- `command` (`string[]`)
  The command to execute.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Comment(string)

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

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

Adds a comment to the Dockerfile. Multi-line comments are supported.

```csharp
public class DockerfileStage
{
    public DockerfileStage Comment(
        string comment)
    {
        // ...
    }
}
```

## Parameters

- `comment` (`string`)
  The comment text. Can be single-line or multi-line.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Remarks

When a multi-line comment is provided, each line will be prefixed with '#'. Empty lines in multi-line comments are preserved as comment lines.

## Copy(string, string)

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

- Name: `Copy(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/DockerfileStage.cs#L103-L106)

Adds a COPY statement to copy files from the build context.

```csharp
public class DockerfileStage
{
    public DockerfileStage Copy(
        string source,
        string destination)
    {
        // ...
    }
}
```

## Parameters

- `source` (`string`)
  The source path or pattern.
- `destination` (`string`)
  The destination path.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Copy(string, string, string)

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

- Name: `Copy(string, 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/DockerfileStage.cs#L136-L140)

Adds a COPY statement to copy files with ownership change.

```csharp
public class DockerfileStage
{
    public DockerfileStage Copy(
        string source,
        string destination,
        string chown)
    {
        // ...
    }
}
```

## Parameters

- `source` (`string`)
  The source path.
- `destination` (`string`)
  The destination path.
- `chown` (`string`)
  The ownership specification (e.g., "user:group").

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## CopyFrom(string, string, string)

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

- Name: `CopyFrom(string, 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/DockerfileStage.cs#L119-L123)

Adds a COPY statement to copy files from another stage.

```csharp
public class DockerfileStage
{
    public DockerfileStage CopyFrom(
        string from,
        string source,
        string destination)
    {
        // ...
    }
}
```

## Parameters

- `from` (`string`)
  The source stage or image name.
- `source` (`string`)
  The source path in the stage.
- `destination` (`string`)
  The destination path.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## CopyFrom(string, string, string, string)

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

- Name: `CopyFrom(string, string, 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/DockerfileStage.cs#L154-L159)

Adds a COPY statement to copy files from another stage with ownership change.

```csharp
public class DockerfileStage
{
    public DockerfileStage CopyFrom(
        string stage,
        string source,
        string destination,
        string chown)
    {
        // ...
    }
}
```

## Parameters

- `stage` (`string`)
  The source stage name.
- `source` (`string`)
  The source path in the stage.
- `destination` (`string`)
  The destination path.
- `chown` (`string`)
  The ownership specification (e.g., "user:group").

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## EmptyLine

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

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

Adds an empty line to the Dockerfile for better readability.

```csharp
public class DockerfileStage
{
    public DockerfileStage EmptyLine()
    {
        // ...
    }
}
```

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Entrypoint(string[])

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

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

Adds an ENTRYPOINT statement to set the container entrypoint.

```csharp
public class DockerfileStage
{
    public DockerfileStage Entrypoint(
        string[] command)
    {
        // ...
    }
}
```

## Parameters

- `command` (`string[]`)
  The entrypoint command to execute.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Env(string, string)

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

- Name: `Env(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/DockerfileStage.cs#L171-L174)

Adds an ENV statement to set an environment variable.

```csharp
public class DockerfileStage
{
    public DockerfileStage Env(
        string name,
        string value)
    {
        // ...
    }
}
```

## Parameters

- `name` (`string`)
  The environment variable name.
- `value` (`string`)
  The environment variable value.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Expose(int)

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

- Name: `Expose(int)`
- Returns: [DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileStage.cs#L185-L187)

Adds an EXPOSE statement to expose a port.

```csharp
public class DockerfileStage
{
    public DockerfileStage Expose(
        int port)
    {
        // ...
    }
}
```

## Parameters

- `port` (`int`)
  The port number to expose.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## Run(string)

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

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

Adds a RUN statement to execute a command.

```csharp
public class DockerfileStage
{
    public DockerfileStage Run(
        string command)
    {
        // ...
    }
}
```

## Parameters

- `command` (`string`)
  The command to execute.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## RunWithMounts(string, string[])

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

- Name: `RunWithMounts(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/DockerfileStage.cs#L233-L236)

Adds a RUN statement with mount options for BuildKit.

```csharp
public class DockerfileStage
{
    public DockerfileStage RunWithMounts(
        string command,
        params string[] mounts)
    {
        // ...
    }
}
```

## Parameters

- `command` (`string`)
  The command to execute.
- `mounts` (`string[]`)
  The mount options (e.g., "type=cache,target=/root/.cache").

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## User(string)

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

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

Adds a USER statement to set the user for subsequent commands.

```csharp
public class DockerfileStage
{
    public DockerfileStage User(
        string user)
    {
        // ...
    }
}
```

## Parameters

- `user` (`string`)
  The user name or UID.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## WorkDir(string)

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

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

Adds a WORKDIR statement to set the working directory.

```csharp
public class DockerfileStage
{
    public DockerfileStage WorkDir(
        string path)
    {
        // ...
    }
}
```

## Parameters

- `path` (`string`)
  The working directory path.

## Returns

[DockerfileStage](/reference/api/csharp/aspire.hosting/dockerfilestage.md) -- The current stage.

## WriteStatementAsync(StreamWriter, CancellationToken)

- Name: `WriteStatementAsync(StreamWriter, CancellationToken)`
- Modifiers: `override`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileStage.cs#L283-L287)

Writes the statement to the specified writer.

```csharp
public class DockerfileStage
{
    public override Task WriteStatementAsync(
        StreamWriter writer,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

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

## Returns

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