Check out a free preview of the full Complete Intro to Containers (feat. Docker) course:
The "Docker CLI" Lesson is part of the full, Complete Intro to Containers (feat. Docker) course featured in this preview video. Here's what you'd learn in this lesson:

Brian shares a few commands in the Docker CLI that give the container's history, pause the container, erase the container, or run a container with, and how to prune stopped containers.

Get Unlimited Access Now

Transcript from the "Docker CLI" Lesson

>> We're just gonna go through a bunch of the features in the Docker CLI, so that we can just kind of type out and see all the various things that it can do for you. It's a Swiss army knife, it does many things for you. So I just want you to be familiar with some of the other features, so that when you go to use them, that you can have them here.

[00:00:16] So let's come back here to my container, or to my terminal, rather. First thing I'm gonna say is docker pull, and I'm gonna do jturpin/hollywood. So you can see there, by default, it was already using latest, right, because I didn't specify a version, but it tells you that.

[00:00:41] And I already had this downloaded. So it's just going to say already exists, already exists, already exists. And so that will actually go and fetch it from the registry, so it's locally cached on my computer. So later when I go to use it, it's already there. Right? I had many of you do this before the class started, just so that you would have these all ready to go.

[00:01:00] And now that it's already here, I could say docker run -it jturpin, and actually I'm gonna do this to with a different container just so, or a different terminal real quick. This is small text, but you don't have to really care what it's saying. docker run-it, jturpin/hollywood, and then I'm gonna put hollywood again.

[00:01:38] So, what this is doing, this is hilarious to me. [LAUGH] It's not doing anything.
>> [LAUGH]
>> But, it's supposed to look like a Hollywood hacker's console. Right? So, one of the people that wrote this said that she'll get on an airplane and just run this, just to see who looks nervous around her.

>> [LAUGH]
>> And she'll just pound the keyboard like this. So yeah, that's exactly what this container is. I think it uses tmux, right. You can see here, it's like doing the matrix on the top. And then it'll just re-split how everything is set up. That one doing SSH keys.

[00:02:15] That's funny. So, it's like a trick, right? But this is a fun party trick. This does absolutely nothing. But it looks really scary, right? You can see some of these don't even work. But does that make sense for like pull right where it pulls something down off the registry?

[00:02:34] And as you might imagine, I could go to or, and I would see that particular entry in the docker hub, right. Do all those dots connect with how I.
>> It's like a VM install, basically.
>> It's like NPM, cache, basically, cuz you're not actually running it yet, but you're pulling it in.

[00:02:56] Yeah. Okay. Let's stop that, cuz that's ridiculous. Stop it, stop it. Thank you. Okay, now we'll go back to the other one. So, let's do docker inspect now. So, I'm gonna say docker inspect. And let's do node 12-stretch. So, you can see here, it just outputs a lot of information about that.

[00:03:25] It'll give you the hash, it'll give you the tags. It will give you a bunch of information, here it will give you environmental variables. Like you can see here, this came with Node, I didn't know this came with Yarn, but you can see there that it came with a Yarn version.

[00:03:40] It'll tell you what command it is going to run. So it actually runs node like that whenever you run it. Entry Point. See, it just gives you a bunch of stuff like that. So this is useful if you're like trying to figure out what's wrong with this version or like how was it built or something like that.

[00:04:00] Bunch of stuff like that. Next one we'll learn is docker pause. So I'm going to do Docker run -dit. So the dash d is detached, which we saw previously. I'm gonna do the jturpin hollywood one again. Hollywood, but then now this is gonna run in the background, right, which is, I mean, it's doing something, but we're not watching it, right?

[00:04:28] But you can see here it's still running. That we can see that's still running, but this one here's running. And this is hard to read like this. So just for a second, I'm just zooming out so you can see all these on one line. You can see the status here, it's been up for about seven seconds.

[00:04:50] So again, it's been up for seven seconds. So now I can say docker pause, and I'll just take that container ID and paste it right there. And now if I say docker ps again, it's gonna say it's paused right there, right? Whatever it was doing, it just froze all of its process trees, and it's just stopping dead in its tracks.

[00:05:15] And now if I say docker unpause, as you might imagine, say docker ps, it's back to execute. So, another thing, if you want to pause something that's happening, you can just say docker pause, and that works. And then at the end of it, when I want to get rid of it, docker kill, and it is gone.

[00:05:35] Let's go ahead and kill that other one, cuz I don't know why it's running.
>> Is there a command to kill all of them at once? Cuz I have a lot of them running, unbeknownst to me.
>> docker kill
>> It doesn't work, I tried.
>> No, you have to do docker ps -q.

[00:05:54] So I think if you do, so this subshells out to another shell, right, if I put it inside of the dollar sign parentheses here. And then docker ps- q, I think -q, you might need to check me on that one, and let's just do it again. Hold on.

[00:06:11] We'll run this a couple of times. The answer is, there's no easy way to do it. But I think you can do docker kill and then docker ps -q. And you can see there it killed all of them. So docker ps -q just outputs the IDs or the container IDs, right?

[00:06:37] And then that's fed into the docker kill command. Okay, another good one. So here's the big key that I need you to keep. When I saw docker run and docker exec, it took me a second to figure out, why do I need both of these commands, right? Docker run is going to start a new container.

[00:07:05] Docker exec is going to run something on an existing container, right? That make sense? So when I say, docker run like this, it's going to start up a new hollywood container, right? But if I say docker exec, it's going to try and find a container to execute against something, right?

[00:07:19] So this one, I ran another docker container. So if I say docker ps, you can see here I have this hollywood one running, it's called fervent_kalam. And I'm going to say docker exec against fervent_kalam, or I can put the ID there, both of those will work. And I just wanna see what's running on that.

[00:07:41] So I can say ps aux. And you can see there, it's running a bunch of stuff, right, because it has all the various different hollywood containers going on. We can also say ls, right? You can see that's in the root directory, pwd, it will give me a slash, right?

[00:08:00] So this is running against that ongoing executing container. Does that makes sense? So if I wanted to connect to it, I could just say bash. I have to say -it, I think. And now, I'm actually attached to the same running container, right? So, again, if I say ps aux, you can see all the stuff that's running against this container.

[00:08:29] Make sense? Docker kill, that one that we just did. Here we go. All right, so we're gonna do another one here. This is what we're gonna do, docker history. And I'm gonna say docker history, and we're gonna say node colon 12 dash stretch. And here, we can see all of the various different, and we'll get into docker files here in just a second.

[00:09:00] But we can see all the changes in the history here. You can see, this was last modified two weeks ago. And you can see all the different sizes and comments and created by, and images, and all that kind of stuff. This is kind of interesting just to introspect how the, your docker container is changing over time.

[00:09:21] Docker info, this is useful like if you're like SSHed into like a VM that's out running in the cloud. This will just dump a bunch of information about the host computer that it's on, right? So I can see that this is on cgroupfs is what it's using for control, cgroups.

[00:09:42] I can see that I have 20 containers here, all of them are stopped, right? Which means that I have a bunch of unnecessary information sitting on my computer. It's not in debug mode, I'm not running swarm right now, bunch of stuff like that. OS type Linux, operating system is Docker Desktop.

[00:10:05] And I'm using the community engine, not the enterprise one, right? So just a bunch of stuff like that, can be occasionally be useful. All right, so let's do docker run -dit mongo, this will start running Mongo in the background. And now I'm gonna say docker top that. This is the same as doing like exact px aux, right?

[00:10:37] This is just an easy way to see all the processes that are going on in a particular container. So that can be useful. So this one is actually, you can see it's running mongodb in the background, mongod. And docker kill that. Let's take a look at docker ps dash dash all.

[00:11:03] Now for me, you can see here, that I have, I knew this, I have 20 containers that I've just kept around, right? And you can see the status here for all these is exited, because I keep executing containers and then not deleting them. That's what that dash dash rm is useful for.

[00:11:19] That would save me a bunch of space, but I keep forgetting about it. But let's take a look at one of these, right. So You can see some of these are from 10 days ago. This one here that I just ran, the mongo one. Let's do docker logs from that.

[00:11:42] You can see here, this is a bunch of Mongo DB logs, right? That should not be surprising to anyone, right? I ran Mongo DB, it generated a bunch of logs. So here they are. So that's what docker allows you, that can be ongoing. It can be one that's still running, right?

[00:11:55] It'll keep track of that information for you. Or you can do look at ones that have recently shut down. But let's say I want to get rid of that, I can say docker rm that, and it will actually go and delete that container, it now freed up all that space.

[00:12:12] And if I wanted to, let's say I am no longer planning on using the Mongo container anymore, I could say docker rmi, which is, remove the image, right? Mongo. Now I am not going to do that, because we are going to use it here later in the course, but that is how you would free up some of that space as well.

[00:12:25] And there is docker, I think it's container prune. This will remove all stopped containers, I wanna do that. And I say yes, and I reclaimed 422 bytes on my 256 gigabyte computer. But now if I say docker ps --all, nothing there. Right? So got rid of all that stuff.

[00:12:58] Now again, you can do docker image prune, right? And that will free up a substantial amount of stuff. I don't suggest doing it right now.
>> You know where to see like all the images?
>> I think a docker image list. Lucky guess. You can see I have quite a few.

[00:13:17] Quite a few. And you can see they're, some of these are quite big. So, yeah, I probably need to go free this up, which I will do after the course. All right, so docker run -dit, do node:12-stretch. So now I have docker ps that, right, it's running in the background.

[00:13:50] I have boring gagarin, I can do a docker restart, that. Unsurprisingly, this will restart the container. That's it. In this particular case, so, the reason why that probably took so long, it might just take that long. But a lot of times some of these containers won't respond to restart signals, right.

[00:14:19] So that's how these things work. It'll send like a terminate signal or send a restart signal. I think in this case, node doesn't respond to restart signals in and of itself. So what it does, is that it waits ten seconds, it's like okay, you're not restarting, and it just hard restarts it for you.

[00:14:33] So some of these will be a lot faster because of the respond to the restart signal. But, Anyway, that's docker restart. And then there's docker search. Let's say we wanna search for a Python container. I can say, docker search python. And you can see here, it'll return off of docker hub.

[00:14:53] Here's a bunch of Python containers that you could be using. It'll show you the stars, the descriptions, all that kind of stuff. And that's docker search, I would probably just do it on docker hub myself, but it is available in your command line if you don't like browsers.

[00:15:08] Probably the same people that are using Mosaic for their day-to-day browsing.