{"id":352,"date":"2025-01-25T18:30:00","date_gmt":"2025-01-25T18:30:00","guid":{"rendered":"https:\/\/imcodinggenius.com\/?p=352"},"modified":"2025-01-25T18:30:00","modified_gmt":"2025-01-25T18:30:00","slug":"containerize-net-8-0-web-api-with-docker","status":"publish","type":"post","link":"https:\/\/imcodinggenius.com\/?p=352","title":{"rendered":"Containerize .NET 8.0 Web API with Docker"},"content":{"rendered":"<h3>1. Docker<\/h3>\n<p>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.<\/p>\n<p>  <span>Read more about Docker and its CLI on <a href=\"https:\/\/codetolive.in\/docker\/Building-Your-First-Container-with-a-HelloWorld-Image-using-docker-cli\/#1-what-is-docker-cli\">Docker CLI<\/a><\/span><\/p>\n<h3>2. Setup Web API project in .NET 8<\/h3>\n<h4>2.1 Create a new ASP.NET Core Web API project<\/h4>\n<p>  Open Visual Studio 2022.<br \/>\n  Go to File -&gt; New -&gt; Project.<br \/>\n  Select \u201cASP.NET Core Web API\u201d<\/p>\n<p>\n    Fig 1: Create ASP.NET Core Web API<\/p>\n<h4>2.2 Configure your new project<\/h4>\n<p>  Enter project name<br \/>\n  Enter Location<br \/>\n  Provide solution details<\/p>\n<p>\n    Fig 2: Configure your new project<\/p>\n<h4>2.3 Additional Information<\/h4>\n<p>  Select framework &#8212; .NET 8.0 (Long Term Support).<br \/>\n  Choose your authentication type &#8212; Default \u2018None\u2019.<br \/>\n  Uncheck HTTPS to disable local SSL certificate<br \/>\n  Uncheck \u2018Enable container support\u2019 to set up our own Docker file for practice purposes.<br \/>\n  Select \u2018Use controllers\u2019<\/p>\n<p>\n    Fig 3: Additional Information<\/p>\n<h4>2.4 Default controller &#8212; WeatherForecast<\/h4>\n<p>  The default controller called \u2018WeatherForecast\u2019 will be created and placed inside the controllers folder named as \u2018WeatherForecastController.cs\u2019<br \/>\n  Open the file to see the default methods to get the weather forecast details.<\/p>\n<p>\n    Fig 4: Default controller &#8212; Weather forecast<\/p>\n<h4>2.5 Run \u2018Get\u2019 API of Weather forecast<\/h4>\n<p>  Press \u2018F5\u2019 to build and run the Web API project.<br \/>\n  Opens the URL &#8212; http:\/\/localhost:portno\/weatherforecast and returns the result in JSON format.<\/p>\n<p>\n    Fig 5: Weather forecast &#8212; GET API<\/p>\n<h3>3. Containerize Web API: Docker Setup, Build, and Run<\/h3>\n<h4>3.1. Create Docker file<\/h4>\n<p>  Right-click the Web API project -&gt; Add -&gt; Choose \u2018Docker support\u2019.<\/p>\n<p>\n   Fig 6: Docker support<\/p>\n<p>  Opens the \u2018Container Scaffolding Options\u2019.<\/p>\n<div><\/div>\n<p>\n   Fig 7: Container Scaffolding Options<\/p>\n<h4>3.2. Dockerfile content<\/h4>\n<p>A new Docker file will be created. Replace the content with the following code:<\/p>\n<p>  The \u2018FROM\u2019 keyword creates a new build stage from a base image.<br \/>\n  \u2018MCR\u2019 stands for Microsoft Container Registry (mcr.microsoft.com).<br \/>\n  The \u2018EXPOSE\u2019 instruction allows you to expose the port to access the application.<br \/>\n  The \u2018ENTRYPOINT\u2019 instruction sets \u2018dotnet\u2019 as host for the \u2018DockerSample.API.dll\u2019.<\/p>\n<p>Code:<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">  # Use the official .NET Core SDK as a parent image<br \/>\n  FROM mcr.microsoft.com\/dotnet\/sdk:8.0 AS build<br \/>\n  WORKDIR \/source<br \/>\n  COPY . .\n<p>  RUN dotnet restore &#171;.\/DockerSample.API.csproj&#187; &#8212;disable-parallel<br \/>\n  RUN dotnet publish &#171;.\/DockerSample.API.csproj&#187; -c release -o \/app &#8212;no-restore<\/p>\n<p>  # Build the runtime image<br \/>\n  FROM mcr.microsoft.com\/dotnet\/aspnet:8.0 AS runtime<br \/>\n  WORKDIR \/app<br \/>\n  COPY &#8212;from=build \/app .\/<\/p>\n<p>  # Expose the port your application will run on<br \/>\n  EXPOSE 5000<\/p>\n<p>  # Start the application<br \/>\n  ENTRYPOINT [&#171;dotnet&#187;,&#187;DockerSample.API.dll&#187;]\n<\/p><\/div>\n<\/div>\n<p>Screenshot:<\/p>\n<p>\n    Fig 8: Docker file content<\/p>\n<h4>3.3. Docker Build<\/h4>\n<p>  Open the terminal (Powershell) and navigate the Web API project path.<br \/>\n  Run the Docker build command to build the image.<br \/>\n  Syntax: Docker build \u2013rm -t [dockerimagename]:latest .<br \/>\n  Example: docker build \u2013rm -t dockerwebapiimage:latest .<\/p>\n<p>-t stands for tag to tag the image.<br \/>\n<br \/>\n\u2013rm option automatically removes the container when it exits.<\/p>\n<p>Note:<\/p>\n<p>The above steps 3 &amp; 5 end with \u2018.\u2019, which is required to set the current directory.<\/p>\n<p>\n    Fig 9: Docker Build<\/p>\n<h4>3.4. Docker Run<\/h4>\n<p>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.<\/p>\n<p>  Run the below command to create a docker container from a docker image.<br \/>\n  Syntax: docker run -d -p host:portno:defaultport [imagename]<br \/>\n  Example: docker run -d -p 127.0.0.1:5000:8080 dockerwebapiimage<br \/>\n  Returns the container ID as a response.<\/p>\n<p>-p stands for port number.<\/p>\n<p>-d indicates the container should run in detached mode.<\/p>\n<p>Note: The port number \u20185000\u2019 that was exposed in the docker file has been mapped to the default port.<\/p>\n<p>\n    Fig 10: Docker Run &#8212; create container<\/p>\n<h4>3.5. Docker Desktop<\/h4>\n<p>Docker Desktop can be used in Windows and Mac to manage the various Docker components, including containers, images, volumes, etc.<\/p>\n<p>  Go to Docker Desktop.<\/p>\n<p>\n   Fig 11: Docker Desktop<\/p>\n<p>Containers<\/p>\n<p>List all the Docker containers. You can see the newly created container in the list and identify the same with the container ID.<\/p>\n<p>\n   Fig 12: Docker Containers<\/p>\n<p>Container Details<\/p>\n<p>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.<\/p>\n<p>\n   Fig 13: Docker Container details<\/p>\n<h4>3.6. Output (Localhost)<\/h4>\n<p>Open the browser and enter the following url:<br \/>\nhttp:\/\/localhost:5000<\/p>\n<p>\n    Fig 14: Docker Run &#8212; Localhost<\/p>","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <\/p>\n<div><a class=\"more-link bs-book_btn\" href=\"https:\/\/imcodinggenius.com\/?p=352\">Read More<\/a><\/div>\n","protected":false},"author":0,"featured_media":353,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/352"}],"collection":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=352"}],"version-history":[{"count":0,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/352\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/media\/353"}],"wp:attachment":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}