⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, November 7, 2025

Containerizing an ASP.NET Core 3.0 Web API

 how to containerize or dockerize an ASP.NETCore 3.0 Web API. I talk about: • How to create an ASP.NET Core 3.0 Web API project • How to add Docker artifacts with Visual Studio Code • How to build and run the ASP.NET Core project as a Docker container 🔥Become a Senior C# Backend Engineer

0:00

welcome today I'll show you how to

0:02

contain your eyes or doctor eyes on

0:04

asp.net core to 0 Web API you'll learn

0:07

how to create an asp.net cortecito Web

0:09

API project how to add docker artifacts

0:12

with missile studio code and how to

0:14

build and run the asp.net core project

0:17

as a docker container before getting

0:21

started let's go through a few things

0:23

that you'll need in your box to follow

0:24

the steps in this video the first one is

0:27

a dotnet core CLI which you can get at

0:30

dot dot net and this will allow you to

0:33

both build and run net core apps in your

0:36

box next we're going to use missile

0:40

studio code which you can get at code

0:41

that we saw studio.com and this we'll be

0:44

using to explore the contents of the

0:45

project that we're going to create as

0:47

well as to edit the docker files used to

0:50

a queenly container images next you're

0:54

going to need a detour engine which you

0:56

can get docks that docker calm and this

1:01

you're going to need in order to be able

1:02

to enable the creation of docker images

1:05

and the execution of the commands in

1:07

your box if you scroll down to the

1:08

support platform section you should be

1:10

able to find a version for your OS and

1:14

finally the postman tool is a very handy

1:18

tool to be able to query a Web API with

1:21

a very nice interaction this is an

1:24

optional requirement as you may as well

1:26

just use the browser for this but it's

1:28

really nice to use polls and for this

1:30

and it's completely free also notice

1:32

that all these tools are available in

1:34

multiple operating systems so you're

1:37

free to pick whichever version works for

1:38

you for this video we'll be using the

1:41

Windows version of the tools so now

1:43

let's close this so now let's go to a

1:48

command prompt and here let's use the

1:50

dotnet core CLI to create a brand new

1:53

asp.net core 3-0 Web API project if we

1:57

were going to call hello a spinet core

1:59

so let's do dotnet new web api

2:03

- oh hello ASP net core and just for the

2:09

sake of making these things a bit simple

2:10

for this video we will not be enabling

2:12

HTTP

2:12

yes but of course you should always

2:15

enable EVPs in your production projects

2:18

so let's do know yes

2:21

so this quantity generate a bunch of

2:23

files in a new hello a spinet call

2:25

directory and create separate for us so

2:28

let's switch to that directory and then

2:32

let's open a business video code to see

2:35

what code generated so we'll do code dot

2:38

and here we are in visa to the code this

2:43

clause is from here and the maintain

2:45

that of our interest is the controller

2:47

that gets included in this a new

2:50

template which is called weather

2:51

forecast controller so in this one

2:55

and before we move over let me actually

2:57

talk about this little prompt here these

3:00

a process to add a few files that this

3:02

is to the code needs to be able to both

3:04

build and run the project in from

3:07

business to your code itself generally a

3:08

good idea to say yes here so that's

3:10

going to get a new that PS code folder

3:12

without going to files in there now back

3:15

to the controller weather forecast

3:16

controller is a very simple a controller

3:19

that gets automatically added as you

3:20

create this new project has just one

3:22

week one API and get API and what it

3:26

does is just returns a bunch of a

3:28

weather cold weather forecast rejections

3:31

or that base it on some date some

3:35

temperature and some summary that's also

3:37

based in this random list of a weather

3:39

summaries ok so this is the API that we

3:43

will be working with across this video

3:44

so now let's actually run this before

3:47

container rising and see what we get so

3:49

let's go to the debug a hop and let's do

3:54

start debugging this is going to do data

3:57

built to builder project and then it's

4:02

going to spin up a browser for us so the

4:06

project is running in localhost

4:08

in port 5000 the actual API is the

4:12

weather forecast and route matching the

4:14

controller that got created so go there

4:16

and as you can see we are saying we're

4:19

getting results out of this call to the

4:21

weather forecast API now like I said you

4:24

can totally use a browser for this

4:26

usually postman is a much better tool

4:28

for exploring what comes back from this

4:30

API so that's what I'll be opening

4:32

postman here and I'm just going to

4:34

create a new request get request to that

4:37

URL and then I'll click send and as you

4:42

can see here now we have those five a

4:45

random weather forecasts a generated for

4:48

us in a very nicely formatted a way okay

4:53

so now let's my since and let's close

4:58

this and let's stop the debugger now

5:01

it's time to actually containerize aid

5:03

applications also close this so in order

5:05

to do that one thing that I recommend is

5:07

to add the docker extension for visual

5:10

studio code and that you can do from the

5:12

extension table if you don't have it

5:14

already installed just look for docker

5:16

it should be the first the first thing

5:19

on pops out there and Yossi Starling

5:21

will take a few seconds what this will

5:23

allow us is to among other things it

5:26

allows to automatically generate a token

5:28

file for the platform that we're working

5:30

with here this case a spinet core so

5:33

close these go back to this folder and

5:36

now let's go to view command pull it and

5:39

let's dive docker a docker files to

5:43

workspace so that is going to ask you

5:45

because a fourth application platform

5:47

that we're working with in this case is

5:49

infinite core which operating system we

5:52

are targeting with this container in

5:53

this case is going to be Linux and which

5:56

is the port that will be our app is

5:58

going to be listening on and let's just

6:00

leave it as port 80 and as you can see

6:03

an initial docker file code generated

6:05

for us right away now let's listen slow

6:09

this file and see what what all this

6:11

stuff means so each line here really

6:13

means or creates a layer within our our

6:18

application that builds on top of the

6:20

previous layer so the first line here

6:22

says that we're going to be creating an

6:23

infinite a core 3 0

6:27

app and that we're tagging this layer or

6:30

this stage as base okay and so we're

6:34

going to be using the MGR magnet comm /

6:36

dance / core / a spinning repository and

6:39

then we will be switching to a new deal

6:42

called a / app as I worked here I will

6:46

be exposing a port 80 as we already said

6:49

now interestingly this template uses

6:51

this concept of multi stage fields which

6:53

is a very nice way to optimize the size

6:55

of the of the docker image what it says

6:57

is that after doing that do not keep

7:00

using that in that stage there and let's

7:04

switch to another stage which is pretty

7:05

much kind of another image kind of an

7:08

oil image and that will we will be at

7:11

aliasing as built and this one here is

7:14

going to be base it actually in the done

7:16

that core sdk-based image 3-0 image

7:20

which is definitely bigger it includes

7:22

all sorts of tools and compilers and

7:25

stuff that you need to build it up but

7:27

that you will not need when it's time to

7:29

actually around there so we will be

7:31

running all this section out of that

7:34

that image and then will be later on

7:37

will be coming back to the initial base

7:38

image that we use in over there where

7:41

we'll use copy the files of all talking

7:43

read but idea is like I said to optimize

7:46

alt so that the final image only

7:47

includes the files needed to run the app

7:49

but not all the other files needed to

7:50

compile there so we say go from that

7:53

basically mission this analysis at Build

7:55

work tears going to be SRC let's copy

7:57

the the project file into the root of

8:00

easy victory is our scene let's restore

8:03

all dependencies over there and no gate

8:05

restore and all dependencies of the

8:07

project copy all the other files into

8:09

that same location we will remove this

8:12

line here is not quite needed and then

8:14

lastly let's do the net build and to

8:16

build the project but in this case since

8:18

we're pulling a locker image it makes

8:20

more sense to do a release version as

8:22

opposed to the default depopulation of

8:23

the app and let's uh put all that into

8:25

an app directory then we go ahead and we

8:29

go from that for that stage and we come

8:32

up with and get another stage called the

8:34

polish stage where we're going to say

8:36

just dotnet polish that same project in

8:40

the same configuration

8:41

and the same opportunity so that ends up

8:45

finishes the actual compilation and

8:47

creation of the binaries they're good to

8:49

include it in this in this docker image

8:51

and as a final step we go back like I

8:53

said to the base image and we see we

8:56

switch to an /ab directory and most

8:59

importantly we copy the files that live

9:02

in this Polish directory created in the

9:04

previous stage and into the slash app

9:08

directory that was declared as a work

9:09

dear a moment ago so all the files are

9:11

in their previous coffee here everything

9:13

else all the all these other layers that

9:15

were created here the involve launch of

9:17

extra files are completely scarred not

9:19

including the final image so keeping the

9:21

image very small in size and finally we

9:24

will say that the entry point is going

9:25

to be dotnet and pointing to the hello a

9:27

spinet code that the DLL and that we

9:29

have here which is the same that we'll

9:31

get if we just explore quickly the bin

9:33

directory and over here for for the DLL

9:38

does produce it by our project ok so now

9:43

that we have that let's open at terminal

9:45

just open a brand new terminal here in

9:47

this code and let's actually build the

9:50

image out of this ogre file one more

9:54

thing that I mentioned that I didn't

9:55

mention by the way is that dr. ignore

9:57

file which is similar to if you know kit

10:00

is similar to ignore file it just

10:01

mentions a bunch of a potential

10:04

directories and files that you may have

10:05

a in your project structure that you

10:08

want to exclude from the image is going

10:11

to be created so for instance anything

10:13

that has to do with being an obj where

10:15

you may not want to include them into

10:19

the final image you just want to include

10:21

the stuff that God that is meant to be

10:23

distributed within the container ok so

10:27

now let's actually build this image and

10:28

we do that using the docker build

10:32

command and we need to sign an attack to

10:34

these images going to reveal so the

10:36

attack will be composite of a name and

10:38

an actual tag so the tank is going to be

10:41

the name of a image is going to be hello

10:43

asp net core and let's say it will call

10:47

it a big one so this is like I said is

10:50

just attack so it's a way to remember

10:53

a way to address this version of the

10:55

image so usually you will be putting

10:57

here some sort of mill number build

10:58

version something that uniquely

11:00

identifies at this version of the image

11:02

that you're producing and finally I'll

11:04

say dot to include to mention that this

11:07

plot is current location is the is a

11:09

context of the docker bill command

11:11

that's going to be executed so I'll go

11:14

ahead and say enter what it is going to

11:17

do is equal to NCR micro calm and

11:20

download the ethernet core image first

11:23

if you need call runtime image and tag

11:26

it as a space then it gets also the ACK

11:29

image and both of them are going back

11:31

into my into my box and of course first

11:36

time this this may take a little bit

11:37

because it tackling is to download all

11:39

these layers into your box

11:41

but after immediately after the second

11:44

time you do this this is going to be

11:45

blazing fast because team is already

11:47

there and only the new images will be

11:49

created so as you see after doing that

11:51

it will go ahead and follow one by one

11:53

each of the lines that we declared

11:56

include the restoration and the build

11:58

building of these of the project and

12:00

finally copying the files into the final

12:03

location

12:03

and this is all done okay and then when

12:08

that is done let's verify that the image

12:10

correctly created so let's do docker

12:13

images and as you can see the image that

12:18

we created is right here

12:19

hello spinet core tag we won and also

12:23

you can notice that we have these other

12:25

two images downloaded into your into our

12:27

box for next for next version want to

12:31

create now that we have the image there

12:33

we may want to also run it so let's run

12:36

it to verify that it's actually working

12:37

as expected

12:38

to do that we can use the docker run -

12:44

IT - RM command so this is kind of an

12:48

interactive session and that the docker

12:50

container should be removed after we

12:52

finish a run in the process that running

12:55

inside the image we should also map a

12:58

some port into the internal port of the

13:00

container so for instance we will say

13:02

that when we browse to the 8080 port

13:05

that should be route

13:06

into the into port 80 within the

13:09

container okay so this is the way to

13:10

kind of open a little window into the

13:12

the app that's running inside a

13:14

container and then we need to mention

13:16

which which image we actually want to

13:18

run here so that would be the hello asp

13:22

net core v1 image so let's hit enter and

13:29

as you can see the app is now running

13:31

just as if we were running it directly

13:33

from from our box and it's just that now

13:36

it is not running and going back to

13:38

Boltzmann it is not running in port 5000

13:40

it's actually running in port 8080

13:43

mapping inside into port 80 so if you

13:45

just hit this you can see we just got a

13:49

response back it's exactly the same

13:51

thing as if it was running in the box

13:53

but now it is running within the

13:54

container and finally I had to stop the

13:57

container we can use to control C and

13:58

that's going to stop the container and

14:01

actually discard it keeps the image but

14:03

this cars the container because of the

14:05

options that we specified oh there you

14:08

go and thanks for watching I hope this

14:11

video was very useful and I'll see you

14:14

next time

 

No comments:

Post a Comment