What is a Dockerfile?

You learned in Chapter 2Learning Docker Commands, that you can run a Docker container, make modifications to that running container, and then using the docker commit command, save those changes, effectively creating a new Docker image. Although this method works, it is not the preferred way to create Docker containers. The best way to create Docker images is to use the Docker image build command with a Dockerfile that describes your desired image. 

A Dockerfile (yes, the correct spelling is all one word, with a capital D) is a text file that contains instructions used by the Docker daemon to create a Docker image. The instructions are defined using a type of value pair syntax. Each one has an instruction word followed by the parameters for that instruction. Every command gets its own line in the Dockerfile. Although the Dockerfile instructions are not case-sensitive, there is a well-used convention that the instruction word is always uppercase. 

The order of the instructions in the Dockerfile is significant. Instructions are evaluated in sequential order, starting at the top of the Dockerfile and finishing at the bottom of the file. If you recall from Chapter 1Setting up a Docker Development Environment, Docker images are made up of layers. All of the instructions found in the Dockerfile will result in a new layer being generated as the Docker image is built, however, some instructions will only add a zero-byte-sized metadata layer to the created image. Since it is a best practice to keep Docker images as small as possible, you will want to use instructions that create non-zero-byte-sized layers as efficiently as possible. In the following sections, we'll note where using an instruction creates a non-zero-byte-sized layer, and how to best use that instruction to minimize the number and size of layers. Another important consideration is the ordering of the instructions. Certain instructions must be used before others, but with those exceptions, you can place the other instructions in any order you please. The best practice is to use instructions that change least early in the Dockerfile, and instructions that change more frequently in the later part of the Dockerfile. The reason is that when you need to rebuild an image, the only layers that get rebuilt are the ones that are at, or after, the first line changed in the Dockerfile. If you don't understand this yet, don't worry, it will make more sense once we see some examples. 

We will review the build command at the end of this section, but we will start with the instructions available to the Dockerfile first, beginning with the instruction that has to be the first instruction in your Dockerfile: the FROM instruction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset