1. Why do this?
- .NET 8 can build a Docker image for you.
- One command creates and tags the image.
- You can push the image without Docker on the build server.
2. What you need
- .NET SDK 8.0.200+ — Build and publish the image.
- Docker or Podman (optional) — Run the image on your PC.
- GitHub or Azure DevOps — Run CI/CD examples.
3. Build and run on your PC
Open a terminal and run:
dotnet new console -o MyApp
cd MyApp
dotnet publish -t:PublishContainer -p:EnableSdkContainerSupport=true
docker run --rm myapp:latest
The SDK picks the base image, tags it myapp:latest
, and stores it locally.
4. Change image settings
- Use Alpine Linux
dotnet publish --os linux-musl -t:PublishContainer
- Use Ubuntu 22.04
dotnet publish -t:PublishContainer -p:ContainerFamily=jammy
- Tiny chiseled image
dotnet publish -t:PublishContainer -p:ContainerFamily=jammy-chiseled-extra
- Set repo & tag
dotnet publish -t:PublishContainer -p:ContainerRepository=ghcr.io/user/app -p:ContainerImageTags=v1
- Push in build
dotnet publish -t:PublishContainer -p:ContainerRegistry=ghcr.io -p:ContainerPush=true
5. GitHub Actions
Save this as .github/workflows/docker.yml
:
name: Build and push image
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Publish image
run: |
dotnet publish -c Release -t:PublishContainer \
-p:ContainerRepository=ghcr.io/${{ github.repository }} \
-p:ContainerImageTags=${{ github.sha }} \
-p:ContainerPush=true
6. Azure DevOps
Save this as azure-pipelines.yml
:
trigger:
- main
pool:
vmImage: ubuntu-latest
variables:
acrName: myacr.azurecr.io
imageRepo: demo/myapp
steps:
- checkout: self
- task: UseDotNet@2
inputs:
packageType: sdk
version: 8.0.x
- script: |
dotnet publish -c Release -t:PublishContainer \
-p:ContainerRegistry=$(acrName) \
-p:ContainerRepository=$(imageRepo) \
-p:ContainerImageTags=$(Build.BuildNumber) \
-p:ContainerPush=true
displayName: Build and push
7. Quick fixes
- Hidden folders like
.well-known
are missing → Rename the folder or add a custom MSBuild target. - Need
apt
packages → Make your own base image and setContainerBaseImage
. - Private NuGet feed → Use the same NuGet auth you use in normal builds.
8. Remember
- No Dockerfile for most apps.
- Images are smaller and run as non‑root.
- One command can build and push.
Top comments (0)