My previous post was about .NET 6 and Docker. Let's move on to .NET 7 and learn together.
Preparation
Starter Project
You can try cloning from the net6.0 branch from this repository.
Our Changes from Previous Repository
- Add package
Microsoft.NET.Build.Containers
. You can use this command:dotnet add DockerNetExample package Microsoft.NET.Build.Containers
. -
Update
DockerNetExample/DockerNetExample.csproj
. We update theTargetFramework
tonet7.0
and addContainerImageName
.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> +<TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <ContainerImageName>docker-net-example</ContainerImageName> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Build.Containers" Version="0.2.7" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> </ItemGroup> </Project>
-
That's it! Our project is ready to create a docker image using
dotnet publish
. You can use this command:dotnet publish --os linux --arch x64 /t:PublishContainer -c Release
. -
Use this command (
docker image ls
) to check the docker image. -
Let's run our container. Using this command:
docker run --name docker-net-example -p 8080:80 -d docker-net-example:1.0.0
-
Check our container. Using this command:
docker ps
. -
Test our Web API. You can navigate to
localhost:8080
using your favorite browser, for example, I use Firefox.
Thank you
If you have any feedback, feel free to share it with me in this post or raise an issue in the repository.
Repository
bervProject / docker-net-example
.NET Dockerfile
Docker .NET Example
Blog Post
.NET 6
Please check net6.0 branch.
.NET 7
Please check net7.0 branch.
License
MIT
You also can check this Pull Request about the changes.
Top comments (0)