Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 30, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 30, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 30, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 30, 2025

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025

      The Witcher 3: Wild Hunt reaches 60 million copies sold as work continues on The Witcher 4

      May 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      How Remix is shaking things up

      May 30, 2025
      Recent

      How Remix is shaking things up

      May 30, 2025

      Perficient at Kscope25: Let’s Meet in Texas!

      May 30, 2025

      Salesforce + Informatica: What It Means for Data Cloud and Our Customers

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025
      Recent

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Containerize .NET 8.0 Web API with Docker

    Containerize .NET 8.0 Web API with Docker

    January 28, 2025

    1. Docker

    Docker is an open-source platform, and it is used to containerize the application. It packages the application along with the dependencies into the containers, which is easier to deploy.

    Read more about Docker and its CLI on Docker CLI

    2. Setup Web API project in .NET 8

    2.1 Create a new ASP.NET Core Web API project

    1. Open Visual Studio 2022.
    2. Go to File -> New -> Project.
    3. Select “ASP.NET Core Web API”


    Fig 1: Create ASP.NET Core Web API

    2.2 Configure your new project

    1. Enter project name
    2. Enter Location
    3. Provide solution details


    Fig 2: Configure your new project

    2.3 Additional Information

    1. Select framework – .NET 8.0 (Long Term Support).
    2. Choose your authentication type – Default ‘None’.
    3. Uncheck HTTPS to disable local SSL certificate
    4. Uncheck ‘Enable container support’ to set up our own Docker file for practice purposes.
    5. Select ‘Use controllers’


    Fig 3: Additional Information

    2.4 Default controller – WeatherForecast

    1. The default controller called ‘WeatherForecast’ will be created and placed inside the controllers folder named as ‘WeatherForecastController.cs’
    2. Open the file to see the default methods to get the weather forecast details.


    Fig 4: Default controller – Weather forecast

    2.5 Run ‘Get’ API of Weather forecast

    1. Press ‘F5’ to build and run the Web API project.
    2. Opens the URL – http://localhost:portno/weatherforecast and returns the result in JSON format.


    Fig 5: Weather forecast – GET API

    3. Containerize Web API: Docker Setup, Build, and Run

    3.1. Create Docker file

    1. Right-click the Web API project -> Add -> Choose ‘Docker support’.


      Fig 6: Docker support
    2. Opens the ‘Container Scaffolding Options’.

      Fig 7: Container Scaffolding Options

    3.2. Dockerfile content

    A new Docker file will be created. Replace the content with the following code:

    1. The ‘FROM’ keyword creates a new build stage from a base image.
    2. ‘MCR’ stands for Microsoft Container Registry (mcr.microsoft.com).
    3. The ‘EXPOSE’ instruction allows you to expose the port to access the application.
    4. The ‘ENTRYPOINT’ instruction sets ‘dotnet’ as host for the ‘DockerSample.API.dll’.

    Code:

      # Use the official .NET Core SDK as a parent image
      FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
      WORKDIR /source
      COPY . .
    
      RUN dotnet restore "./DockerSample.API.csproj" --disable-parallel
      RUN dotnet publish "./DockerSample.API.csproj" -c release -o /app --no-restore
    
      # Build the runtime image
      FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
      WORKDIR /app
      COPY --from=build /app ./
    
      # Expose the port your application will run on
      EXPOSE 5000
    
      # Start the application
      ENTRYPOINT ["dotnet","DockerSample.API.dll"]
    

    Screenshot:



    Fig 8: Docker file content

    3.3. Docker Build

    1. Open the terminal (Powershell) and navigate the Web API project path.
    2. Run the Docker build command to build the image.
    3. Syntax: Docker build –rm -t [dockerimagename]:latest .
    4. Example: docker build –rm -t dockerwebapiimage:latest .

    -t stands for tag to tag the image.

    –rm option automatically removes the container when it exits.

    Note:

    The above steps 3 & 5 end with ‘.’, which is required to set the current directory.

    Hostinger


    Fig 9: Docker Build

    3.4. Docker Run

    A Docker container is a running instance of a Docker image. It allows you to package the application code and its dependencies into a single unit.

    1. Run the below command to create a docker container from a docker image.
    2. Syntax: docker run -d -p host:portno:defaultport [imagename]
    3. Example: docker run -d -p 127.0.0.1:5000:8080 dockerwebapiimage
    4. Returns the container ID as a response.

    -p stands for port number.

    -d indicates the container should run in detached mode.

    Note: The port number ‘5000’ that was exposed in the docker file has been mapped to the default port.



    Fig 10: Docker Run – create container

    3.5. Docker Desktop

    Docker Desktop can be used in Windows and Mac to manage the various Docker components, including containers, images, volumes, etc.

    1. Go to Docker Desktop.


      Fig 11: Docker Desktop
    2. Containers

      List all the Docker containers. You can see the newly created container in the list and identify the same with the container ID.



      Fig 12: Docker Containers
    3. Container Details

      Shows the details of the selected container. The logs show the default port number, and the same has been mapped to the Docker file exposed port number during the Docker run execution.



      Fig 13: Docker Container details

    3.6. Output (Localhost)

    Open the browser and enter the following url:
    http://localhost:5000



    Fig 14: Docker Run – Localhost

    Source: Read More

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDiscover the best way to learn modern Android development [FREE]
    Next Article Monitor the health of Amazon Aurora PostgreSQL instances in large-scale deployments

    Related Posts

    Security

    China-Linked Hackers Exploit SAP and SQL Server Flaws in Attacks Across Asia and Brazil

    May 30, 2025
    Security

    New Apache InLong Vulnerability (CVE-2025-27522) Exposes Systems to Remote Code Execution Risks

    May 30, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    This AI Paper Introduces Differentiable MCMC Layers: A New AI Framework for Learning with Inexact Combinatorial Solvers in Neural Networks

    Machine Learning

    Aggregate-and-Adapt Natural Language Prompts for Downstream Generalization of CLIP

    Development

    Extension Manager Update Brings UI Buffs, Support for GNOME 48

    Linux

    La Distribuzione GNU/Linux Absolute Linux Termina il Suo Percorso

    Linux

    Highlights

    Development

    Top Courses on Statistics in 2024

    May 23, 2024

    Statistics has become increasingly vital in today’s data-driven world. With the growth of big data…

    Planview Acquires Sciforma, Expanding Global Leadership in Portfolio Management Solutions

    February 21, 2025

    Laracon AU 2024 is heading to Brisbane, Queensland

    May 13, 2024

    This AI Paper Introduces MathCoder-VL and FigCodifier: Advancing Multimodal Mathematical Reasoning with Vision-to-Code Alignment

    May 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.