A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is essentially the build instructions to build the image itself. Docker can build images automatically by reading the instructions from a Dockerfile. The Dockerfile is processed sequentially to assemble the image when the user executes the docker build
command. The basic syntax of a Dockerfile is as follows:
# Comment
INSTRUCTION arguments
The instruction is not case-sensitive, but convention is for them to be uppercase to distinguish them from arguments more easily. Docker runs instructions in a Dockerfile in order, and a Dockerfile must begin with a FROM
instruction. The FROM
instruction specifies the base image to use for the subsequent instructions. Other common instructions in a Dockerfile include RUN
, COPY
, ADD
, CMD
, and ENTRYPOINT
.
The advantage of using a Dockerfile over just storing the binary image is that the automatic builds will ensure that the latest version is available, which is beneficial from a security perspective. Dockerfiles can also set environment variables that can be used in the Dockerfile and any scripts that it calls.