0% found this document useful (0 votes)
3 views17 pages

Dockerizing DotNet App

Docker is a platform that packages applications and their dependencies into containers, ensuring consistent behavior across different environments. It simplifies deployment by allowing developers to send a complete application kit, which includes the runtime and libraries, rather than just source code. The document provides a step-by-step guide on how to dockerize a .NET application, including creating a Dockerfile, building an image, and running a container.

Uploaded by

dragonkid2143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Dockerizing DotNet App

Docker is a platform that packages applications and their dependencies into containers, ensuring consistent behavior across different environments. It simplifies deployment by allowing developers to send a complete application kit, which includes the runtime and libraries, rather than just source code. The document provides a step-by-step guide on how to dockerize a .NET application, including creating a Dockerfile, building an image, and running a container.

Uploaded by

dragonkid2143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dockerizing .

Net Application

What is Docker?

Docker is a platform that allows developers to package an application along with


everything it needs to run (runtime, libraries, dependencies, configuration files) into a
single unit called a Container.

This container can run anywhere:

• Developer's Laptop

• Testing Server

• Production Server

• Azure

• AWS

• Linux

• Windows

without changing the application.

Simple Definition

Docker is a tool that packages an application and all its dependencies into a container
so that it runs the same on every machine.

Why was Docker introduced?

Imagine this situation.

Developer says

"My application works perfectly on my laptop."

Tester replies

"It is not working on my system."

Production team says

"It crashes on the server."

Why?

Because every machine is different.


Example

Developer Machine

• Windows 11

• .NET 8

• SQL Server

• IIS Installed

Testing Machine

• Windows 10

• .NET 6

• Different SQL Server

Production Server

• Linux

• No .NET Installed

Result

Application behaves differently.

Docker solves this problem.

Instead of sending only the project files,

Developer sends

Application + .NET Runtime + Libraries + Dependencies + Configuration

Everything is packed together.

That package is called a Docker Image.

Real-Life Example

Suppose you are making tea.

Normally you carry

• Tea powder

• Sugar
• Milk

• Cup

• Spoon

• Gas Stove

everywhere.

Instead,

you prepare a complete Tea Kit.

Anyone receives the kit.

They can make tea immediately.

Docker works exactly like this.

Instead of sending only source code,

it sends the complete application kit.

Before Docker

Developer Machine

Application

Testing Machine

Error

Missing .NET Runtime

Production

Different Configuration

Application Failed

After Docker
Developer

Application

.NET Runtime

Libraries

Configurations

Docker Image

Docker Container

Runs Everywhere

What is a Container?

A Container is a running instance of a Docker Image.

Example

Image

Recipe Book

Container

Prepared Food
Another example

Image

Blueprint

Container

Constructed House

What is Docker Image?

Docker Image is a template.

It contains

• Application

• Runtime

• Libraries

• Dependencies

• Environment Variables

Image cannot execute.

Container executes.

Example

Docker Image

[Link] Core API

.NET Runtime

Libraries


Run

Container

Image vs Container

Docker Image Docker Container

Template Running Application

Read Only Executing

Can create many containers Created from image

Similar to Class Similar to Object

Docker Architecture

Developer

Docker CLI

Docker Engine

----------------------------

Docker Image

Docker Container

Application Running

Important Docker Components

1 Docker Engine

Main software that runs Docker.

Responsible for

• Creating images

• Running containers

• Managing containers

2 Docker Image

Contains

• Application

• Runtime

• Libraries

3 Docker Container

Running application.

4 Docker Hub

Public repository where Docker images are stored.

Similar to

NuGet

or

GitHub

Example
docker pull [Link]/dotnet/sdk:8.0

Docker downloads the image from Docker Hub (or Microsoft's container registry).

Virtual Machine vs Docker

Virtual Machine Docker

Heavy Lightweight

Has Complete OS Shares Host OS

Slow Fast

Large Size Small Size

Boots in Minutes Starts in Seconds

Virtual Machine

Hardware

Host OS

Hypervisor

Guest OS

Application

Docker

Hardware

Host OS

Docker Engine

Container

Application

Docker doesn't install another operating system.

Hence it is much faster.

Advantages of Docker

Portable

Runs anywhere.

Lightweight

Consumes less memory.

Fast

Starts within seconds.


Consistent Environment

Same behavior everywhere.

Easy Deployment

Only image is deployed.

Version Control

Images have versions.

Isolation

Each container is isolated.

Common Docker Commands

Command Purpose
docker --version Check Docker Version
docker images Show Images
docker ps Running Containers
docker ps -a All Containers
docker build Build Image
docker run Run Container
docker stop Stop Container
docker start Start Container
docker rm Remove Container
docker rmi Remove Image

Docker Workflow

Write Application

Create Dockerfile

Build Image

Run Image

Container

Application Running

Dockerizing an [Link] Core Web API (.NET 8)

We will create the simplest possible API.

Step 1

Create Project

[Link] Core Web API

.NET 8

Project Name

DockerDemoAPI
Step 2

Create Controller

using [Link];

namespace [Link]

[ApiController]

[Route("api/[controller]")]

public class HelloController : ControllerBase

[HttpGet]

public IActionResult Get()

return Ok("Hello from Docker Container");

Run normally.

[Link]

Output

Hello from Docker Container

Step 3

Install Docker Desktop

Download and install Docker Desktop for Windows. During installation, enable WSL 2
integration if prompted. After installation, start Docker Desktop and wait until it reports
that Docker is running.

Verify installation from a terminal:

docker --version

docker info
Step 4

Create Dockerfile

In project root

Right Click

Add

New Item

Name

Dockerfile

Content

# Build Stage

FROM [Link]/dotnet/sdk:8.0 AS build

WORKDIR /src

COPY . .

RUN dotnet restore

RUN dotnet publish -c Release -o /app/publish

# Runtime Stage

FROM [Link]/dotnet/aspnet:8.0

WORKDIR /app

COPY --from=build /app/publish .


EXPOSE 8080

ENTRYPOINT ["dotnet", "[Link]"]

Explain Dockerfile

FROM

FROM [Link]/dotnet/sdk:8.0

Uses .NET SDK image.

SDK contains

• Compiler

• Build Tools

WORKDIR

WORKDIR /src

Creates working folder.

COPY

COPY . .

Copies project files.

Restore

RUN dotnet restore

Downloads NuGet packages.

Publish

RUN dotnet publish -c Release -o /app/publish

Publishes application.
Runtime

FROM [Link]/dotnet/aspnet:8.0

Runtime image is much smaller than SDK.

ENTRYPOINT

ENTRYPOINT ["dotnet","[Link]"]

Starts application.

Step 5

Open terminal in project folder.

Build image

docker build -t dockerdemoapi .

Explanation

docker build

-t

Image Name

Current Folder

Output

Successfully built

Step 6

Verify image

docker images

Output

REPOSITORY TAG
dockerdemoapi latest

Step 7

Run Container

docker run -d -p 8080:8080 --name mycontainer dockerdemoapi

Explanation

• -d → Run in background (detached mode)

• -p 8080:8080 → Map host port 8080 to container port 8080

• --name mycontainer → Give the container a friendly name

• dockerdemoapi → Image name

If your application listens on a different port (such as 80), adjust the mapping
accordingly.

Step 8

Check Running Container

docker ps

Example output

CONTAINER ID

STATUS

PORTS

NAMES

Step 9

Open Browser
[Link]

Output

Hello from Docker Container

Step 10

View Logs

docker logs mycontainer

Step 11

Stop Container

docker stop mycontainer

Step 12

Start Again

docker start mycontainer

Step 13

Remove Container

docker rm mycontainer

(Stop it first if it is running.)

Step 14

Remove Image

docker rmi dockerdemoapi

You might also like