DEV Community

Cover image for Docker - .Net Core
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

Docker - .Net Core

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

  • Install Docker Desktop (Community Edition): Link

  • Install .Net Core: Link

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

.\bin\Release\netcoreapp3.1\publish\
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

To verify images list, run below command

docker images
Enter fullscreen mode Exit fullscreen mode

Output

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 Instagram Facebook Twitter.

Latest comments (0)