In my previous blog posts, I wrote about the built-in container support in the .NET 7 SDK that allows you to create and publish Docker images for your .NET applications without writing any Dockerfile. I also showed how to use the Chiseled Ubuntu base images optimized for .NET and containers.
For this blog post, I will explain how to publish your Docker images using the .NET SDK to GitHub Packages / Container Registry. It is possible to publish to different container registries, such as Docker Hub, GitHub, Azure Container Registry, or your own private registry. I will show you how to publish to GitHub Packages / Container Registry, but the process is similar for other registries.
This blog post will help you learn how to leverage the .NET SDK’s built-in container support to effortlessly distribute and run your .NET applications in the cloud using Docker. Stay tuned!
Prerequisites
To follow along with this blog post, you will need the following
- .NET 7 SDK or later
If you target a remote container registry, only the .NET SDK is required.
To publish your Docker images to a local container registry, you will also need
- Docker Desktop installed
Publish profiles
Like my previous blog post, I will use the publishing profiles to define the Docker image name and tag. I will also use publishing profiles to set the base image and the registry name for the Docker image.
In this blog post, we are focusing on ContainerRegistry
and ContainerImageName
properties.
GitHub Packages / Container Registry publish profile
Where the new image goes after being created depends on the container registry property, ContainerRegistry
, which specifies the target registry. In the case of GitHub Container Registry you need to set ContainerRegistry
to ghcr.io
.
The ContainerImageName
needs to be set as a path using your GitHub username, laurentkempe
, then /
and the name of the image, containerplayground
. ContainerImageTag
specifies the tag of the image, e.g. 1.0.0
. To automate the versioning of your Docker image, you can glance my previous blog in the section “Tagging your image“.
1 | <Project> |
To be able to publish, it is extremely simple with the command specifying the GitHub publish profile.
1 | dotnet publish --os linux --arch x64 -p:PublishProfile=github -c Release |
This publishing profile will create a Docker image named ghcr.io/laurentkempe/containerplayground:1.0.0
.
You will then be able to pull it using the following command:
1 | docker pull ghcr.io/laurentkempe/containerplayground:1.0.0 |
or run it
1 | docker run -it --rm -p 8080:80 ghcr.io/laurentkempe/containerplayground:1.0.0 |
Finally, browse to http://localhost:8080/ to see the “Hello World!” message.
Authentication
To publish to a remote container registry, you need to authenticate. The authentication method depends on the registry you are targeting.
Publishing to GitHub Container Registry uses a Personal Access Token (classic) (PAT) with the read:packages
and write:packages
scopes. You can then use this PAT as your password when authenticating to GitHub Container Registry.
To authenticate to GitHub Container Registry, you can use the docker login
command:
1 | docker login ghcr.io -u <username> -p <token> |
It is a way of interacting with a Docker config file containing rules for authenticating with specific registries.
You can read more about authenticating to container registries for the .NET SDK here. Or, in our case, about the authentication methods for GitHub Container Registry here.
Supported registries
The .NET SDK supports publishing to any registry that supports the Docker Registry HTTP API V2.
Here is a list of supported registries:
- Azure Container Registry
- Amazon Elastic Container Registry
- Google Artifact Registry
- Docker Hub
- GitHub Packages
- GitLab-hosted Container Registry
- Quay.io
Conclusion
In this blog post, I showed you how to use the .NET SDK’s built-in container support to publish your .NET applications to Docker image. I also demonstrated how to use variables in your project file to customize base image, image tag, and registry name for your Docker images. Finally, I showed you how to publish your Docker images to different container registries, such as GitHub, Azure Container Registry or Docker Hub.
References
- Announcing built-in container support for the .NET SDK
- .NET 7 SDK built-in container improvements
- .NET 7 SDK built-in container support and Ubuntu Chiseled
- Containerize an app with dotnet publish - .NET | Microsoft Learn
- Development workflow for Docker apps | Microsoft Learn
- Containerize an app with Docker tutorial - .NET | Microsoft Learn
- Docker Registry HTTP API V2