Complete Intro to Containers, v2

Docker Images with Docker

Brian Holt

Brian Holt

SQLite Cloud
Complete Intro to Containers, v2

Check out a free preview of the full Complete Intro to Containers, v2 course

The "Docker Images with Docker" Lesson is part of the full, Complete Intro to Containers, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates some of the common commands used when running images with Docker. The commands include run, attach, ps, and kill. Interactive, detached, and name flags are also demonstrated.

Preview
Close

Transcript from the "Docker Images with Docker" Lesson

[00:00:00]
>> Brian Holt: At this point we now we're fully leaving behind all of the unsharing. We won't run unshare again in this class. At this point, I'm gonna assume that you're convinced that Docker is wholly unnecessary, and therefore Docker is totally necessary to you, right? Let's go ahead and just start running containers and just kind of getting you really comfortable with what Docker is and start using it.

[00:00:19]
So I'm gonna say docker run, and I'm gonna say -- interactive. We've been doing this, this entire time, but I just tended to shorten that to --it or sorry, -it.
>> Brian Holt: But --it is really is just -- Interactive, inter, I can spell, active --tty. That's what that -it is.

[00:00:50]
And then you could say alpine, which is just the name of the individual container, that implicitly is latest. That's when, if you don't put something there, it'll just attach that for you. I'm being super prescriptive in this class, because if you take this course in three years, which all this stuff will be still entirely valid as is.

[00:01:13]
But sometimes the Linux distributions can change a little bit and then it messes people's up. So I'm just gonna have you be explicit on which versions you're using so that I can guarantee you that all these commands are gonna work now and in three years,right?
>> Brian Holt: So that's why I'm gonna say alpine 319.1.

[00:01:31]
As of today, that would be latest, I think, or at least as of last week. But by the time you watch this, it probably isn't.
>> Brian Holt: So that dash dash interactive tty puts you into this, right? It makes it interactive so I can actually, interact with it. If I don't put that in there, and I just say run, it notice it just goes straight to the background, right?

[00:02:03]
>> Brian Holt: Because alpine actually doesn't have any commands to find in of itself. It's just a distribution, it has nothing in it. It actually just starts and then stops cuz it has nothing to do, right? So that's why that -it is super important there.
>> Brian Holt: And you also have to give it something to do as well, right?

[00:02:32]
>> Brian Holt: Run, what did I run do there? Did I say I forgot run.
>> Brian Holt: Yeah, you need run. And that will drop you in your shell. So just remember, every time you do run, you are starting a new container. That's important to know because people get frustrated. It's like, how do I interact with the same container that is exec?

[00:02:52]
Exec will allow you to interact with other containers or existing containers. Run is always going to start a new container for you. I think even if I do that, yeah, it'll just drop me in there, because, by default, it'll run the shell,cuz that's what is defined as its default action.

[00:03:06]
What happens if I wanted to do something other than just run a shell? This is really dumb, but what if I just want it to run ls then finish? You can do that, right? You can just pass it whatever command you want it to do. I don't know why you would wanna do this, but it's possible, right?

[00:03:23]
Sometimes you wanna use this for something like, let's say I wanna connect to a MongoDB instance that's out in the cloud, and I don't really wanna install Mongo on my computer. I'm sure many of you kind of felt that before, you can actually just run a Mongo container and have that connect to the thing.

[00:03:42]
So you actually running a container that's connecting to the network. You can do all the stuff you need to do. And then you can exit and it'll remove everything. So you actually never have to install Mongo which is cool. Stuff like that.
>> Brian Holt: So this was done on Alpine.

[00:03:58]
What happens if we do this on ubuntu:jammy? You can see it's different. Not a lot different, but slightly different.
>> Brian Holt: Which is kinda cool, cuz this is running on Ubuntu instead of running on Alpine
>> Brian Holt: What happens if you do wanna run something in the background? Well, you can say docker run, --detach.

[00:04:31]
Or for short, you can just say dit, ubuntu:jammy.
>> Brian Holt: This will throw it into the background. This is actually what it called it. So if I say Docker PS now, you can see that I have jammy running in the background. So this container is still living on. It's living on in the background.

[00:04:55]
And so now, because I didn't give it a name. You can either call it vigilant retain, which is the random name that assigned it or you can use the hash as well, which is totally fine. Or you can use this mini hash, which I think is the first 10 numbers of it or whatever.

[00:05:14]
>> Brian Holt: You can see it's just hanging out doing bash so it's not doing anything. It's just hanging out the background, yeah.
>> Speaker 2: Is there any reason besides debugging to go into a running container?
>> Brian Holt: Is there any reason to go into a running container besides debugging? If I'm running a thousand Kubernetes, or a thousand container cluster out there, probably not.

[00:05:39]
Probably even a bad idea to connect directly to your containers, right? You wanna get that from Datadog or wherever you're aggregating all of your information from. But if I'm just messing around and doing development and hopping around all my containers and popping on my database container to connect to the client and then popping over to my app container to see, all that kind of stuff.

[00:06:01]
For all that stuff running locally. Yeah, I'm all messing around with all of my containers out in production almost never. So there's another thing you can do as well, which is docker attach. So if I do this, docker attach to that. Because this container is just hanging out, this actually allowed me to connect to the same instance of it as well.

[00:06:25]
So it's not actually starting a new Docker process or a new Bash process. It's not starting a new Bash or anything like that. You can see I'm actually connected to the same one. So Docker attach is different than Exec because Exec is actually going to execute something new on your container.

[00:06:39]
Docker attaches is basically like, hey, just attach me to what's already running. In this particular case, doesn't really matter, right? We're both connecting to Bash, but imagine if you're running like Postgres on your container. If I say docker attach, it's going to attach me to the process that's actually running the server, which may not be what you want, right?

[00:07:00]
It's just gonna show you logs that will not be interactive. If I wanna actually connect to the container and not be directly connected to that process, I'll have to docker exec to get into that particular one. So the other reason I bring that up is that does confuse people sometimes with like, hey, I just wanna get on the container and mess around with Bash, I don't wanna be connected to the database process, right?

[00:07:25]
That's why attaches to get on to what's existing and running, Exec will run a new process for you. Okay, I'm gonna exit. I'm gonna say, Docker kill, and again, I can say either blah, vigilant-brattain. I can say this which is for shorthand or I can go up here and I can grab the whole thing.

[00:07:50]
Any one of these will work.
>> Brian Holt: It was not running because I exited here, cuz I attached to it, right? So docker image, if I say ls, you can see here I have, these are all of the images I've pulled. You can see I have stuff in here from weeks ago.

[00:08:20]
>> Brian Holt: Communities web, I got all sorts of stuff on here. So that'll show you the images that you have. If I say docker container ls, this should be all the containers that I have not actually got removed all of their remnants, all of their logs and all that kind of stuff, I think that's the case.

[00:08:41]
Yeah, just manage the individual containers that are running at the moment
>> Brian Holt: But that's why we always there it is dockerps--all. Yeah, you can see here I have a bunch of stuff like, you can see even from this class, every time that I'm not putting dash dash rm in there, it's keeping all these remnants laying around, which can add up after time.

[00:09:10]
I think if I say docker prune, I've got to remember now, docker. System prune, container, prune. Container, prune, I think you're right. Yeah, so this will remove all the information for stopped containers that I have. And you can see here bunch of stuff just freed up. I removed 140 megabytes of just stuff laying around, right?

[00:09:35]
And so now if I say dash, dash, all. Now it's nothing in there, right? So that's why prune is nice for that as well. That's why I almost always do docker run dash dash rm.
>> Brian Holt: Ubuntu jammy.
>> Brian Holt: So I ran that. I just put it in the background.

[00:10:03]
I'm gonna kill it really quick.
>> Brian Holt: And now if I say docker ps ---all, notice it doesn't show up in here. That -- rm is what keeps it from showing up there. So if I'm just churning through a bunch of stuff and not really caring about why stuff exits, I always put that --rm on there, so I don't have to remember to run prune every so often.

[00:10:30]
Some people that know will have a cron job that just runs prune once a week or something like that. I don't use Docker that much, so I don't typically do it, but if I did this whole process without the ---rm, it would keep all that stuff around.
>> Brian Holt: And then, yeah notice I put a name on here of my ubuntu.

[00:10:51]
Notice that this is not a randomly generated name anymore. It's called My Ubuntu. That's just helpful for me to remember what I called stuff. As opposed to being randomly generated. Which by the way, all this stuff I'm showing you, it all shows up in here as well. This is just a visual representation of Docker PS and Docker images that I'm showing you.

[00:11:08]
It all just shows up here. But there is a lot of stuff you can do here, you can figure how much resources it gets, how many processes you can run, blah blah blah, that all exists here. Resources maybe? Yeah, see this is what you configure here, is all gonna translate into c groups, which is cool, right?

[00:11:35]
They definitely hide about, they're not allowing you to control how many PIDs that show up in here, right, but you could.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now