DEV Community

Cover image for .NET 7 + Docker
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

2

.NET 7 + Docker

My previous post was about .NET 6 and Docker. Let's move on to .NET 7 and learn together.

Preparation

  1. Docker
  2. .NET 7 SDK

Starter Project

You can try cloning from the net6.0 branch from this repository.

Our Changes from Previous Repository

  1. Add package Microsoft.NET.Build.Containers. You can use this command: dotnet add DockerNetExample package Microsoft.NET.Build.Containers.
  2. Update DockerNetExample/DockerNetExample.csproj. We update the TargetFramework to net7.0 and add ContainerImageName.

    <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>
    
  3. 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.

    docker publish

  4. Use this command (docker image ls) to check the docker image.

    docker image

  5. Let's run our container. Using this command: docker run --name docker-net-example -p 8080:80 -d docker-net-example:1.0.0

    run container

  6. Check our container. Using this command: docker ps.

    docker ps

  7. Test our Web API. You can navigate to localhost:8080 using your favorite browser, for example, I use Firefox.

    Test Image

Thank you

If you have any feedback, feel free to share it with me in this post or raise an issue in the repository.

Thanks gif

Repository




You also can check this Pull Request about the changes.

References

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay