Transcript from the "Docker Images with Docker" Lesson
>> The next section we're gonna be talking about is running Docker Images with Docker, and you'll see that this is about 100 million times easier. So the first thing that we're gonna learn how to do is use Docker run. So Docker does quite a few different things for you.
[00:00:20] But honestly, for most people that I imagined will be taking this course, Docker run is the part that you actually care most about. Most of us probably won't be building containers by hand and managing all that kind of stuff. Typically, a lot of times the ops teams will handle that.
[00:00:34] And as like developers, you might be more interested in running containers than actually building them. But who knows? I'm not your mom, you can do whatever you want. So we're gonna do this. We're gonna run a Docker alpine container. And we've kinda been doing this already and I've just been kind of like saying don't peek behind the curtain yet.
[00:00:54] Now we're gonna peek behind the curtain. So I'm gonna come back here to vs code. I'm here in MacOS. And I'm gonna say Docker run--interactive--tty. Yep, I can can ever remember, run. And then at that point, once you've done this, you can run basically anything that you want to run.
[00:01:22] In this particular case, I want to run let's say, Alpine, right, which is a flavor of Linux. I'm gonna say Alpine. And this is what's coming off of Docker Hub right? Which is what we were looking at previously. Now I could say Docker run Alpine, and this would implicitly run :latest, right?
[00:01:41] Which would be the latest version that they've published, right? Now I'm going to suggest that you never use latest. It's just kind of a foot gun. And in particular, since people will be watching this course later, I'm gonna be very explicit about the versions that I'm using, cuz I want these demos to work for you with code that I've written.
[00:02:00] So I'm gonna say 3.10, which I think is one of the more recent ones. Now if you haven't installed this or pulled this previously, it'll take a second say downloading all these layers and it'll take a second. I think Alpine, the final image ends up being like, 20 some odd megabytes, something like that.
[00:02:20] And that's the point of alpine. Alpine is like a tiny Linux, it has the barebones and the necessity for running web services, stuff like that, and nothing else. And I think we'll get more into that later there's a whole section just talking about Alpine. But again, you can say cat, etc, issue, and we can see, welcome to Alpine Linux 3.10, that's expected given that we asked for Alpine 3.10.
[00:02:44] Now this probably seems a little bit easier than what we were doing before which is like exporting and doing control groups and on shares. This just did all of that for you. So we just spent, I don't know, couple hours going over what one command does for you.
[00:03:02] I don't know how you feel about that. But that's honestly what happened. This is a little bit easier to do, a little bit easier to remember than all the control groups and the shares and stuff like that. All right, so I'm gonna hit exit. That'll exit us out of the container.
[00:03:17] And again, to remind you, containers are made to be, the entire idea behind Docker containers in general is that they're made to be spun up and destroyed. And frequently do that. The tournament uses their ephemeral rights but they do not last very long. So nothing like when you are dealing with Docker containers, if it is not in the Docker file, just assume nothing is permanent.
[00:03:38] So that is kind of a good way of thinking so if you are running a database without a volume and ignore volumes for just a second. But if you say, if you're running all of your databases with Docker, as soon as you close down those Docker containers, your data goes with it.
[00:03:55] So you have to mitigate that kinda stuff. So I'm gonna show you just a little shortcut, because --interactive-tty, kind of hard to remember it. You can just say docker run -it, right? And what this -it does is it puts you interactively into the container. So if I say alpine:3.10 again, that's what that -it does.
[00:04:22] Now if I just say Docker run Alpine 3.10, just gonna say, okay, so what it did is it ran Alpine is like okay, I did it and then it finished right? So that's what the -it is like no, no hold on to it I want to actually connect to it right.
[00:04:43] Now I can say Docker run Alpine 3.10 and I can actually give it a command to execute first before finishes. So if I say ls, it's gonna go in, it's gonna run ls, and then it's gonna finish. So this is what the ls is of wherever it is in the container, which looks like it's the root, right?
[00:05:02] So that's what that last part is and the order here actually is very critical for Docker. So you do Docker, then you do the command. Then the next part here. Like that has to be all of your flags, right, the -it, the --RM, all that kind of different stuff.
[00:05:18] And then you put the name of the container, and then everything after the name of the container is whatever you want to run. And again, if you don't put the LS here, the container has some sort of command baked into it, right? And so it'll run that. Unless you tell it to do something else.
[00:05:38] It's the CMD, right? That's what they call it. Okay. So just we can say, Docker run Alpine cat /etc/issue, right? And you'll see here that this is Alpine Linux. What happens if we change alpine:3.10 to ubuntu:bionic? There you go, it has Ubuntu 18.04, right? Right, which is what we would expect.
[00:06:09] So you can see here we can swap out anything that we want as long as it's on Docker Hub or we built it locally. So I'm gonna give you a warning here, since I'm thinking about it. It's very easy to get a lot of images on your computer and have Docker take up like a, three gigabytes just in images, right?
[00:06:31] So I think if you do Docker prune images or images prune, Docker image prune, Docker image prune, like that, and that'll clear up. Like if you've been running Docker for a for a while, this will probably clear up a decent amount of space for you. But again, you'll have to rebuild everything next time that you download it.
[00:06:57] All right, so, still here on the Docker images with Docker. I'm gonna say Docker. Run, and we're gonna say -it but we're gonna put it in this new flag --detach ubuntu:bionic and this is gonna run something. It's gonna split up the hash which is the hash of the container of that's currently running.
[00:07:25] And then it completes but this container's now actually running in the background. And that's what detach does and says like hey run this but I don't wanna be in the foreground I want it to run in the background. So now I have a container running in the background.
[00:07:39] So if I say Docker ps, I can see here I actually have two running. I still had that my Alpine one running from my previous example, right? So you can see this has been running for 34 minutes. This has been running for 18 seconds. Now I can say, Docker attach.
[00:07:57] And then I can just copy and paste that. I didn't give this one a name so I can't attach to any specific name. Actually, you can give the ones stupefied. Brattain, I already know that is, let's do it. So if I do that, this will actually attach me to the running process, right?
[00:08:17] So now if I say a cat/ etc/ issue, you can see now I'm inside of Ubuntu. If I exit, It's actually gone now because I told it to exit, right? But I don't want this running in the background cuz this is just wasting resources in the background, so how do I kill it?
[00:08:38] Docker kill. I guess it kind of showed my cards on that one. So you can say Docker kill and you can give it the container ID. You can give it the short one. You can give it the long one. The long one looks like that. Or you can give it the name, right?
[00:08:52] So in this case, I'm gonna say Docker kill my-alpine. Now if I say Docker PS, totally empty. Two of these things that you have already seen before is if I say Docker run -it, you see me put name here, right? So like I did my -Alpine, right. The reason why is I wanted to be able to reference that later, right?
[00:09:22] So if I say alpine:3.10. Now if I go back to my other running thing here. If I say Docker ps you can see here that it has the name of my-alpine. This just makes it easy later to reference, right? So now I can say Docker kill my-alpine. Now my issue here, if I tried to do this again, it's gonna say I can't do that, that one's already in use.
[00:09:55] Why is that? Well, I can actually still look at the Docker logs of my Alpine, it keeps it around because it seems like I don't wanna throw anything away here. This is obviously garbage, it doesn't matter, right? But if I had a long list of things, it keeps those logs around just in case you wanna check them afterwards.
[00:10:11] And there's a bunch of introspection you can do after the fact of that container has completed. In this case obviously, this is garbage, I don't want it. So I'm gonna say Docker remove my-alpine. So now, it's thrown everything away that was using that before and now I can go back and do that again and it would run Docker alpine this time.
>> So when you use scale you just stop the process. You just stop the container. But then this is how you remove it entirely.
>> Right. It removes all like the post meta data and all that kind of stuff that it kept around for it. That's frequently useful, right.
[00:10:48] So that's why it does that by default. But in this particular case, I'm creating and destroying containers and I wanna reuse that name quickly. So if I just put --rm here, it'll remove everything once it's done, all right? So if I do this exit, and I come out right again, now I can keep doing that quickly.
[00:11:10] So that's what --rm is, you'll see me do a bunch because I'm lazy and I don't wanna type Docker rm afterwards and I forget all the time.