‫So what do we do with this file?
‫Well, let's exit out. We're going to see in this directory that we just have this one file.
‫It's barely a k in size.
‫It obviously doesn't have anything in it other than the text, but it's the instructions on how to build
‫our image.
‫The from command, when I build this image, is going to actually pull that debian jessie image from
‫Docker Hub down to my local cache. And then it's going to execute line by line
‫each of those stanzas inside my docker engine and cache
‫each of those layers.
‫Let’s type docker image build and we're going to tag it.
‫Remember, that's the name that we're going to give an image.
‫Since I'm going to only use this image locally I don't have to use my Docker Hub account name. I
‫can call it whatever I want.
‫I'm going to call it custom nginx, and I'm going to put a dot here, which basically says, build the
‫dockerfile in this directory. And you'll see as it that steps through this,
‫that each step is a line in the dockerfile that it's executing inside this image as it's building it.
‫And then there's a little hash at the end which is actually the hash
‫it keeps in the build cache so that next time we build this thing, if that line hasn't changed in the
‫dockerfile, it's not going to rerun it.
‫This is one of the magic pieces of why Docker makes deployment and software building so fast is
‫it actually is intelligent enough to cache the steps in the build.
‫So quite often, after you’ve built an image the first time, and you're really just there changing your custom
‫source code and not necessarily changing the application in itself,
‫all this installation stuff has already happened.
‫So you will have very short built times.
‫Okay, so now we're done building. That took about a minute.
‫But, what if I go back in that file? I'm going to add an additional exposed port.
‫For example, I'm just going to do 8080.
‫Now, that doesn't mean that nginx is smart enough to know that I'm opening this port, or that it's
‫in any way communicating with nginx.
‫It's just me allowing the container to receive packets on that port.
‫Just to be sure, let's do a docker image ls. We'll see right up at the top that it auto tagged
‫it as latest,
‫right?
‫Because we only called it the repository name.
‫We didn't actually give it a colon tag name when we tagged it.
‫So let's build it again.
‫Wow, that was fast, right?
‫That only took us a couple of seconds. And you'll notice that on each step it'll say using cache. Starting
‫with Step 2,
‫it'll say using cache, using cache on 3, using cache on 4. On 5, it recognizes on step 5, that that
‫line is different.
‫So it actually executes that into the container. And then, on Step 6, it has to rerun that line because
‫the minute a line changes, every line after that now has to be rebuilt as well.
‫This brings up the point about the ordering of your lines in your Dockerfile. Because, if you
‫get things out of order,
‫for instance, if you copied the code in...let's say you're building a website. If you're copying the software
‫code that you're creating at the very beginning of the file, then every time you change a source file
‫and you rebuild, it's going to have to build the entire Dockerfile again.
‫So it's critically important for your sanity and time that you usually keep the things at the top of
‫your Dockerfile that change the least and then the things that change the most at the bottom of your
‫Dockerfile.