Complete Intro to Containers, v2

Introduction to Dockerfiles

Complete Intro to Containers, v2

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

The "Introduction to Dockerfiles" 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 introduces Dockerfiles, which allow you to outline how a container will be built. Each line in a Docker file is a new directive for changing your Docker container. A basic Node 20 image is created with a command that executes a console.log statement when the container is run.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Introduction to Dockerfiles" Lesson

[00:00:00]
>> Brian Holt: So so far we've been talking about basically how to interact with containers and not necessarily how to build containers. You never really have to learn how to build containers if you just learn how to interact with them that's like most of the usefulness cuz you can use what other people have created.

[00:00:16]
Which generally is most of my interaction with containers is just using stuff that people have built for me. But I am gonna show you in this section here, Docker files, as well as in making tiny containers, we are going to build our own containers. So let's talk about Docker files.

[00:00:38]
Docker files are basically as you might guessed it's a manifest of what goes into a container. It's very declarative that you say take this container, add these things to it, add these users, copy these files, run this command, and then you're done. That's what a Dockerfile is. The cool thing about having a Dockerfile is you can build that into a container, or you can build that into an image, right?

[00:01:03]
And then you can use that image to run containers. But the fact that you end up with an image is that you have this like abstract container you can use over and over and over and over again. This is a important concept when it comes to containers, that people like to say that, I mean, it's a not everyone likes this analogy, but I think it illustrates it well is that you want your containers to be cattle, not pets, right?

[00:01:29]
The idea of, if you have a 1,000 heads of cattle, you don't have a name for every individual one of them. They're kinda interchangeable, right? As opposed to if you have a a pet dog, like that's Luna, Luna's my dog, right? That's Luna and Luna is irreplaceable, right?

[00:01:45]
You don't want your containers to be pets because you want them to be, well I'm done with this one, throw it away, and then tomorrow when I come back I can just spin up another one that was just like it and keep going from that way. So that's the idea behind cattle, not pet.

[00:01:59]
You don't wanna have what we call snowflakes right? Not like the companies to work for but like snowflakes in the sense of like, it's very special, it's very unique, it's very catered. Your containers shouldn't be that way they should be useful and reusable and disposable. So it makes sense?

[00:02:17]
Okay.
>> Brian Holt: So let us make the most basic Node.js Dockerfile ever. So where am I? I'm just gonna go on my desktop and yeah, that's fine. Make directory, my-project, something like that. I'm gonna open this in code. Get rid of this one, I don't need that. Don't save that.

[00:02:51]
Yes, I trust the authors myself or rather I don't really trust myself but I don't trust anyone any more than I trust myself. Okay, empty project I'm just gonna create a new file and I'm gonna call it Dockerfile. No extension, technically Dockerfile is the extension, but just call it capital D dockerfile.

[00:03:11]
That's what everyone calls them. Don't ask me, I didn't invent it.
>> Brian Holt: Not that one.
>> Brian Holt: In here, I'm gonna say,
>> Brian Holt: what is the container that I'm gonna start with? So this could be from any of the containers that we've talked about so far. We're gonna start with node:20.

[00:03:42]
Be whatever you want. So what's cool about this is because it's all Dockerfile, I have the Docker extension installed. It gives me some completions and lets me know what it's expecting, all that kind of stuff. CMD is going to tell it what command that you want it to run.

[00:03:57]
It's typically the last line in your Dockerfile. So I'm gonna say node, I'm gonna put -e, and I'm gonna say console.log ("hello, or what did I say? I said,
>> Brian Holt: That look right? Whatever you wanna put in there. You typically give it a comma separated array of all the various different arguments order that you wanna put them in.

[00:04:48]
There's ways of doing it the other way, but this is just the way that everyone does it, so we're gonna stick with that.
>> Brian Holt: And then I'm gonna open my console here and I'm gonna say docker build..just telling it where you are, right? So in this directory,
>> Brian Holt: And it's gonna build that for me.

[00:05:15]
>> Brian Holt: So, you can see that this ended up building. It took five steps, so it took basically zero time cuz there's nothing really here and we now have a container that doesn't have a name that would be kind of hard to run if we wanted to. I think that's really what it would be right there.

[00:05:41]
>> Brian Holt: That's interesting. I've never looked at this before. I didn't know they gave you a URL that you could look at it.
>> Brian Holt: There you go. Pretty cool, I didn't know it did this. It gives you some sort of information. Some source shows the source that I built it from, which is that.

[00:06:05]
The logs and history. None of builds, okay? Pretty cool. Didn't know it did that.
>> Brian Holt: All right, so, we did that for all of these files as well. By the way, I will have a link to the project so you can actually copy and paste the entire thing and see the entire project.

[00:06:28]
They're just organized into directories here for you as well. Because I don't really expect you to type all of these. I don't think it's helpful. Okay, so let's go ahead and we built our container and we're gonna go ahead and run our container here in just a second.

[00:06:48]
>> Brian Holt: So let's go grab that ID. Where is this? I think it's this one.
>> Brian Holt: So it just gives you the hash of whatever it is, right? So I said, run the hash. And you can see here, it ran from node.js, hi, right? Yeah, in case you're not familiar, you can say node-e, and then whatever string you gave afterwards, it'll literally treat that as the file to run.

[00:07:19]
So that's what we did there. Cool, so let's talk a tiny bit about this particular process that we're going through here. So we're starting with another image, right, we didn't build node:20 I have no interest in building node:20 that's maintained by a bunch of security professionals over a docker that know an infinite more about security than I do.

[00:07:50]
And then that container is actually built on top of another image, and that image is built on top of another image, and then that image is built on top of base db image that db maintains. My point here is like it's really powerful, right? That we can all kind of like share these kinds of base images like I could go and build some sort of like Frontend masters specific container for you to get started with building Frontend masters projects that can be built on seven more images before that.

[00:08:24]
And then we all can just kind of build stuff on top of each other. That's like the Docker way, right? Every single one of these lines here is called a layer, right? So you're just layering stuff one on top of the other, right? So you notice when you say like Docker pull something and it shows you like it's downloading seven things at the same time, right?

[00:08:45]
Those are all layers, right? And so it's just saying like if I stack all these layers on top of each other, that it turns into this image, this container, right? One that makes it super cashable so that if you only change the fifth thing of seven, you don't have to rebuild everything from one to five because none of that changed, none of those layers change.

[00:09:08]
It's only five, six, seven that changed, right? We'll take advantage of that fact to make our images rebuild really, really quickly as well. I'll show you that here in a sec. But just keep in mind, it's just images and layers all the way down here. Okay, so we used from, that's to start with some sort of image.

[00:09:27]
And then CMD is basically saying like when you go to actually execute this, so this part does not execute until the container is run, right? So this doesn't actually run anywhere inside of build, right? It only runs in run. That's something to keep in mind and it kind of messes with people to keep in mind of like some of this stuff runs when you're building, and some of this stuff runs when you're executing.

[00:09:53]
CMD always runs when you're executing.
>> Brian Holt: Okay, we did that. So this was inconvenient to remember this hash, so, I'm gonna show you how to do that a little bit easier. So you can say docker build .--tag, and you can give it a tag of some variety. So we're gonna call it my-node-app.

[00:10:16]
Instead of -- tag as well, you can say just -t. And so now I have my-node-app. So I can say docker run my-node-app much easier than dealing with hashes. And then you can also do it so you can version it yourself. By default, this is getting just one for version but then we can start giving it various different tags as well of like this is version one, this is version two, version three.

[00:10:49]
Just like we were seeing with Jammy and Focal and all of those, we can give it names, we can give it numbers, Docker doesn't really care. It's gonna pull whatever tag you tell it to. So let's say, I just say, can I add something so it's a tiny bit different.

[00:11:06]
And I could say a docker build tag:2. So I built that. So now if I run my-node-app:1, what do you think it's gonna say?
>> Speaker 2: The original.
>> Brian Holt: And then if I run out with 2, what do you think it's gonna say? Right, so it keeps all of them as well, which is cool.

[00:11:30]
Very easy versioning.
>> Brian Holt: Question so far? Yeah, Dustin.
>> Dustin: Will Docker build create a component or an image when I run it on my local machine to create an image.
>> Brian Holt: So it creates an image then when you go and just say docker run, it will create container, it's a bit of a haredess split, I mean, who really cares at the end of the day?

[00:11:55]
I call them all container cuz everything mix up my brain cuz that's having a two year old does to you but at the end of the day, we're running containers and the containers are built from images. I'm positive that I've probably mixed it up at least once up here.

[00:12:10]
So not the most interesting differentiation between the two.

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