Docker - .Net Core
Tags: csharp, docker, dotnet, beginners
Published: 2021-06-10 08:36:07

Docker container renders a lightweight way to separate your application from the rest of the host system, utilizing the resources enabled.

## Prerequisites

* More Info on Docker: [Link](https://docs.docker.com/get-started/overview/)

* Install Docker Desktop (Community Edition): [Link](https://www.docker.com/products/docker-desktop)

* Install .Net Core: [Link](https://dotnet.microsoft.com/download/dotnet-core)

## Getting Started

##### This article covers

* Create and publish a “Hello World” .Net Core App.

* Create and configure Dockerfile for .Net Core.

* Build a Docker Image.

* Create and run a docker container.

### Create and publish a “Hello World” .Net Core App.

Fire a new command prompt in “Administration Mode” and **create a new .Net core application** using the following command.

    dotnet new console -o App -n NetCore.Docker

The command will generate a new folder named “App” and create a Hello World .Net core application inside it.

**To test the console application**, run below command.

    dotnet run

The preceding command will output Hello World onto the console.

Before containerizing the app, first it must be published. Create a publish build of a .Net Core application run below command with the “Release” parameter.

    dotnet publish -c Release

The preceding command will generate a published version of Hello World application into the following path.

    .\bin\Release\netcoreapp3.1\publish\

### Create and configure Dockerfile for .Net Core

After necessary .Net Core application setup is ready and published, let’s move on to create a docker file.

Create a file named “Dockerfile” in the App folder directory. Open the created file and place the below fragment into it & Save.

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

The above line instructs Docker to use .Net Core Runtime environment, which is relevant for .Net Core application created above.

### Build a docker image

Docker will process all lines inside the Dockerfile. The “.” character in the build command tells Docker to use the existing folder to find the Dockerfile.

    docker build -t counter-image -f Dockerfile.txt .

**To verify images list**, run below command

    docker images

**Output**

![](https://cdn-images-1.medium.com/max/2000/1*w4wu48Q6zOdaFRVviG4LOA.png)

## Create and run a docker container

**Create Container**

After a container is running, you can establish a connection to see the output. However, to create a container applying below command.

    docker create --name core-counter counter-image

**Run a docker container**

Below command perform two things, first, automatically use the command prompt to connect to the container, and then when the container completes the task, remove it.

    docker run -it --rm counter-image

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh-532284a2/) [Instagram](https://www.instagram.com/sukhpindersukh/) [Facebook](https://www.facebook.com/sukhpinder.singh.52/) [Twitter](https://twitter.com/sukhsukhpinder).