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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

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

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      7 MagSafe accessories that I recommend every iPhone user should have

      June 1, 2025

      I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

      June 1, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 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

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

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

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025
      Recent

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025

      Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 22/2025

      June 1, 2025

      Rilasciata PorteuX 2.1: Novità e Approfondimenti sulla Distribuzione GNU/Linux Portatile Basata su Slackware

      June 1, 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.



    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

    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

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 1, 2025
    Artificial Intelligence

    LWiAI Podcast #201 – GPT 4.5, Sonnet 3.7, Grok 3, Phi 4

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    What Do You Want to See From Ubuntu in 2025?

    Development

    DeepMind CEO claims Google has the “ingredients” to maintain AI lead over DeepSeek’s “exaggerated” success — after Microsoft CEO Satya Nadella said Google already missed the opportunity to be the “default winner”

    News & Updates

    A Fluent Email Validation Rule Added in Laravel 11.38

    Development

    MSI goes big at CES 2025 with its lineup of new 18-inch gaming laptops

    News & Updates

    Highlights

    Development

    Google Acquires Wiz for $32 Billion in Its Biggest Deal Ever to Boost Cloud Security

    March 18, 2025

    Google is making the biggest ever acquisition in its history by purchasing cloud security company…

    The Power of Linux Shell Environment Variables

    The Power of Linux Shell Environment Variables

    April 9, 2025

    How regulatory standards and cyber insurance inform each other

    August 22, 2024

    Rubikverse – Online Rubik’s Cube Solvers, Simulators & Tutorials

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

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