Efficient Containerization of .NET Applications using dotnet publish
0 views
Jun 19, 2025
This session offers a thorough, practical guide to containerizing .NET applications using dotnet publish, covering the fundamental concepts and benefits of containerization, the necessary environment setup, and the use of dotnet publish for preparing applications for containerization. Conference Website: https://dotnetconference.com šŗ CSharp TV - Dev Streaming Destination http://csharp.tv š C# Corner - Community of Software and Data Developers https://www.c-sharpcorner.com #CSharpTV #CSharpCorner #LiveShow #dotNet #DotNetConf2025
View Video Transcript
0:00
so this session is about u efficient
0:04
containerization of net application
0:07
using net publish so my name is I'm a
0:11
Microsoft
0:12
MVP so uh first of all why we need to
0:18
contain application so basically when we
0:22
are
0:24
uh like developing and deploying
0:26
application
0:27
uh earier we used to my okay if I'm
0:32
building a net application is my host
0:35
also contains the same version of the
0:39
net installed on that machine or some if
0:42
you are using some other dependencies we
0:45
have to make sure that dependencies are
0:47
already installed on that machine so all
0:50
those problems we were able to fix with
0:54
docker container so when docker
0:58
introduced or containerization
1:00
introduced
1:01
in core we were uh we can just build the
1:06
application and container and we will be
1:10
able to deploy to the source
1:13
so uh basically uh when I started my
1:16
career I used to like say okay it works
1:20
on my machine and my manager and used to
1:24
say okay we are not shipping your
1:25
machine to the client so uh docker
1:28
actually solves that problem by pulling
1:31
everything abstracting everything into
1:34
one or every dependencies everything
1:37
into a single unit and you can deploy
1:40
that unit so these are the various uh
1:44
things why we need to conduct it we'll
1:47
be able to port the applications very
1:50
easily between various
1:52
environments we'll be able to scale so
1:55
for example if I'm running an
1:57
application in uh container I'll be able
2:01
to like say I want to run five instance
2:05
of that
2:06
application i can just say okay my tail
2:10
value is five then I'll be able to run
2:13
it in five
2:16
different different instances and uh
2:20
it's same line so as a developer I can
2:24
focus on okay building the application
2:26
and I don't need to worry about okay in
2:29
my server that dependency is there or
2:32
the same version of net is installed on
2:34
that I don't need to
2:37
worry so uh when in when we starting uh
2:42
like container we need to understand
2:46
okay what all things we need to so we
2:48
need in the traditional container
2:50
session These are the challenge so when
2:53
we are developing a container you need a
2:56
specific file called docker docker file
3:00
so it is a set of instruction what link
3:03
you need to be part of that container
3:07
image so it is manual process like if
3:11
you're using your code you will be able
3:14
to generate it but still there are some
3:17
challenges for example I'm building a
3:20
web AP application file i am adding a I
3:23
created a docker file then I am adding a
3:26
library as a dependency then I have to
3:29
modify my docker file to adjust that
3:34
also i need to copy that i need to
3:36
update the build step to build that
3:39
assembly all those things it should be
3:42
again
3:43
updated that is one issue second is uh
3:48
we want we will be like we have to make
3:52
sure that container runtime is installed
3:55
on my machine so if you're using docker
3:58
or pod man or any other container
4:01
runtime we have to make sure it is
4:03
installed and uh since this is a
4:06
virtualization
4:07
technology either you have to
4:10
install like uh this one WSL or
4:16
virtualization hyperv so all those
4:18
things matter so that we have to enable
4:22
virtualization in your machine if it is
4:24
not usable
4:26
okay you won't be able to do that you
4:29
have to enable WSL all those things and
4:32
in
4:33
PCD we have to conteer more step for
4:37
adjusting the uh again Docker has to be
4:40
installed on the
4:42
pipeline all those things and people
4:46
learning for VN so if you're a net
4:49
developer and you have to learn one more
4:52
thing like you need to understand uh
4:56
yiml you need to create a doctor files
4:59
you need to understand some uh like
5:03
basics of Linux
5:06
um and what are base images how they
5:10
work all those things it may be a little
5:13
all working for a newcomer but it is
5:18
So
5:21
how do net team or Microsoft net
5:25
team resolve these issues uh in from 6
5:30
onwards uh they introduce a new
5:33
capability uh we can publish
5:37
docker images or we can create docker
5:41
images without any container uh
5:44
container running we can use the dot
5:48
publish command to create
5:51
a docker name without any to uh no
5:57
docker file container runtime if
6:00
needed and it's very simple you just
6:03
need to say
6:04
docker you can create a project as a
6:08
docker publish with the uh default
6:11
profile
6:13
command and If you want to customize the
6:18
uh docker image settings you can modify
6:21
the project file and
6:24
update so these are the
6:27
prerequisites uh you need net by SDK and
6:32
container contain again it's optional
6:35
uh if you already installed it good if
6:39
you want to play around and you want to
6:42
test it or um like
6:46
u run in locally all those things you
6:50
need docker or man or any other
6:54
container let's jump into the demo i
6:58
already created an
7:00
uh web a application
7:04
so it's nothing just plain old net web
7:13
ap
7:17
and then uh I am already running docker
7:21
in my
7:24
machine but I don't have any images I
7:27
don't have any
7:28
containers so I'm just going to export
7:32
this command
7:38
[Music]
7:44
uh this is uh everything is 8 uh it was
7:50
uh from 6 onward so it is building a
7:55
image hello world that is the project
7:58
name with a tag tag is basically version
8:02
of thing so tag latest on base image
8:09
Microsoft that is
8:11
the base image from that it is actually
8:16
the kind of kernel for that okay now if
8:21
I say
8:23
docker go here and images you'll be able
8:27
to see it created an
8:30
image let's run it docker run
8:35
[Music]
8:48
so it started as web server uh listening
8:52
on port 80 i don't have any uh like
8:57
uh or any other thing working on my
9:00
machine so it
9:04
is
9:09
and now you'll be able to see okay this
9:14
is warning I'm getting this is actually
9:17
working let me modify that
9:28
log I'm typing
9:32
No and shutting
9:36
down then publish it again
9:51
now if I run this
9:54
again and browse
9:57
again you'll be able to see that
10:01
log so this is the way we'll be able to
10:06
publish the uh content or save document
10:11
now uh there are certain things all
10:15
these things are running on time
10:18
machine but uh sometime if I'm
10:22
publishing to a uh hub or docker hub or
10:29
any container registry so docker or
10:32
container workflow is basically you
10:34
create a container you publish it to a
10:38
container registry and your of stream or
10:42
de deployment team takes that image or
10:46
your whatever KD pipeline takes that
10:49
image and deploy to the that of in
10:58
natur that is the offer
11:02
so if I want to publish to docker hub
11:06
I'm not using container say I'm using
11:09
docker hub so in that scenario I have to
11:13
make sure that my container name instead
11:17
of hello world it should be in the
11:19
format of uh hub
11:23
username/ the image name application
11:26
name so if I want to customize that I
11:30
just need to add a property like this
11:33
container repository this is my docker
11:36
hub username and this is the application
11:39
and now if I do uh
11:53
this Oh sorry i didn't save
12:04
the Now it build a different image so
12:09
Android with the
12:11
latest okay now if I come
12:14
here you'll be able to see
12:17
Android the
12:19
same no
12:21
other and
12:23
uh keeping uh the latest as version is
12:28
not the recommended packing so if I want
12:32
to customize the tag I can do it with
12:36
containment tag
12:38
uh tag containment tag
12:42
element
12:46
so
12:49
here these are the various customization
12:51
options so I'm changing I added a work
12:56
v1 so I'm keeping
12:58
down these things
13:02
value then I'm updating again
13:16
now it came as V1 tag early it
13:19
was now it is
13:22
V1 and
13:25
uh so okay so if you look
13:29
into you'll be able to see Debian and if
13:34
I want to and it's almost 220 MB 2 MB so
13:41
if I want to reduce the size of that
13:44
image I can use alpine image so I'm
13:51
just changing it to al and I'm updating
13:55
the tag as well
13:58
now if I run this
14:14
again and if I go
14:16
here that alane got a smaller footprint
14:21
also 110 m and it's
14:26
al So these are the various ways you can
14:32
connect and uh all these things are
14:36
published to my local registry if I want
14:39
to publish it to a different one what I
14:43
can do I
14:45
can like add a container registry which
14:49
is the container you want you can set
14:54
it yeah okay this is the way you can run
14:57
the
14:59
container and for deploying to docker
15:02
hub you need to uh set these two
15:06
environment container with the username
15:08
and
15:09
password
15:14
let's go
15:23
[Music]
15:36
[Music]
15:41
[Music]
15:59
that open
16:14
i don't have any word or
16:17
anything
16:18
so here I'm uncommenting this
16:58
Um I am publishing again
17:03
now hopefully it will get published
17:06
to Yeah it is actually getting published
17:09
to
17:11
the you see here
17:44
yeah some practices
17:47
[Music]
18:10
no so these are some best practices you
18:13
can follow use physical tags always
18:18
don't use status uh monitor the image
18:21
size so you can optimize the tane or any
18:24
other images you can do security
18:26
scanning uh regularly scan me for so
18:31
there are yeah
18:33
this was to the
18:37
uh so there are some uh tools available
18:40
for security
18:42
yeah these are some resources uh which
18:45
helps you to learn more about it and
18:49
thank you