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.


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.

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.

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.

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.

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.

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?

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.

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.

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.

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.

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?

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.

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.

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.

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.

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.

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?

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.

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?

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.

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?

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.

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.

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.

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.

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.

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?

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.

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.

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.

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?

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.

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.

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.

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.

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.

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.

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.

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.

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.

Probably the same people that are using Mosaic for their day-to-day browsing.

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