# .NET Containers advancements in .NET 8

This is a summary of [.NET Containers advancements in .NET 8](https://www.youtube.com/watch?v=scIAwLrruMY&list=PLdo4fOcmZ0oULyHSPBx-tQzePOYlhvrAU&index=14&t=10s) on .Net Conf 2023.

It is requied from communitity for .Net Container Images to be

* Small (registry pull faster)
    
* Secure (non-root; no shell or package manager)
    
* Compliant (minimal dependencies, easy audit)
    
* Composable (e.g. add locallication when needed)
    
* Compatible (glibc vs musl libc)
    
* Supported (part of a support contract / lifecycle)
    

## Ubuntu Chiseled .NET images

Ubuntu Chiseled .NET images are a type of "distroless" container image that contain only the minimal set of packages .NET needs, with everything else removed. These images offer dramatically smaller deployment sizes and attack surface by including only the minimal set of packages required to run .NET applications.

Please see the [Ubuntu Chiseled + .NET](https://github.com/dotnet/dotnet-docker/blob/main/documentation/ubuntu-chiseled.md) documentation page for more info.

## Container Size Improvements

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701976490720/42d677c7-0ee3-4b27-8e90-1478dee6315c.png align="center")

## .Net Containers in General

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701976578218/f5073c06-a2bb-494d-9f30-0481f889102a.png align="center")

## How to Publish Your Application?

First add the below package from Nuget.

```markdown
dotnet add package Microsoft.NET.Build.Containers --version 8.0.100
```

Then, publish your container using .NET CLI. I am using -`r linux-x64`, because I am on Windows.

```markdown
dotnet publish -t:PublishContainer -r linux-x64
```

Then your application is published as a container to your local registry without any Dockerfile as below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701977850910/5efd18a3-3897-4748-9311-c7f3e7f934a4.png align="center")

However, the size of the image will be big. We need to shrink the size of the container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701978004011/8bc08f72-65b6-47f8-8120-4942bcd202c2.png align="center")

To shrink, add the `PublishTrimmed and ContainerFamily` to your csproj file.

```xml
<PropertyGroup>
....
    <PublishTrimmed>true</PublishTrimmed>
    <ContainerFamily>jammy-chiseled</ContainerFamily>
....
<PropertyGroup>
```

Then publish your project again.

```markdown
dotnet publish -t:PublishContainer -r linux-x64 -p ContainerImageTag=chiseled
```

You will see that the size is reduced now.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701978956025/5a880caf-a0fa-48af-841d-d79fa8c2697a.png align="center")

You can also publish your app as [Native AOT](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7%2Cwindows). Add the below to your csproj file.

```xml
<PublishAot>True</PublishAot>
```

```markdown
dotnet publish -t:PublishContainer -r linux-x64 -p ContainerImageTag=aot
```

You can also specify the base image for the container.

```xml
<ContainerBaseImage>mcr.microsoft.com/dotnet/nightly/runtime:8.0</ContainerBaseImage>
```

I believe that creating containers without Dockerfile is a life-saving feature for developers. You can check the [documentation](https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0) for more information.
