A quick overview of the Dockerfile's syntax

In this section, we explain the syntax or the format of Dockerfile. A Dockerfile is made up of instructions, comments, parser directives and empty lines, as shown here:

# Comment

INSTRUCTION arguments

The instruction line of Dockerfile is made up of two components, where the instruction line begins with the instruction itself, which is followed by the arguments for the instruction. The instruction could be written in any case, in other words, it is case-insensitive. However, the standard practice or the convention is to use uppercase to differentiate it from the arguments. Let's take a relook at the content of Dockerfile in our previous example:

FROM apline:latest
CMD echo Hello World!!

Here, FROM is an instruction which has taken apline:latest as an argument, and CMD is an instruction which has taken echo Hello World!! as an argument.

The comment line

The comment line in Dockerfile must begin with the # symbol. The # symbol after an instruction is considered as an argument. If the # symbol is preceded by a whitespace, then the docker image build system would consider that as an unknown instruction and skip the line. Now, let's understand the preceding cases with the help of an example to get a better understanding of the comment line:

  • A valid Dockerfile comment line always begins with a # symbol as the first character of the line:
    # This is my first Dockerfile comment
  • The # symbol can be a part of an argument:
    CMD echo ### Welcome to Docker ###
  • If the # symbol is preceded by a whitespace, then it is considered as an unknown instruction by the build system:
    # this is an invalid comment line

A sample Dockerfile can be found at /chapter02/build_basic/ in the repo:

# Example of a really bsaicDockerfile

FROM alpine:latest
CMD echo Hello World!!

The docker image build system ignores any empty line in the Dockerfile and hence, the author of Dockerfile is encouraged to add comments and empty lines to substantially improve the readability of Dockerfile.

The parser directives

As the name implies, the parser directives instruct the Dockerfile parser to handle the content of the Dockerfile as specified in the directives. The parser directives are optional and they must be at the very top of a Dockerfile. Currently escape is the only supported directive.

We use escape character to escape characters in a line or to extend a single line to multiple lines. On a UNIX like platform, is the escape character whereas on windows is a directory path separator and ' is the escape character. By default, Dockerfile parser considers as the escape character and you could override this on windows using the escape parser directive as shown below:

# escape='
..................Content has been hidden....................

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