‫Let's dive into the Dockerfile, which is actually a recipe for creating your image.
‫So far, all of the Docker images we've been using were created from Dockerfiles where you can go look
‫at them on Docker Hub.
‫If you've cloned my repository from the resources listed in the first section you should see a
‫docker sample file 1 directory, and in there we should have just one file. A Dockerfile.
‫We're going to go line by line and talk about these different stanzas. A Dockerfile seems to look
‫like a shell script, but it's not.
‫It's not a batch file, it's not a shell script. It's a totally different language of file that's unique
‫to Docker and the default name is Dockerfile with a capital D.
‫But, from the command line, whenever you need to deal with a Dockerfile using the docker command, you can
‫actually use the dash f. Which is actually common amongst a lot of the tools with Docker, you can use
‫dash f to specify a different file than a default.
‫So, first up is the from command. The from command is in every Dockerfile. It's required to be there.
‫It's normally a minimal distribution.
‫In this case it's Debian.
‫But a lot of times nowadays it's going to be Alpine.
‫And really, the reason you would use these is to save yourself time and pain. Because these minimal distributions
‫are actually much smaller than the CDs you would use to install a virtual machine from them.
‫For example, the Ubuntu one doesn't even have cURL in it, whereas obviously, if you installed
‫a full Ubuntu installation on a VM it would have cURL and a lot of other commands already installed.
‫Because all of these distributions are official images, it means that they're going to be always up
‫to date with the latest security patches and you can depend and trust on them.
‫And one of the main benefits for using them in containers is to use their package distribution systems
‫to install whatever software you need in your package.
‫In this case, we're actually using the Jessie release, which is what's after the colon.
‫And, as you remember from our Docker Hub conversation, there's lots of different options you can specify
‫here for the Debian repository.
‫Next, we have the ENV stanza, and ENV is for environment variables.
‫It's a way to set environment variables, which are actually very important in containers because they're
‫the main way we set keys and values for container building and for running containers.
‫In this case, it's actually setting the version of Nginx it would like us to install and this environment
‫variable will be set so that any subsequent lines will be able to use it.
‫Now, as a reminder from previous lectures, each one of these stanzas is an actual layer in our Docker
‫image.
‫So the order of them actually matters, because it does work top down.
‫As you'll see by this relatively long run command here for the next one. You’ll see that it looks like
‫one long command.
‫What it is doing is really just executing shell commands inside this container as it's building
‫it.
‫You'll usually see run commands when you need to install software with a package repository, or you
‫need to do some unzipping, or some file edits inside the container itself.
‫Run commands can also run shell scripts that you've copied in earlier in the file or any commands that
‫you can access from inside the container
‫at that point in time in the file.
‫Since we're coming from Debian, this run command has access to all the commands and binaries that
‫would have been installed with that release.
‫And in this one for Nginx, this list of commands is more or less adding a key to the repository where
‫you can get the package to install the latest Nginx from the official Nginx repository.
‫There's a few things here that are really key in making good Dockerfiles and we'll actually talk
‫about this more in a later section where we talk about best practices.
‫But two things to note.
‫The reason that we're adding all these commands with the double ampersand here, so that they're chained
‫one after the other, is because, if you remember, each stanza is its own layer.
‫What this does is ensures that all of these commands are fit into one single layer.
‫It saves us a little time.
‫It saves us space. And it's so common that you'll probably see it in every Dockerfile on Docker Hub.
‫Okay, on line 27 we've got another run command.
‫This one is all about pointing our log files to the stdout
‫and to the stderr.
‫We'll see later, the proper way to do logging inside a container is to not log to a log file. And there's
‫no syslogd or any other syslog service
‫inside a container. Docker actually handles all of our logging for us. All we have to do inside the
‫container is make sure that everything we want to be captured in the logs is spit to stdout and stderr
‫and docker will handle the rest.
‫There's actually logging drivers that we can use in the Docker Engine itself to control all the logs
‫for all the containers on our host.
‫And so that's really what you want to do with them.
‫It adds more complexity to your app if your app is actually doing the logging itself.
‫And then, if you have to deal with files in every container, now you've got a problem of how do you get those
‫files out, and searchable, and accessible. Here, we're taking the default Nginx logs and we're actually
‫linking them to the stdout.
‫So that Docker can capture them.
‫We also have an expose command.
‫By default, no TCP or UDP ports are open
‫inside a container. It doesn't expose anything from the container to a virtual network unless we list
‫it here.
‫And, of course, because this is a Web and proxy server it's going to expose 80 and 443.
‫Now, this expose command does not mean these ports are going to be opened automatically on our host.
‫That's what the dash P command is whenever we use docker run.
‫Lastly, we have the CMD here. The CMD is a required parameter that is the final command that will
‫be run every time you launch a new container from the image, or every time you restart a stopped container.
‫There is some really excellent documentation on all of these stanzas, plus a whole lot more, that
‫we're going to go into later, on the Docker documentation website at docs.docker.com,
‫which you can find in the resources of this lecture.
‫Okay, so these are the basics, right? These five different stanzas are pretty normal in every single Dockerfile.
‫Some of them are required.
‫Some of them, like the run, and the ENV, and the expose, are optional, but they're pretty typical for most
‫containers that you're going to create images for.