Getting started with Docker and ASP.NET Core application
Moataz Nabil
Author of 'Mobile DevOps Playbook' | Software Engineering Manager, Platform Engineering | AWS Community Builder | Developer Advocate
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
- Docker installed - I'm using Docker for Mac
- Visual Studio 2019 with .NET Core SDK
What is Docker?
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
- Open Visual Studio 2019
- From the Home screen select New Project
3. From the left side select .NET Core Template, then click on App then Select Web Application and click Next
4. Enter the application name "dockerDemo" and click create
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
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
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
13. Navigate to the port and you will find the Web App running
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