Check out a free preview of the full Complete Intro to Containers, v2 course
The "Docker Images" 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 explains that images are premade containers. Docker Hub is an online repository with thousands of images you can download as a starting point for a project or to experiment with a new framework, tool, or environment. An example image is downloaded from Docker Hub and run in a container to highlight how images can be used without Docker.
Transcript from the "Docker Images" Lesson
[00:00:00]
>> Brian Holt: Let's talk about containers, we went through and we did crafting containers by hand. Now you are armed with the knowledge of what a container is, what it's doing, what it's solving for you. And now you're never gonna have to do anything I just showed you. Which is both cool, cuz you can see here I had to have notes for all that cuz I can't remember the stuff off the top of my head.
[00:00:22]
But I find this useful just because, whenever I don't know how my tools work, I tend to kind of resent them a little bit because they add complexity to my life. But when I understand what they're doing for me and what I no longer have to do because the tool is doing it for me, then I tend to really like my tools, right?
[00:00:41]
Because I know what I'm getting myself out of.
>> Brian Holt: So, we're gonna actually start diving more into depth into Docker, Docker-specific features. A bunch of this stuff will apply to other container runtimes and container orchestration frameworks. But we're gonna do it all through the lens of Docker because it's by far the most popular.
[00:01:06]
>> Brian Holt: So, we're gonna hop into Docker images here. So the first thing that you wanna to know about containers is a premade container that you can use is an image. You can think of this container like class, if we're using programming terms, right? It's like a reusable structure that you can instantiate many of, and you would call this an image, right?
[00:01:32]
>> Brian Holt: So basically, what it is, is you create a container, and then you just dump out the state. And that state that you have dumped out is what the image is, right? So, we've been using one already, right? That ubuntu:jammy is an image that we got off of Docker Hub.
[00:01:51]
So it's probably useful for you to just go to hub.docker.com. If you're familiar with NPM or any one of those package manager frameworks, Docker Hub is what that is for containers. So you can see here, there is a container for everything. There's one for Nginx, there's one for Postgres, there's one for Python, there's one for LangChain, there's one for TensorFlow.
[00:02:17]
If you can guess a language or a framework, I guarantee you someone has written a Docker container for it. Kind of like NPM, right, but just for containers. Which is great, because, for the most part, this means if you have a some specific need, this is actually extremely applicable to what I'm doing now with Docusaurus.
[00:02:37]
I'm rebuilding the docs for the company that I work at. Turns out there's already a container image for that, I don't have to go build that, I can just go ahead and use theirs. So a lot of what we're gonna be doing is going to be based of these base containers.
[00:02:50]
And then we're gonna add little modifications to suit our needs to it. Let's go ahead and do a Docker image without Docker, just to show you that nothing Docker is doing for you is really super interesting from that perspective. So I want you to copy and run this.
[00:03:12]
You can go ahead and get rid of everything that we have done previously, which I'm gonna to just exit out of all of these. Just so you know, this will destroy all the work that we did, that's okay, right?
>> Brian Holt: And if I say docker ps, and now I have no containers running, so that's cool.
[00:03:34]
Okay, so I'm gonna run this docker run, that's gonna run an image, the -ti, as you might remember, is interactive. This -v is mounting a volume, that we're gonna talk about that at the end. But suffice to say, this is going to connect to our core, our host Docker instance so that we can use its containers inside of our container.
[00:04:00]
And then we're gonna, Docker makes a container which has Docker in it. I know that's a little wild, but what we're gonna do with that is we're going to pull in containers off of Docker Hub and then we're going to start using them inside of Docker.
>> Brian Holt: So, pwd, I'm inside of a container, and if I say cat etc/issue, cuz I'm always curious of what version of Linux they're using.
[00:04:32]
You can see I'm in Alpine Linux, which I actually was not super expecting, but cool, it doesn't matter, right? We'll talk about Alpine here very shortly as well.
>> Brian Holt: And what we're gonna do is we're gonna run just a normal stock. We're gonna run another Alpine container inside of our Alpine container, which is gonna just be super fun.
[00:04:52]
run --rm -dit --name my-alpine, you can call it whatever you want. I just prefix a lot of stuff with my- just to kind of show you that it's a name that I'm adding, it doesn't mean anything in and of itself. And we're gonna do alpine, which is the name of distribution of Linux, just like Ubuntu or Arch or RHEL or something like that.
[00:05:21]
So 3.19.1, and then we're gonna run shell, which is like Bash, but it's just a shell, right? So now, this was an Alpine container, so I was in macOS, and then I ran an Alpine container that had Docker in it. And now inside of that, I am running an Alpine container, so I am running a container within a container on my macOS system.
[00:05:48]
So here,
>> Brian Holt: I think I need to,
>> Brian Holt: dockercontainer.tar my-alpine, I think I have to do this from outside of it, I don't know, has that worked right there, it did, okay. So what I did here is, docker export allows you to just dump out whatever's in an image.
[00:06:14]
I did that and I outputted it into a dockercontainer.tar, which, a tar is just like a zip, right? But tar, it's not compressed, it's just tarred together, okay. And then I'm gonna make a directory called container-root.
>> Brian Holt: And then I'm gonna do, I have to look up the the detarring thing every single time, tar xf dockercontainer.tar -C container-root/.
[00:06:43]
And now if I look in that container root, you'll see that I have an entire file system that I grabbed off of Docker Hub. This is just a normal Alpine container, and lo and behold, this stuff should all look very familiar to you. So we're gonna unshare all of that stuff and say blah.
[00:07:04]
I probably could have said shell is there again as well, but ash is the name of the shell that Alpine uses, which is like bash, but just slightly different. And mount this, blah, mount that, blah, and mount this, blah. Now I could go in and I could do all the cgroups again, I'm gonna spare you all of those details, don't really need to think too much about it.
[00:07:36]
But now, what have I done here? I pulled a container off of Docker Hub, an Alpine container, I then did an unshared environment, I change rooted into it. I mounted all of its namespaces, and I could go do all the cgroup stuff, just fathom for a moment that we did.
[00:07:53]
I ran a Docker image without Docker, right? I used all that kind of stuff, and if I was running this all on a Linux computer, which, obviously, this is not a Linux computer. I wouldn't have had to do all the Docker magic to make all that happen. We wouldn't need Docker at all, right?
[00:08:10]
We could just run these images without using any sort of the Docker tools, right? Just using unshare, using change root, and using cgroups. So this is just kind of to prove the point to you that you don't need Docker in that sense. Docker is just a very convenient tool to get all these containers running for you.
[00:08:31]
So again, just to kind of make sure we're all on the same page here, that's why, this -dit was detached, that's why we weren't necessary. So now we're inside of a handcrafted container inside of the Docker container inside of macOS. We're not actually inside that other container, it is running, so if I say docker ps over here, you can see that I have my-alpine which is running.
[00:08:59]
The only reason that we had that running was so that we could dump out the state of it, so then we could go ahead and use it, make sense?
>> Brian Holt: Pretty cool, right? Again, you can do all this stuff yourself, you don't need Docker, you don't need domain, right?
[00:09:16]
But in this particular case, Docker is extremely helpful, and you should definitely use it, all right? We're gonna exit out of that, exit out of that, and then we can say docker kill my-alpine.
>> Brian Holt: And now we have nothing running in Docker. So, again, Docker does so much more for you than just pulling images and running them for you.
[00:09:43]
Though I'm gonna argue that that core functionality, no matter how simple it is, is really what you care most about. It does networking and all that other stuff, but yeah, you really could get around using it.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops