‫All right. Let's look at some more Dockerfile examples.
‫In our repository that you've downloaded from my Github, let's go into Dockerfile sample number 2.
‫This time, we actually have two files.
‫We have the Dockerfile. And now we have an index.html.
‫Super simple. Hello world. HTML.
‫It's not really important to understand the HTML.
‫We're just going to copy it into our container image as we're building it.
‫If we look at the Dockerfile, you'll see on this one that we're actually using Nginx as our FROM
‫command.
‫We're not building our own Nginx because ideally, if you can use an official image to get the
‫job done from Docker Hub, then it will be a lot easier for you to maintain this Dockerfile and
‫keep it working well.
‫In simpler scenarios, quite often the official image is work. But as you grow and add complexity to
‫your environment and your systems, you'll probably find that you need to add additional custom software,
‫or change the way that it starts, or add scripts in so that it tweaks the configuration.
‫But when I'm starting a new greenfield project, or if I'm converting some old app, I always start with
‫the official images from Docker Hub. Then once I hit some roadblocks and I'm not able to use that
‫image anymore, I might go back to Docker Hub and take another look and see if there's anything custom out
‫there that's really popular, that I can trust, and look into and see if it's going to solve my problem.
‫But there's nothing wrong with building your own.
‫It's just more work and more upkeep over time.
‫So here is super simple. We've got three stanzas.
‫You've seen the FROM before. And this time we have the WORKDIR.
‫Now that's really just basically running a cd space directory change. So you might be tempted to
‫use the run command and just type run cd to this directory and then do some things. But really the best
‫practice for Dockerfiles is to always use a separate WORKDIR stanza for whenever you're changing directories.
‫So if your file gets a little complex and you have to move back and forth in your container to do things
‫while it's building, you always want to use the WORKDIR because it's a lot easier to describe in the
‫Dockerfile what you're doing.
‫In this case, what we're actually doing here is we're changing to the default Nginx directory
‫for its HTML files.
‫In the default configuration on Docker Hub, Nginx is acting just as a web server, and it's just serving
‫static files right off the container disk.
‫The last stanza here, we have, is the copy. And this is the stanza you'll always be using to copy your
‫source code from your local machine, or your build servers, into your container images.
‫In this case, we're just taking our simple index.html and we're overwriting the file in the
‫Nginx default directory so that it's our custom home page for the web server.
‫Before we build this, you'll notice that we're missing required stanzas, like the cmd.
‫So how can we get away with that?
‫Well, there's already a cmd specified in the FROM image.
‫And when we use the FROM, we inherit everything from the Dockerfile we're 'froming,' if that's even a word.
‫This is how you can chain Dockerfiles together so that images depend on other images that depend
‫on other images.
‫All right. Let's see how this works.
‫Before we build this, let's just quickly remind ourselves what a default Nginx server does.
‫Let's do a quick docker container run -p 8080, and we're not going to need this very long so we're
‫going to do a --rm so it will delete on quitting. We don't care about the name. We're just
‫going to run Nginx. If we hop over to our browser and just type in localhost, we see the default
‫Nginx index page.
‫OK.
‫Let's remember that, and then let's just quit this container and we're going to now build our version
‫of Nginx with our extra source code file.
‫OK.
‫This one should actually go really quick.
‫You notice it only took a few seconds. That's because we already had Nginx in our image cache. And
‫all it was running was copying a very small file and changing the working directory.
‫Now let's run the exact command we just ran for Nginx. Only this time, we're going to use this
‫new image we just built.
‫Switch over to our browser, and refresh, and great.
‫Our custom index HTML is running in there and our container works.
‫If we do a quick image ls, we can see our latest image up there at the top.
‫If we actually wanted to send this up to Docker Hub, it works the same way as we did earlier.
‫We just need to re-tag it. So we can do docker image tag.
‫If you remember the help command, we can look at the format and it's always the one we're tagging
‫and the tag that we want it to then be called. So docker image tag nginx with html latest.
‫And then I'll have to call it bretfisher...like that.
‫Then if I do a docker image ls again, you'll see those top two lines are the same image ID again and
‫just tagged differently.
‫Now I could do a docker push with that repository and tag, and I could get it up on Docker Hub.