# ContainerResource.WithLifetime

- 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`

Sets the lifetime behavior of the container resource.

## Remarks

Prefer `WithPersistentLifetime` or
`WithSessionLifetime` for new code.
Marking a container resource to have a `Persistent` lifetime.
```
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithPersistentLifetime();
builder.Build().Run();
```

## Definition

```go
type ContainerResource interface {
    // ... omitted for brevity
    WithLifetime(lifetime ContainerLifetime) ContainerResource
}
```

## Signature

```go
WithLifetime(lifetime ContainerLifetime) ContainerResource
```

## Parameters

- `lifetime` ([`ContainerLifetime`](/reference/api/go/aspire.hosting/containerlifetime.md)) -- The lifetime behavior of the container resource. The default behavior is `Session`.

## 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`
