YTread Logo
YTread Logo

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

May 31, 2021
Hello friends, I'm Scott Hanselman, this is, I guess, another episode of things

they

didn

't

teach

you in school. Computer, things

they

didn

't

teach

you in school. In this one we are going to talk about

containers

. Now this is not a Docker 101 class, it's not all you need to know about Docker, it's more like Docker. I don't really understand

what

containers

are like. I use them every day and I guess why they make things better, but I don't fundamentally understand it. What's going on with all my videos, why don't we try to explain it in one shot with real code and real examples.
containers so what docker 101 explained   computer stuff they didn t teach you 8
I will do my best to make you understand the containers. What was probably that will probably be the thumbnail of the video youtubers make thumbnails that will be my thumbnail

docker

so

what

, let's do this as usual, let's go to the command line now to see the context. I am using Windows 10. I am using

docker

. I have installed it on this machine and am using wsl 2, the Windows subsystem for Linux. If you have a Mac or Linux machine, install Docker and do the same if you have Windows. Install wsl2 and Docker. I can do a more detailed video on that, but I think I've made a couple of videos to explain that, as well as several blog posts, so let's forget the details of whatever operating system you're running.
containers so what docker 101 explained   computer stuff they didn t teach you 8

More Interesting Facts About,

containers so what docker 101 explained computer stuff they didn t teach you 8...

Let's go back to basics. I'm in my terminal and it opens my directory and I have some things here that I wrote a couple of minutes ago. This Hello world C application has i o standard and it has a main and it says Hello world and then exits, presumably printing something. to the console and then I went and ran gcc which compiles that c code and put in static links to make sure I had everything I needed in my little hello world and now I have here this hello world you see it's green and I can go over here and let's see that it's marked as executable, that's what it means x people can run it, it has a little star and then add a little bit of color there and I'll say hello, 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 it and it works.
containers so what docker 101 explained   computer stuff they didn t teach you 8
I can run it again. I can shut down my Linux machine. I can turn it off completely and I can open it. come back up pretend I rebooted fine I'll go back to that folder and that file is still there this is a persistent installation of Linux those files stay they don't go anywhere they're always there because I have set them on disk now you can mess up a

computer

probably have you done that before if i made a mistake if i deleted it if i renamed it if i put it in the wrong location if i did some installation and ruined my machine that can be really frustrating sometimes you will go to work at a company and they will give you a document and they will tell you here are the instructions on how to configure your machine, even more they might say here are the instructions on how you configure the server you install nginx you install apache you install python you install php you put net here you configure all that and that is annoying because if you make a mistake you will have problems of configuration and when you deal with 1 2 8 64 256

computer

s, if one of them is bad, then you have configuration problems, then people came up with the idea of ​​virtual machines, machines within machines, where you would actually lie to them to your machine, the guest lives on the host and has everything configured for it and the virtual machines can be paused, stopped and started and that was really cool, but the virtual machines are a little bit heavy because let's see a diagram.
containers so what docker 101 explained   computer stuff they didn t teach you 8
In fact, one of our friends in the comments suggested that instead of using a brush, consider using this drawing point io. so I spend a few minutes getting familiar with drawing diodes, so let's say you have your hardware, let's see if this is going to work well for us, we'll try draw.io to make my boxes instead of what I like to use. which is brush or maybe windows slate microsoft slate now you have your host operating system, you have your hardware, you get your host operating system and then when you do things with a virtual machine, let's say you're running a couple of virtual machines, okay , we will say this is vm1 and you need an operating system, you need the entire operating system for that vm2, the operating system and that could be a lot of space, it could be, I don't know, 20 gigs could be no matter how big the operating system is, If it's Windows, Linux or Ubuntu, you need a bunch of

stuff

and then here's the fun part: you take your app, let's say your app is Hello World, you put it on top of it and then you have another app. here maybe this is your website to call hello world.
I'm going to go and call some system calls some calls that are available on the operating system are probably available on this host operating system that knows how to do things like the console. writeline but instead I'm going to have this whole operating system that I carry with me, which could mean 10 gigs, it's certainly not small, maybe if it's a small Linux it could be a couple of 100 megas, but it has a size and it's duplicative. which means there is duplication, I have my little Hello World application, my virtual machine and then between these virtual machines and this host operating system, we have something called a hypervisor.
A hypervisor is what lies, if you've ever heard of it. vmware or hyper-v or virtualbox they are all hypervisors, this is hello world, there is a virtual machine with its own operating system that is being managed by the hypervisor that lives inside a host that sits on top of the hardware, that is something to remember a lot. the host OS probably has the things we need if you were to make a list of the system calls you would most like to run, say web applications, you would need to open sockets and write to consoles and write to files, and you know that, by doing things like threads, you would have a list of some system calls.
What if you could get rid of the hypervisor? Get rid of virtual machines. Zoom them in, but you don't really want them to run like normal apps. Remember? it can mess up my host OS, I don't want them that close so I'm going to need a little help, so what I'm going to do is put in Docker, this will be the demon doctor. I always forget how to spell daemon at that point these apps are going to need a little help they're going to need some extra things because every app is different they're going to carry those things with them they're going to carry their binaries and their libraries only the ones that need this is super important we want these applications I want hello world to have the bare minimum I want hello world to have nothing if there is a library that I don't need or a function that I don't need You don't need to call that I won't even include it it won't even be involved and then the little dockable daemon is very thin, it's nothing, a little thing here will mediate these calls, this relationship, so they are much lighter, much smaller now with a virtual machine normally someone hands you a virtual hard drive here is the hard drive of this machine here is a giant file as much as they had said here is a disk drive that I'm going to give you except it's virtual okay so how?
Do we describe these things so we can give you something smaller? Okay, let's talk about that. Let's say I'm in a folder here called containers. Well, what I'm going to do is try to show how it works. in the absolute nothing example, let's use the Visual Studio code to do our editing and I'm going to open a docker file and in this docker file I'm not going to have anything and what I'm going to do is go To say Oops, I'm going to say from scratch, We're going to start from scratch, literally, like when you bake a cake, you start from scratch.
Now I want to run my little Hello World application. Now remember that a Hello World application in C can be quite a lot. small, but in this case I made that static. Remember I said static script. I brought the libraries I wanted, so this file is a little larger than necessary. This is a mega. I probably could have made it smaller, but bear with me. I have a Here's a nice little Linux Hello World file and I'm going to say from scratch, let's copy Hello World to the root of our new little container, not a virtual machine, and then we'll run it when we start our container.
I'm going to run hello world and it looks like a batch file or a shell script, but this is the docker language, it's something on the left and something on the right, these are kind of verbs, these are nouns, so from zero copy my file into this. something new that doesn't exist yet so look at my folder all we have is a docker file and we have hello world okay let's clear that up and say docker build I'm going to say docker build I'm going to label it give it a name . I'm going to build a container that will be called my test and I could put a version at the end.
We'll call this the last one. This is actually very important. Notice that there were three lines. in that docker file, there are three layers here, there are three things that are happening, there are times when something happens and each one gets a little name, so they all have little numbers, so there is an image of the docker It just got created and I can go and write docker. images and I can see that there was just 30 seconds ago, look how small it is, it's about the same size as that hello world binary because the hello world binary is inside, now I said from scratch, I didn't say from ubuntu or from linux or whatever which I just said from scratch, that just assumes the absolute base of the posix Linux machine, what they call posix or the base, nothing, just the minimum amount, but if you come back here, remember that we said we have a host operating system, so basically I have nothing in here a megabyte let's make it really small to the minimum and doctor Damon will take care of that so now you could say Docker runs fine and we're going to go and call it my test I think we call it last , okay and says hello, this is really important, it comes and goes, it starts and then it ends, that's very important because Docker containers or any type of container are ephemeral, they start and stop, so what does that mean ?
Well, there is another small container that I used. from scratch, it's another tiny container in the world and that container is called Busybox Busybox is this tiny base image. These are all base images that you can start from. You can choose all types of base images. I'll choose a few different ones, but togetherbox is the smallest possible Unix element you could start with. Well, what we're going to do is change our docker container instead of building it from scratch. I'm going to say that from Busybox we will get rid of it. Hello world, for a second, everything I'm saying is from Busybox.
Again, it's just the Unix utilities you need. It's really nothing. Let's do this. We will build it again. It's like compiling source code. Look, I don't know. if you saw that but it said pull it didn't have it locally and it pulled it down and it said I'm going to go get an image for Busybox and I'm going to say Docker images now I have the one we built it before sorry oh I deleted it so which is a good warning here. Remember I did my test before. I should have given it another version number. Now we have the latest version of Busybox, just one megabyte.
I didn't put Hello World there. my hello world one is a little orphaned at this point that's the intermission let's go and do this let's go and say run well look at that I'm in a different message what's going on here where are we what can we do I can I don't really do anything, the reason why The one I can't is because I didn't tell it to be interactive. I wanted to be interactive and let me see the results I'm going to add. Now I can write. I'm in a. busy little box I have another little Linux here, so let's make a file here is my new file there.
I see I can do a hello world dot text so I just put oops I just put two files oops there we go there we go I have two files here in my busy mailbox so I'm going out now we're going back to where we were before let's run fizzy box again and see if our

stuff

is there it's not really important when the docker container stopped it's gone forever it's very important so I'll have to store those files somewhere else because this is a perfect machine configured exactly as I described it, how is it configured? 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 the most recent busy box here, pull it down from the top, you'll notice it wasn't necessary because it was already local, remember we have images of docker, so I have the last one, it looks like no one has touched it for three weeks, but I wanted to build on it, so now I could come in here and we could say hello world like this because izzybox has a little more than scratch.
I'm going to say hello echo, so we'll do a build again. I don't need to run it interactively. Wow, I want to run my latest test and it should say welcome to Docker. Oh, he's mad at me for that. You don't even have to go fine, so this allows me to run something without messing up my local system. I could do something a little more complicated. Why don't we do this instead of building from scratch or building from Busybox? we could build from, say, ubuntu, which is a bitmore legit a little bit bigger a little bit more functionality and then what if we copy this hello world there?
Would it work? We will copy it from our local machine inside the container that compiled and then run it come back, we will compile it and look at this watch, look at those layers, that means that the ubuntu base, the ubuntu based docker file, has other layers before it and they are the layers the ones that make things so interesting. starting from a base image you say you know I want my app to start from ubuntu and then I'll add something and I'll add something I'll add something and each layer is cached, cached, so it's local and it runs fast these binary libraries and limes and then they are based on other images that are based on other images and once they are streamed locally they don't have to be compiled every time you run them, they are super fast, you design them completely from scratch the way you want and now we are going to run this 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 delete it when you are.
Done boom to last and now look at that a little bit more than I had when we were looking at the busy box because I have 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 includes that and there is one called nginx. This is where things get interesting if you follow me so far. Remember we are talking about layers, so here is the docker file of the actual nginx web server from the Debian base using Buster Slim. The version I called mine is the last one they're going to use and they added a little tag so we know who does it and they put in some environment variables and they're going to see this and they're going to start configuring it.
We are saying that this image is just a portion of what we need after getting that image. We will update some things. We will add some users. We will configure our server. We will get some nginx packages. Let's 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 Docker Hub image, the Docker repository and that Docker repository is a place where all these Docker files are stored, so yeah let's go up here in Docker Hub all these images are here for you to download some are big some are small some are base images for you to start with let's see if we can say hello world not with text now but with a web server what we are going to do i.e. docker run, delete it when you're done, we'll call it docker nginx, you can name it whatever we want and then this will be interesting, let's say I want to configure a port.
I'm going to say that port 81 on my computer will be port 80 inside the container because the container is its own little operating system, so it has a little port sticking out. Wow, that's not a very good port, it's going to be port 81, let's map that port to port 80 on my host OS, then I can go and connect, maybe a web browser, maybe this is my web browser, like this which this is port 80. this here will be localhost 81 in my browser, okay, so let's go back to our command line and then we'll call it nginx and now watch it as it deploys.
This is important to see how many layers they can't. find the image Oops, now look at this, I have a little mistake. Wow, this is a really big deal. Sometimes when you make a mistake and you're starting your career, you can feel overwhelmed, you can stay that way for a while, but if I actually read the error message. You can see I actually forgot to put a dash here. That script is a switch in this case for the port. They were telling me what was wrong there. Okay, I can't find the image. Now look, look one, two, three, four. five layers 30 megs 30 megs look how fast it was okay cool so something is happening this was the extraction of the images now it's actually running let's go to our browser, let's go to localhost 81. look at that if you see this page, the nginx web The server is running, let me put it here on the right and I'll put it on the left.
I'm going to press Ctrl C. I'm going to press Refresh. It's still running. We can run Docker ps. We can see that we have some. The containers running this one we just created called nginx are running. Remember the mapping I mentioned, we moved port 81 on the outside points to port 80 on the inside. I have this pretty little thing, now I named mine, I called it a plug-in motor. x you don't name it, you'll just get a cool name from an adjective plus a scientific one. If I say Docker Stop, I can give you a container ID.
This is the unique idea of ​​my container. I'm just going to choose the first couple of numbers because that's unique enough, there are no other containers that are called 1c and then let's go and hit refresh, see how it doesn't work, it's cached, I can't access the page docker. PS we have a busy box still running, it's probably needed. to get rid of that and we have another one that we created that's also running, we'll want to close that as well, so now if I write docker images, this is the list of the little images. Note that none of these are really big and all the middle layers, some are named, some are not dizzybox nginx ubuntu, the one I built myself, so why is this interesting?
Well, here's my podcast. I have a podcast you can check out. It's on hanselMinutes.com. I like it. You should really take a look at it. I have a lot of new and interesting faces, including people who have worked on Docker. I run this website and when I build it I have my.net code here. I have my tests and I have a docker file, so let's look at that docker file, let's open it in vs code. This is a type of real-world production docker file. Here I see the use of the Microsoft container registry and pull it out of the Microsoft image. does net give it a name decide that we are 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 also this means I don't even need to have.net or go or rust or whatever I have on my local machine.
In this case all the code will be compiled and run inside that file, then we will compile and compile into our docker file, each layer will be cached and run. In my tests, I finally publish a release version of my website, then get another docker file, publish the version of my website that I'm going to put into production, and then launch it all in a single 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 the Docker Build command we did before Docker Build and then we labeled it.
Let's try to write it without running the script. Now this will be slow once because I cleaned it up. my cache and I wanted you to see the layers. the.net SDK containers pull down the images in layers pull them down extract them now once done that once will be fast that's the good thing about this it didn't shave off 5 gigs or a 30 gig operating system wouldn't shave off too much enough to do the job and again each step gets a name and a layer and then if you realize you don't need to do it again, like for example this step 23 here is A pretty slow step.
Look, 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 layer needed to change the layer, it will be reused and that means. that Docker files can be built very quickly and then they also start very, very quickly because all those images are pre-built. We just created my application and now we are going to run our unit tests in Docker. What's so good about containers? I can give you this. I could give you the Docker file or my image and you could do it if I said, "Hey, go run Docker Run nginx." Press Enter.
Try this line on your system. It will work. because you will have Docker installed, you will deploy it and get exactly the same thing and then you will see an nginx web server starting. Well, mine was just built, it was called the latest podcast, I'll say Docker images that you can see. The podcast is here, my entire podcast site is only 256 megs, so I can go and run it, let's see how it looks, very similar to the way we ran nginx before, okay, let's run it, so we'll run that container so fast . that starts fine, let's go and hit localhost, look there's the local version of my podcast running right there, let's stop it, run it again, let's see how fast it starts, stop it, let's build it again, let's see how fast it was because nothing changed the things. is cached, each layer is streamed if we come back here, my website layer had 23 different layers in which each individual step, each individual moment, some of them could be individual medical files, some could be steps within a docker file, there is a complete and rich ecosystem for you to create. it's much lighter, it's much easier to work with now again the data is erased, the file system that is changed, if something is changed it disappears when the docker stops running, nothing happens there, I want to remind you if come on. and run a docker file look here maybe make a new file you can see it I leave it I come back it's gone it's like unplugging the plug and plugging it back in it wakes up and it's a newborn baby that doesn't I don'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 for my app to start and not know anything. power from the containers that's the answer, I think in one shot it looks like it took me about 30 minutes, sorry it was a little long for the containers so I know I was moving fast but this should give you the fuel you need. you need to go and learn. about Docker and learning about containers and why they're interesting, why they're fun and why they're fundamentally useful, they're a useful thing and I think the future of how we're going to build, deliver and run software and another time maybe we'll talk about kubernetes kubernetes or what they call container orchestrators because a container is just a musician in an orchestra and you need a conductor to orchestrate them all together so maybe we'll do another one on that again if you find these videos useful please leave a comment smash that bell subscribe and honestly the best thing you can do to help me is tell me what you think I should talk about next and if you find them helpful or not, definitely tell a friend.
Great if you guys would come and subscribe too. Thank you so much. This was computer science material that they didn't teach you in school.

If you have any copyright issue, please Contact