C# Course - Docker, Kubernetes, and ASP.NET Fundamentals - Part 1 (Docker Survival Kit)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay i think we should get going let's switch to english uh and let's get started with docker and kubernetes this will be the topic of today's lesson as i told you we will split that into multiple parts the first part will be a survival kit about docker you told me that you have some basic knowledge about docker you know what docker is in general but most of you have only very limited uh experiences with um with docker files and that's that's exactly what we are going to practice okay do you have an idea what a dockerfile already is correct and that's exactly what i wanted to hear a docker file is a kind of recipe how to build an image i assume that you have at least basic knowledge a basic understanding what sorry i have to close my discord i assume you have a basic understanding of what a docker image is and exactly what you told us a docker file is a recipe for building a darker image are you aware of the fact that docker images consist of layers do you know what that is some of you say yes some of you don't okay good so this is exactly what we are going to take a look at we are going to take a look at docker files and you will get this slide deck from me obviously and let's quickly recap what uh these docker files and images are all about the first thing is a recap about images and then we are going to talk about uh docker files now this is how images typically look like you always have a base image the base image is typically an operating system like debian or ubuntu or alpine or whatever you choose and then you add multiple layers on top of the image you could install some applications like an editor emacs you could install a web server like apache and when you start the container from the image you get another layer on top of it which is the writable container layer so every file that you create is written on top of the next image is like an onion i always say darker images are like ogres have you seen shrek yeah odors are like onions they have layers i think this is what trek says and exactly that is true for images images have layers this is why you sometimes call it an onion file system i hope you are aware of that please be aware that these layers here the layers below the writable layer are immutable so they are read only you cannot write something there if you would delete the file you would never delete the file in the base image but you would always set a marker you would add something to the writable container so you would add a flag the file is deleted but the file is never really gone okay question i'm not sure if you know that is it possible to build a docker image without a base image like debian ubuntu alpine yes how how would you do that if i would give it that as a homework what would you do such a cookie yes could you do it with java could you do it with c sharp no no could it do it with go yes could you do it with rust yes could it do with c plus plus yes because below the base image you always have the linux kernel so if you manage to build software that only needs the linux kernel nothing else just the linux kernel then it's fine then you can really build an image that consists only of a single file and this single file is just sitting on top of the kernel in practice you never do that in practice you always have an operating system below can you tell me what operating systems are very common used uh very commonly used in connection with docker alpine correct for runtime images for instance for images that run on a web server something like this alpine is a great operating system debian is also very frequently used especially when it comes to more powerful containers that do the building of a software like translating c sharp into intermediate language these things are often based on on debian okay very good so and we want to build such an image okay you have here some additional information about how um how you can find the stuff exactly we can split it we can yeah docker files now let's talk about docker files i think the easiest thing to understand docker files is to see them in action okay so let me open a folder here let's say this is my web server and let's open this web server folder here with visual studio code okay i trust it i will zoom in a little bit so you can see it better let's create a docker file if you want you can but currently it's also okay if you just follow what i'm doing on the screen because the steps will be very very simple it's more for understanding the core principle okay so this demo is not that important we will not continue it it's just a demo to prove the concept to give you enough information to follow the kubernetes part so let me create an html file for instance and let's add a simple html file whose title should be hello docker and here we will just say h1 exactly hello docker something like this and now i want to build a web server that serves this static file now you have you have to use your imagination this could be your static web app this could be your react application this could be your angular application this could be as wealth application your view application your single page of your blazer application single page apps only consist of static files like my index.html in my case it will be super super trivial but still it is a single page app and now we need a docker file every docker file starts with a from clause and here we specify the base image if you can remember what we had here in the slides i told you that every image has a base image and therefore we have to choose a base image on which we want to base our code if we would write docker or rust or c or c plus plus code that wouldn't need a base image we would write from scratch that means no base image we are the base image an operating system like debian does exactly that if you take a look at the docker file of debian you will find from scratch in the first line but typically we don't do that at application developers do you have an idea what base image could we use for building an image that serves a single page app to a browser what could we use we need a wrap server right so any idea angenix i agree let's take angelix where can we get all these images from there is the so-called docker hub it's at hub.docker.com and there we can look for images for instance nginx nginx is a very popular web server a very lightweight web server and it works like charm this is how you can pull nginx docker pull and genix and you can if you take a close look here where is it here let me zoom in a little bit you can use an nginx version that runs on top of linux alpine and that is a very very efficient and fast and small web server taking alpine linux and taking angenix on top of it and that's the whole idea when you start the docker file you have to think of what is my base image sometimes all you need is the base image if you want to run postgres or mysql or mongodb or sql server you want just to run the database right why bother creating your own image you can run the image as it is but if you want to host your own application you have to use a base image like a web server or something like this and put your code on top of it and this is exactly what we are going to do so in our case we will just say take engenex alpine so let's build something on top of the angenix web server now the ngenix web server if we scroll down a little bit even more even more and more then you will see if i zoom in a little bit here has a a clearly defined folder here you see this folder where you can put your static application your static single page app and nginx will serve these static files from exactly there so what we can do i will copy this folder here to make sure that i don't make a mistake we can copy the index.html to exactly that folder and this line this line here is exactly what i meant with this next image so this thing here adds a layer on top of my onion so angenix is already an onion layered and layered and layered and layered in the middle we have alpine then we have engenix and so on and now we put our own application as a separate additional layer on top of our image understand it is super crucial to understand that this layering in docker is hugely important when it comes to performance because docker is smart enough to recognize if multiple of your images are based on the same base image like alpine for instance that docker will only store the base image once you will not fill up your precious solid state disks with unnecessary virtual disks you only have the base image once and if you have dozens of applications based on this base image the base image will only be present once also when pulling down an image from the internet docker will recognize that it already has the huge base image on your disk so it will not download the base image again it will only download those layers which are really different and which are not already on your machine got it why do i say this is important well build time install time if you deploy your application to your kubernetes cluster for instance the kubernetes cluster will be faster deploying your application because it already has the base image downloaded got it understood this is the reason why you have this this layering this onion system and believe it or not that's it we are done that's the entire docker file because we have a very simple application here question mark yeah let's try to build this docker file [Music] so here we are let's build this docker file and there is a statement which is called docker build then you can give it a name t is a tag let's call it hello engenix and then you specify the folder name where the the docker file can be found it's not perfectly correct what i'm saying here but the mental model is okay now you see the the docker system is now reading all the data that we have here and it's downloading everything and in a bunch of moments here you can see we are done so now we have an image which is called hello ngenix and it was created in nearly no time because it is so small let's take a look at this image darker images find hello nginx something like this and as you can see the entire image is only 22.1 megabyte that's the entire operating system that's the entire web server and our single page app understood we already have an image we can use that we can publish that we can run that on the on the on the hattie leoning cloud or any other cloud let's try that docker run give it the port for instance 808080 i will publish it on my local machine 8080 and hello nginx good started let's go to localhost 8080 and here we are see if we stop everything i can prove it to you it's really coming from there because if i want to reload it i have to stop it of course it was not automatically stopped i'm sorry for that but now see it's gone so what you should remember is a docker file is a kind of recipe for building a darker image darker images consist of layers and those layers the purpose of these layers is to save storage to save bandwidth because we only install layers that we need for multiple applications once that's the important thing that you should remember here question um that's correct nginx by default exposes the port 80. yes the default http port that is exposed by nginx yes correct any other questions okay i'm fine let's let's make that a little bit more complicated that's the last step that you need for your survival kit for docker and then we can immediately switch to our main topic today that's kubernetes but let's do the following let's make something which is very very simple we'll just add a typescript file here this typescript file will be super super simple it will just say uh const greet of type string equals to hello world okay and it will say console log greet in the index.html file we will specify a script with a source that says index.js javascript something like this and let's move that one to here so it will run when we start the application got it so essentially what we have to do is we have to translate the typescript file into javascript let's quickly do that and tsc index.ts i have not installed tsc globally let's quickly do that we will not do that locally later on so that's just for demo purposes so as you can see we translate the typescript file into javascript this is the javascript file and if i try to open this html thing just to prove the point i will get hello world in the console got it so our job is to translate the typescript file to host everything in nginx and then build an image from it how can we do that now let's delete the index.js again okay and let's think about what we have to do we have two possibilities we can either now follow closely this is important for your for your diploma but and so on we can either compile the code locally and then copy the ready-made compiled files into the image or we can compile the code inside the docker file and that is the interesting one what we're going to do now let me show you what i mean we go into the docker file and here we suddenly specify a second from statement in this from statement i will use the base image node alpine which will give me a very compact and small version of an opera a linux operating system with node installed with the latest version of node installed now i can do the following i can say let's give this guy a name okay as build now i can say um let's run npm install minus g like global typescript and once typescript is installed we can say tsc.edu index dot ts so we are building the javascript code inside the docker file and this is a general concept you could build your c sharp code in the docker file by running.net build you can build your jar files i'm hoping i'm not telling you wrong things by running java the java compiler and and compiling your java into something else you can compile your c code your go code your ros code whatever you want to do you can do whatever you want to do you can run your unit test everything in a docker file this is called a multi-stage docker file because we have one stage this one which does the building and then we have a second stage and get was guess what we have to do here we have to add another copy command and here we can say from build so we are referring to the build up here and then we are going to copy index.js to the current folder sorry not to the current folder to this folder of course and to make our uh docker file a little bit easier to read well we can say uh work there this one and then we can make it a little bit easier by just specifying dot i think even if you have not so much experience with docker files you can probably guess from the context what the different statements do understood we still have one error in the script we want to compile index.ts but we have never copied it so the last thing that we have to do is we have to copy the index.ts file to let's say slash app it's it's good it's something like this or let's let's do it like below work there app and then we can say dot and now it should work do you understand the principle now we are using a docker container to build our application type script to javascript c-sharp to intermediate language c to executable rust to executable go to executable this is what we do here and then we are building the resulting image which does no longer have the source code no typescript anymore just the object code in our case javascript and the html file so this will be tiny tiny tiny this one will be rather huge because we have node we have typescript but at runtime we don't need that understood this works exactly the same in c sharp and exactly the same in nearly old programming languages multi-stage doctor files let's quickly try that uh where is my docker build statement here is my docker build statement let's make it a little bit larger and let it run so i will let it run through and then we'll analyze the output and i have a mistake you see index.js not found let me see yes of course i forgot slash app see there was a mistake up here so let's try again oh i'm on windows currently we'll switch to linux later on good let's build it again nice if you take a look a close look at what docker has written here we will see let me scr here it is okay you see it started with the whole node alpine stuff then it was at one point in time here running the npm install and the typescript compiler and at the end it was copying you see that one copying the index.js from the build container into the final image and now we have an image that should be a little bit more powerful let's give it a try i'm going to run it again and do the port mapping this one good let's go into my browser and let's go back to our localhost 8080 and if i run it you see it says hello world so it correctly had my index.js created from typescript okay i don't have a favorite icon but that's not a problem i can prove it to you if i click here if i click here index.js uh-huh it doesn't find it yeah here it is i have my index.js so it is really exactly as i promised you to be and this is what you have to understand docker files are recipes for building images and docker files are not just copying the results docker files are much more they are to a certain degree a replacement to build servers you can ignore things like i don't know github actions or azure devops or jenkins you can do the same things in docker files docker files are absolutely not complicated they have from clauses work to your clauses copy clauses run clauses and maybe two or three other things more it's really simple the complexity in docker files are those scripts you have to learn how to automate your build process you don't have visual studio in a docker file doing all the magic for you you have to understand the command line interface the cli and you have to master it give me just one second and then i'll go to your questions okay if you take a look at angenix for instance if we take a look at the docker file for ingenix it's equally simple you see from some environment variables and then you have a single run statement but do you see where the complexity is this is a single statement in the dockerfile all that you see here is a single statement in the docker file up until here that's a lot of bash scripting going on and that's the complexity docker files writing docker files it's absolutely not complicated you have seen eighty percent of what you have to know to build docker file the complexity is mastering bash mastering build systems on the command line you have to know and master the command line in order to be a good doctor developer got it and at the end we have a docker image and we can run it currently i'm running it locally later on we are going to run it in kubernetes so we have questions i think we had a question here did we good this is essentially what we need to learn there is so much more to learn about docker docker is so so powerful but that's it just for me as a feedback who just learned something new who wasn't aware of some of the aspects that i just showed you here i want to check what you already know most of it okay good good good good so now you know this is level zero if you want to go into our school's kubernetes cluster or use a cluster a kubernetes system in azure or google cloud or even in docker desktop this is something you have to understand and you have to practice if you don't master docker you will have a very hard time in kubernetes okay so this in your mind this this is a level zero the absolute the the lowest level that we have and now we are going to move and take a look in cubing this what kubernetes is and what we can do with kubernetes good yep yeah maybe we will do one additional exercise it just came to my mind that i should really do that you don't have to follow along i will delete the code in a second just to show you that c sharp works exactly the same this is a c sharp cos so it is important so if we take for instance an asp.net core web api um put that somewhere i don't need that code later on and let's yeah let's take five and you see i enabled darker here see that one and it will darker if i create this project and i take a look at the generated docker file it really works exactly like we just learned it if you take a closer look here you will see that there is also a base image this is not that important i think you can currently ignore that one that's not that important i wanted to show you that one you have a build image which uses a base image which is which is called sdk this is the base image where all the c sharp compiler and the net core framework is installed and so on and so on and then we are building additional images on top of that we do a build we do a publish something like this and now comes the important one the final one the one that will be stored is no longer based on the sdk it's based on the base image which we can find up here and the base image is based upon asp.net so this is the run time image the runtime base image and this one is the compile time base image okay the compile time base image is huge but we don't care because we only need it to compile the software the runtime based image needs to be tiny because this is the image that we sent to kubernetes and we don't want to wait for installing and starting up our application understood yes i agree this docker file is a little bit more complicated you see they have multiple layers they have the base layer and then they have the no here the build layer and then they have the published layer microsoft is doing that to optimize caching of your images they want to speed up your build process but conceptually that's not though so important it's just an optimization step you could easily make this docker file simpler just as we did it with our typescript example but yes they they went the extra mile to make it a little bit more efficient but the concepts are exactly the same understood got it good and for any other programming language it would look like exactly the same great next step we were at level zero what you have to master in order to get going now we reach level two at level one okay the first level that's getting a little bit more interesting we need to somehow publish the images that we create because if we have the image on our local computer nobody can use it the kubernetes cluster can't know about it okay granted if you have a kubernetes caster running locally on your machine it would be able to fetch the images from there but in real life for instance the kubernetes cast in our school is not running on your local computer it's running somewhere down in the basement so therefore we have to send the image to the kubernetes customer in real life you have to send your results to your customers they want to run the software in their cloud in their data center understand what i mean and for that we are going to use our so-called image registry and one image registry you have already seen it it's the docker hub the docker hub this one is a registry and inside of this registry we have so-called repositories nginx would be a repository or do you want to use open jdk i don't know if you would want that but yeah no just kidding you can use that one open cdk or do you want to use mongodb voila here is mango do you want to use postgresql what uh here is postgres so remember the the terms registry is the server and the database where everything is stored for instance the docker hub repository is the product or project inside of the repository you have the different images and those images have tags see that one and these tags they describe the variant of the repository so you can in this case when we talk about postgres you have an image per version you see version 13 version 12 version 11 and so on and additionally for every version you have different base images for instance there is a base image based on debian a base image might be from ubuntu a base image might be from alpine so you have version x on debian version x on alpine version x on ubuntu and these are the images that you have got it so we have registry repositories images and what we we now have to do is we have to somehow publish our image into such a registry how do we do that well first thing we need to have a user account in our target registry in the docker hub you can easily create a user i'm already signed in here as you can see but you can go on hop to docker.com and get your own account you can just register there same is true for our schools cumulus cluster in our school we have our own docker registry so you do not need to copy the images out into the public internet but you can use registry.cloud.html.acat we have a dedicated registry for that so you have to register on cloud.tatia lending.acat and then you automatically also get a user for the registry you can also create your own registries out in the public internet for instance microsoft azure the cloud infrastructure from microsoft they have their own so-called azure container registry so with your credits that you get as a student you can create your own container registry and put your images there it depends it's just like a build server do you prefer jenkins do you prefer uh i don't know bitbucket github or maybe microsoft microsoft azure pipelines it depends they all do approximately the same but they have some strength some weaknesses it depends you have to choose okay in our case for this demo last demo before the break we will take our image and put it on the docker hub how do i do that well the first thing that i would have to do but i don't i will not do it because i already did it for you is docker login you just say docker login and at the end you specify the registry where you want to log in so if you check the website of the of our schools cloud you will see the name of the cloud so you have to specify docker login name of the registry and then you have to enter user and password if you enter nothing like that you log into the docker hub the docker hub is like github but not for source code but for images i already did that so i'm already logged in now we created an image together and this image was called hello njx right this was our image and now we want to publish this image how do we do that how would you publish code on github with git correctly what do you think is the statement how we can publish an image on daca docker push exactly let's try that docker push hello and genius it will fail see doesn't work you have to have very special credentials if you want to publish something a name like that that is only allowed for so-called trusted publishers so if you have a very very well-known product like node or or postgres then you get such a beautiful name if you are a usual guy like i am you have to use a prefix and the prefix has to be the name of the registry docker hub we can ignore it and your name your user account so what we have to do is we have to tag our hello engenix with my user account in the docker hub and then call it hello and jacks what ah sorry docker tag i forgot to write tag here and i should put the dash t here good now if i run the darker images again you will see that i now have two images hello and genix and rstrophex hello ingenix they have the same id you see it here so uh technically they are the same image but they have two names and the purpose of the second name is that i can publish it now i can go to docker push and here i just say rstropic slash nice do you see what we what we learn here mounted from mounted from and so on that means all the elements of mounted from are onion layers which are already on the docker hub and they don't need to be uploaded so the system only uploads the delta only those things that are not already in the internet and now we are done if we take a look here on my personal repositories here we are let's refresh that guy here you are now the world can see our beautiful single page app everybody who likes can now see docker pull and run the application that i created and this is the way how we publish images so that our kubernetes cluster which is in our basement in the school of our basement or maybe in your google cloud or your azure cloud how they can get your software so what you have to essentially to do is you write your code you write a doctor file that builds the app and creates the image and then you say docker push to the registry of your choice it might be docker hub it might be our schools registry it might be a registry in your own cloud and your kubernetes cluster will pull the images from there and run them so the second thing that you have to master in order to work with kubernetes is handling of registries there would be a lot more to say about registries but this is a starter course and i promised you a kind of survival kit for how to publish code to be run in kubernetes and this is exactly what we wanted to do questions all the steps that i just showed you are described step by step by step in the slide deck that you will get from me okay but it's really not complicated because all you have to remember is docker push nothing right question could you also set the repository to private and give only a few minutes yes yes yes if you want to do that with the docker hub you have to pay for it if you do that for instance in the azure cloud every docker repository every image repository is private by default and it's protected by azure active directory so if you think of a company context your employees like the user accounts of your employees are identical to the user accounts used for the registry so the developers use their developer account to sign in of course you need special credentials for the kubernetes cluster so that the kubernetes cluster can fetch images from the private registry for our school the registry is available over the public internet so you can push from it home but it's always private so you have to use credentials you cannot push in a in a public way yep but for the docker hub if you want to use it for free you have to make your image proper public otherwise they you have to pay there's a little bit of a difference with github it's like github was in the past yeah it's it's really hard for a docker as a company to earn money because their free tools are so good that most people are not likely to pay yeah further questions did you learn something new with these registries and repositories and images has it become clearer good nice so now we will do a break uh we will do a break let's see until when is the break i think it is now right or three minutes in three minutes so we have 15 minutes break and we will continue at 10 and then we will start with a completely new topic because now we want to switch towards kubernetes and there we will spend a little bit more time and really explore in detail what this community stuff is and how it works and what different concepts are i'm looking forward to that we will start at 10.
Info
Channel: Rainer Stropek
Views: 6,375
Rating: undefined out of 5
Keywords: Docker, Kubernetes, Container, DevOps, C#, ASP.NET, HTL Leonding
Id: 30VTF0PlEPQ
Channel Id: undefined
Length: 42min 5sec (2525 seconds)
Published: Fri Jun 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.