Docker – The FROM instruction

How to change the time zone on Windows Server 2019

Every Dockerfile must have a FROM instruction, and it must be the first instruction in the file. (Actually, the ARG instruction can be used before a FROM instruction, but it is not a required instruction. We will talk more about that in the ARG instruction section.) 

The FROM instruction sets the base for the image being created and instructs the Docker daemon that the base of the new image should be the existing Docker image specified as the parameter. The specified image can be described using the same syntax we saw in the Docker container run command from Chapter 2, Learning Docker Commands. Here, it’s a FROM instruction that specifies using the official nginx image with a version of 1.15.2:

# Dockerfile
FROM nginx:1.15.2

Note that in this example, there is no repository specified that indicates that the specified image is the official nginx image. If no tag is specified, the latest tag will be assumed.

The FROM instruction will create the first layer in our new image. That layer will be the size of the image specified in the instruction’s parameter so it is best to specify the smallest image that meets the criteria needed for your new image. An application-specific image, such as nginx, is going to be smaller than an OS image, such as ubuntu. And, the OS image for alpine will be much smaller than images of other OSes, such as Ubuntu, CentOS, or RHEL. There is a special keyword that can be used as the parameter to the FROM instruction. It is scratch. Scratch is not an image that you can pull or run, it just a signal to the Docker daemon that you want to build an image with an empty base-image layer. The FROM scratch instruction is used as the base layer for many other base images, or for specialized app-specific images. You have already seen an example of such a specialized app image: hello-world. The full Dockerfile for the hello-world image looks like this:

# hello-world Dockerfile
FROM scratch
COPY hello /
CMD ["/hello"]

We will discuss the COPY and CMD instructions shortly, but you should get a sense of how small the hello-world image is based on its Dockerfile. In the world of Docker images, smaller is definitely better. Take a look at the size of some images for reference:

Comments are closed.