Getting started with Docker and ASP.NET Core application

Getting started with Docker and ASP.NET Core application

In this article, I will demonstrate how to dockerize ASP.NET Core Web Application in a few steps using Microsoft Visual Studio 2019 and Docker

Pre-requests

  1. Docker installed - I'm using Docker for Mac
  2. Visual Studio 2019 with .NET Core SDK

What is Docker?

No alt text provided for this image


Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

The developer can assure that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they're running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

And importantly, Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren't available out of the box.

Docker is a tool that is designed to benefit both developers and system administrators, making it a part of many DevOps (developers + operations) toolchains

What is Dockerfile?

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

What is Docker Compose?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration

Getting started with Docker

You can Install Docker here or switch to others OS (Windows, Linux)

What is ASP.NET Core?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. With ASP.NET Core, you can:

  • Build web apps and services, IoT apps, and mobile backends.
  • Use your favorite development tools on Windows, macOS, and Linux.
  • Deploy to the cloud or on-premises.
  • Run on .NET Core or .NET Framework.

Get started with ASP.NET Core

  1. Open Visual Studio 2019
  2. From the Home screen select New Project
No alt text provided for this image

3. From the left side select .NET Core Template, then click on App then Select Web Application and click Next

No alt text provided for this image

4. Enter the application name "dockerDemo" and click create

No alt text provided for this image

5. Now you can run your ASP.NET application locally by click on run icon in the toolbar or from Run Menu select Start Debugging

No alt text provided for this image

6. After building the project you will notice that the app has started, browse to https://localhost:5001

You will notice that in the project root if you open Dependencies you will find NuGet and SDK for ASP.NET Core

No alt text provided for this image


You can use the command line for creating the project without Visual Studio but you must have .NET Core SDK installed on your machine by the following commands

>: dotnet new webapp -o dockerDemo


Trust the HTTPS development certificate
>: dotnet dev-certs https --trust

>: cd dockerDemo
>: dotnet run

After the command shell indicates that the app has started, browse to https://localhost:5001

Adding Docker Support

7. Right click on the Project folder, then click Add and Click on Add Docker support

The amazing feature will create for you both the Dockerfile and docker-compose files without any effort from your side

You will find the Dockerfile with the following syntax

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY dockerDemo/dockerDemo.csproj dockerDemo/
RUN dotnet restore dockerDemo/dockerDemo.csproj
COPY . .
WORKDIR /src/dockerDemo
RUN dotnet build dockerDemo.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish dockerDemo.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet""dockerDemo.dll"]

And the docker-compose file

version: '3.4'

services:
  dockerdemo:
    image: ${DOCKER_REGISTRY-}dockerdemo
    build:
      context: .
      dockerfile: dockerDemo/Dockerfile

For sure you customize your docker files after that as you want

8. Now it's time for running the application using Docker commands

9. Right click on the Project, select Tools, then Open in terminal

10. You can use the docker-compose file to build the image directly and expose the app to the port using the following command

>: docker-compose up -d

Docker will start to pull .Net core docker image and build your app

11. Wait until Docker finish and check the running containers with the following command

>: docker ps

12. You will notice that the app running successfully

No alt text provided for this image

13. Navigate to the port and you will find the Web App running

No alt text provided for this image

14. To stop the docker container use the following command line

>: docker-compose down

Resources

https://docs.microsoft.com/en-us/visualstudio/containers/container-tools?view=vs-2019

Thank you for reading and Happy learning :)

Moataz

要查看或添加评论,请登录

Moataz Nabil的更多文章

  • From Inspiration to Publication: My Journey to Becoming a Tech Author

    From Inspiration to Publication: My Journey to Becoming a Tech Author

    Embarking on the journey of becoming a tech author and writing a book is a path many dreams of but few dare to tread…

    1 条评论
  • A Guide to the Mobile DevOps Playbook

    A Guide to the Mobile DevOps Playbook

    One week from now, my book "Mobile DevOps Playbook will be published; thanks Packt Publishing for giving me this…

  • How to make Mobile UI Regression Testing stable?

    How to make Mobile UI Regression Testing stable?

    In this article, I'll provide you with actionable steps to help you establish a reliable mobile UI regression testing…

    2 条评论
  • A helpful list of tools for your android test with Espresso

    A helpful list of tools for your android test with Espresso

    If you are using the Espresso test automation tool for your Android UI tests or you will start using it, you can check…

    1 条评论
  • 25 Bitrise Integration Steps You Should Know for iOS Apps

    25 Bitrise Integration Steps You Should Know for iOS Apps

    As we discussed previously in this article about the 20 Bitrise Integration Steps You should know for Android apps, We…

    2 条评论
  • 20 Bitrise Integration Steps You Should Know for Android Apps

    20 Bitrise Integration Steps You Should Know for Android Apps

    In this article I will go through the 20 Bitrise Integration Steps You Should Know for Android apps and in the next…

    1 条评论
  • Why Software Test Engineers should know about Cloud Computing?

    Why Software Test Engineers should know about Cloud Computing?

    In the article, I will explain from my point of view why Software Test Engineers should know about Cloud Computing…

    2 条评论
  • 20 Q&As about Bitrise every beginner should know

    20 Q&As about Bitrise every beginner should know

    In this article I will go through questions and answers every beginner should know about Bitrise. In the beginning, we…

  • Running Selenium Web Tests with GitHub Actions

    Running Selenium Web Tests with GitHub Actions

    In this article, I will explain the integration between GitHub actions and Selenium WebDriver for running Web UI tests.…

    6 条评论
  • Running Appium tests with GitHub Actions

    Running Appium tests with GitHub Actions

    In this article, I will explain the integration between GitHub actions and Appium for running mobile UI tests. This…

    6 条评论

社区洞察

其他会员也浏览了