Containers? So What? Docker 101 Explained - Computer Stuff They Didn't Teach You #8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

r/asmr


For mobile and non-RES users | More info | -1 to Remove | Ignore Sub

👍︎︎ 1 👤︎︎ u/ClickableLinkBot 📅︎︎ Aug 25 2020 🗫︎ replies
Captions
hey friends i'm scott hanselman this is i guess another episode of things they didn't teach you in school computer things that they didn't teach you in school in this one we're going to talk about containers now this is not a docker 101 class it's not a everything you need to know about docker it's more of a docker i don't really understand what containers are like i use them every day and i kind of guess why they make things better but i don't fundamentally understand what's happening so as with all my videos why don't we try to explain it in one take with real code and real examples and i'll do my very best to get you to understand containers so what that was probably that'll probably be the thumbnail for the video the youtubers they do thumbnails that'll be my thumbnail docker so what so let's do this as we always do let's go to the command line now for context i'm using windows 10 i'm using docker that i've installed in this machine and i'm using wsl 2 the windows subsystem for linux if you have a mac or a linux machine install docker and do the same if you have windows install wsl2 and docker i may do a more detailed video on that but i believe i've done a couple of videos to explain that as well as a number of blog posts so let's just forget about the details of whatever operating system you're running let's get back to the basics i'm in my terminal and i open up my directory and i've got some stuff here now i wrote earlier just a couple minutes ago this hello world c application it's got standard i o and uh it's got a main and it says hello world and then it leaves it prints something out presumably to the console and then i went and i ran gcc which compiles that c code and i put in static linking to make sure that i got everything that i needed in my little hello world and now i have here this hello world you see that that's green and i can go here and let's look at that you see that it's marked as executable that's what that x means that folks can run it it's got a little star by it and then add some color there and i'll say hey hello world this is on my linux machine now again i'm using wsl on windows but this is linux real linux real linux kernel so i ran that and it works i can run it again i can close my linux machine i can shut it down entirely and i can open it up again pretend that i've rebooted okay i'll go back to that folder and that file is still there this is a persistent installation of linux those files stick around they're not going anywhere they're always there because i've set them up on the disk now you can mess up a computer you've probably done that before if i made a mistake if i deleted that if i renamed it if i put in the wrong location if i did some installation and i messed up my machine that could be really frustrating sometimes you'll go to work at a company and they'll give you a document and they'll say here's the instructions on how to set up your machine even more they might say here are the instructions for how you set up the server install nginx install apache install python install php put net here you set that whole thing up and that's annoying because if you get it wrong then you're gonna have configuration issues and when you're dealing with 1 2 8 64 256 computers if one of them is wrong then you have configuration issues so then people came up with the idea of virtual machines machines inside of machines where you would actually lie to your your machine the guest lives in the host and it has everything set up for it and virtual machines could be paused and stopped and started and that was really cool but virtual machines are kind of heavy because let's look at a diagram actually one of our friends in the comments suggested that rather than using paintbrush i consider using this draw dot io so i spend a few minutes and i familiarize myself with draw diodes so let's say you have your your hardware let's see if this is gonna work out nicely for us we'll try draw.io to make my boxes instead of what i like to use which is paintbrush or maybe windows whiteboard microsoft whiteboard now you've got your host operating system you've got your hardware you get your host operating system and then when you're doing things with a virtual machine let's say you're running a couple of virtual machines right we'll say this is vm1 and you need an os you need the whole operating system for that vm2 the os and that might me that might be a lot of space that might be i don't know 20 gigs could be however big the operating system is if it's windows or linux or ubuntu you need a bunch of stuff and then here's the fun part you take your application let's say your application is hello world and you put that on top and then you have another application over in here maybe this is your website in order to call hello world i'm gonna go and call some system calls some calls that are available in the operating system they're probably available in this host operating system it knows how to do things like console.writeline but instead i'm going to have this whole operating system that i'm carrying around with me that might mean 10 gigs it's certainly not small maybe if it's a small linux it could be a couple 100 megs but it's a size and it's duplicative meaning there's duplication i have my little hello world app my virtual machine and then between these virtual machines and this host operating system we have a thing that's called a hypervisor a hypervisor is the thing that does all of the lying if you've ever heard of vmware or hyper-v or virtualbox they're all hypervisors this is hello world there's a virtual machine with its own operating system that's being managed by the hypervisor that lives inside of a host that sits on top of hardware that's kind of a lot remembering that the host operating system probably has the things we need if you were to make a list of the system calls that you would want to run most let's say web applications you'd need opening up sockets and writing out to consoles and writing to files and you know doing stuff like threading you'd have a list of some system calls what if you could get rid of the hypervisor get rid of the virtual machines bring these closer but you don't really want them to be running just like regular apps remember i can mess up my host operating system i don't want them that close so i'm going to need a little bit of help here so what i'm going to do is i'm going to put docker this will be the doctor demon i always forget how to spell demon there and then these apps are going to need a little bit of help they're going to need some extra stuff because every app is different they're going to carry those things along with them they're going to carry their binaries and their libraries just the ones that they need this is super important we want these applications i want hello world to have the least i want hello world to have nothing if there's a library i don't need or a function i don't need to call that i won't even include it won't even be involved and then the little tiny docker daemon it's very thin it's a nothing little thing here will broker these calls this relationship so these are much lighter much smaller now with a virtual machine usually someone hands you a virtual hard drive here is the hard drive of this machine it's the here's a giant file as much as they'd said here's a disk drive that i'm going to hand you except it's virtual okay so how do we describe these things so i can hand you something smaller all right let's go and talk about that let's say i'm in a folder here called containers all right what i'm going to do is we'll try to show how this works in the absolute nothing example let's use visual studio code to do our editing and i'm going to open up a docker file and in this docker file i'm going to have nothing and what i'm going to do is i'm going to say oops i'm going to say from scratch we're going to start from scratch literally like when you bake a cake start from scratch now i want to run my little hello world application now remember a hello world application in c can be quite small but in this case i did that static remember i said dash static i brought the libraries i wanted so this file is a little bit bigger than it needs to be it's about a meg probably could have made it smaller but bear with me i have a little tiny linux hello world file here and i'm going to say from scratch let's go and copy hello world into the root of our new little container not a virtual machine and then we're going to run when we start up our container we're going to go and run hello world and it looks like a batch file or shell script but this is docker language it's something on the left and something on the right these are kind of verbs these are the nouns so from scratch copy my file into this new thing that doesn't exist yet so look at my folder all we have is a docker file and we have hello world all right let's clear that out and let's go and say docker build i'm going to say docker build i'm going to tag it give it a name i'm going to build a container that's called my test and i could put a version at the end of it we'll just call this one the latest this is really really important notice that there were three lines in that docker file so there's three layers here there's three things that are happening there's moments where something happens and each one gets a little name so they all got little little numbers so there's a docker image that just got built and i can go and type docker images and i can see there was just 30 seconds ago look how tiny it is it's just about the same size as that hello world binary because the hello world binary is inside there now i said from scratch didn't say from ubuntu or from linux or whatever i just said from scratch that just assumes the absolute base linux machine posix what they call posix or the base nothing just the minimum amount but if you go back here remember that we said we have a host operating system so i really have basically nothing in here a megabyte let's make that really small just the least and the doctor damon's going to manage that so now i could say docker run all right and we're going to go and we called it my test i think we call it latest all right and it says hello this is really important it comes and it goes starts up and then it ends that's really important because docker containers or any kind of container are ephemeral they start up and they stop so what does that mean well there's another tiny little container i used from scratch it's another tiny tiny container in the world and that container is called busybox busybox is this tiny tiny little base image these are all base images that you can start from you can pick all kinds of base images i'll pick some different ones but busybox is the smallest possible unix thing that you could ever start with okay what we're going to do is we're going to change our docker container instead of building from scratch i'm going to say from busybox we'll get rid of hello world for a second all i'm saying is from busybox it's again just the unix utilities that you need it's really nothing let's go and do this we'll build it again it's like compiling a some source code now look i don't know if you saw that but it said pulling it didn't have it locally and it pulled it down and it said i'm going to go and get an image for busybox and i'm going to say docker images now i have the one that we built earlier i'm sorry oh i deleted it so that's a good point notice here remember i made my test before i should have given it another version number now we've got busybox latest only a megabyte i didn't put hello world there my hello world one is just kind of orphaned at this point that's that intermediate one let's go and do this let's go and say [Music] run right look at that i'm at a different prompt what's going on here where are we what can we do i can't really do anything the reason i can't is because i didn't tell it to be interactive i wanted to be interactive and let me see the outputs i'm going to add that i now i can type i'm in a a little busy box i've got another little linux right here so let's make a file here is my new file there it is i see that i could make a hello world dot text so i've just put oops just put two files oops oops there we go there we go i've got two files here in my busy box so i'll exit now we're back out where we were before let's run fizzy box again and go see if our stuff is in there it's not really important when the docker container stopped it's gone gone forever very important so i'll need to have those files be stored somewhere else because this is a perfect machine set up exactly as i described it how is it set up it's set up exactly as i described it in my docker file now i could have said let's go ahead and exit docker run busy box latest here pull that down from the on high you notice it didn't have to because it was already local remember we have docker images so i've got the latest one it looks like no one's even touched it for three weeks but i wanted to build on top of it so now i could go in here and we could say our hello world this way because izzybox has a little bit more than scratch does i'm going to say hey echo so we'll go back do a build i don't need to run it interactively oops i want to run my test the latest and it should say welcome to docker oh it's mad at me because of that i don't even need that there you go cool so this allows me to run a thing without messing up my local system at all i could do something a little bit more complicated why don't we do this rather than building from scratch or building from busybox we could build from let's say ubuntu which is kind of a little bit more legit a little bit bigger a little bit more functionality and then what if we go and we copy this hello world over there would that work we're going to copy it from our local machine inside the container that we built and then run it go back we'll build it and look at this watch look at that layers so that means that the ubuntu base um the ubuntu-based docker file has other layers before it and it's the layering that makes things so interesting you start from a base image you say you know i want my application to start from ubuntu and then i'll add something and i'll add something i'll add something and each layer gets cached it gets cached so it's local and it runs fast these binaries and limes libs and then build on other images that build on other images that build on other images and once they're cast locally they don't have to get built every time you run them they're super fast you completely design these from scratch the way that you want and now let's go and run this one or ubuntu one should say hello the only difference here was that we started with ubuntu now i could say docker run and i'm going to say remove it when you're done boom to the latest and now look at that a little bit more that i had when we were looking at busy box because i've got more stuff in there more ubuntu stuff now if i wanted to do something like this like a website i could find a base image that included that and there is one that's called nginx now this is where things get interesting if you follow with me so far remember we talk about layering so here's their actual nginx web server docker file from the debian base using the buster slim version i called mine the latest they're going to go and they added a little label so we know who does it and they put some environment variables and they're going to look at this they're going to start setting it up they're saying that this image is just one slice of what we need after we get that image we're going to update some things we're going to add some users we're going to set up our server get some nginx packages do all this stuff all of this could run on my computer and i can do the build or we could just go and get that image from the docker hub the docker repository and that docker repository is a place where all of these docker files are stored so if we go up here to docker hub all these images are up here for me to pull down some are big some are small some are base images for you to start from let's just see if we can say hello world not with text now but instead with a web server what we're going to do is we're going to say docker run remove it when you're done we'll name it docker nginx you can name it whatever we want and then this is going to be interesting we're going to say i want to set up a port i'm going to say like well port 81 on my computer is going to be port 80 inside the container because the container is a little little operating system of its own so it's got a little port sticking out oops that's not a very good port that's going to be port 81 we're going to map that port to port 80 on my host operating system then i can go and connect maybe a web browser maybe this will be my web browser so this here is port 80. this here will be localhost 81 on my browser okay so let's go back out to our command line and then we'll just name it nginx as well now watch it as it pulls down this is important see how many layers are unable to find image oops now look at this i've got a little error great thing this is a really important deal sometimes when you make a mistake and you're beginning your career you can get overwhelmed you can stay at it for a while but if you actually read the error message you can see that i've actually forgotten to put in a hyphen here that hyphen is a switch in this case for port they were telling me what was wrong there okay unable to find image now look watch one two three four five layers 30 megs 30 megs look how fast that was okay cool so something's happening this was the pulling of the images now it's actually running let's go over to our browser let's go to localhost 81. look at that if you see this page the nginx web server is working let me put that over here on the right and i'll put that on the left i'm going to hit ctrl c i'll hit refresh still running we can run docker ps we can see that we've got some containers that are running this one that we just made called nginx is running remember that mapping i mentioned we moved port 81 on the outside points to port 80 on the inside i've got this nice little thing now i named mine i named it docker engine x you don't name it you'll just get a cool name of an adjective plus a scientist if i say docker stop i can give it a container id this is the unique idea of my container i'll just pick the first couple of numbers because that's unique enough there's no other containers that are named 1c and then let's go and hit refresh see how it's not working gone that's cached see can't reach the page docker ps we've got busybox kind of sitting around still running probably need to get rid of that and we've got another one that we made that's running as well we'll want to shut that down as well so now if i type docker images these are the list of the little images notice that none of these are really large and all the layers in between some are named some are not dizzybox nginx ubuntu the one that i built my own so why is this interesting well here's my podcast i have a podcast you can check it out it's at hanselminutes.com i like it very much you should check it out i got lots of cool and interesting fresh faces including people that have worked at docker this website is run by me and when i build it i've got my.net code here i've got my tests and i've got a docker file so let's look at that docker file let's open that up in vs code this is a real world kind of production docker file here i see the from using the microsoft container registry and i pull out of the image that microsoft makes of net give it a name decide that we're going to work in a certain folder i start copying my code into it this is cool because you can use docker files to go and compile your code as well this means that i don't even need to have.net or go or rust or whatever i have on my local machine in this case here all the code is going to compile and run inside that file then we actually compile and build in our docker file each layer gets cached i run my tests i ultimately publish a release version of my website then i get another docker file publish the version of my website that i'm going to put into production and then launch it all in one call now i made a little script to do that but i'll show you what it is it's not really rocket surgery remember that docker build command that we did before docker build and then we tag it let's try typing that without running the script all right now this will be slow one time because i cleared out my cache and i wanted you to see the layers the.net sdk containers brings down the images in layers pulls them down extracts them now once it's done that once it'll be fast that's what's so nice about this didn't bring down 5 gig or 30 gig operating system wouldn't bring down too much just enough to do the job and again every step gets a name and a layer and then if it notices it doesn't need to do it again like for example this step 23 here is quite a quite a slow step see it took 11 seconds you know i don't want to do that every time so the next time i run this if it doesn't detect that that layer needed to change the layer will get reused and that means that docker files can get built really quickly and then they also start up very very fast because all those images are pre-built we've just built my application now we're actually going to run our unit tests in docker now what's great about containers is that i can just hand this to you i could give you the docker file or my image and you could just do it if i said to you hey go and run docker run nginx hit enter you try this line on your system it'll work because you'll have docker installed it will pull this down you'll get the exact same thing and then you'll see a nginx web server startup okay mine is just built it called itself podcast latest i'll say docker images you can see podcast is right here my whole podcast site is only 256 megs then i can go and run it let's look at what that looks like very similar to the way we ran nginx before okay let's go and run that so we'll run that container so quickly that starts up all right let's go and hit localhost look at that there is the local version of my podcast running right there let's stop it run it again see how fast that starts stop it let's build it again see how fast that went because nothing changed things get cached each layer gets cast if we go back here my website layer had 23 different layers that each individual step each individual moment some of them might be individual doctor files some might be steps within a docker file there's a whole rich ecosystem for you to build on it's much lighter weight much easier to work with now again that the data is erased the file system that gets changed if anything gets changed it disappears when the docker file stops running there's nothing happening in there at all i want to remind you if we go and run a docker file look in here maybe i'll make a new file you can see it i leave i come back it's gone it's like pulling the plug and plugging it back in it wakes up and it's a fresh baby just born it doesn't know it doesn't know what's going on but you can mount volumes you can have external storage or in the case of my podcast i use an external database so it's okay that my application starts up and it doesn't know anything that is the power of containers that's the answer i think in one take it looks like it took me about 30 minutes sorry that was a little long of containers so what i know i was moving fast but this should give you the fuel that you need to go and learn about docker and learn about containers and why they're interesting why they're fun and why they're useful fundamentally they are a useful thing and i think the future of how we're going to build deliver ship and run software and another time perhaps we'll talk about kubernetes kubernetes or what are called container orchestrators because one container is just one musician in an orchestra and you need a conductor to orchestrate all of them together so maybe we'll do another one about that again if you find these videos useful please leave a comment smash that bell subscribe and honestly the best thing that you can do to help me out is let me know what you think i should talk about next and whether or not you find these useful certainly tell a friend that would be great if they came over and subscribed as well thank you very much this has been computer stuff that they didn't teach you in school
Info
Channel: Scott Hanselman
Views: 98,333
Rating: 4.9759278 out of 5
Keywords:
Id: 0oEsMwSxBsk
Channel Id: undefined
Length: 31min 13sec (1873 seconds)
Published: Sun Aug 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.