Each instruction in your Dockerfile results in a new image layer being created and added to your local image cache.
That image then becomes the parent for the image created by the next instruction
As Docker is processing your Dockerfile to determine whether a particular image layer is already cached it looks at two things:
the instruction being executed
and the parent image.
When executing docker build the first line of output typically reads "Sending build context to Docker deamon . . ."
The build context constitutes everything in your build directory
(the directory that you pass to the docker build command)
and is used by Docker so that you can inject local files into your image using the ADD and COPY instructions.
Docker will also check to see if the file(s) being copied have changed.
the .dockerignore file allows you to specify a list of exclusion patterns.
Any files/directories matching those patterns will be excluded from the build context.
If you know you need to install a bunch of OS packages in your image
(which is typically one of the slower parts of building an image)
put your package installation instructions toward the top of the Dockerfile.
If you have a core set of instructions that you use across all of your images
(like a MAINTAINER value you always use),
it's best to keep those at the top of your Dockerfile and always in the same order
If you have files in your build directory that change often
and are not required by your image,
you should consider adding them to .dockerignore file.
A good rule of thumb is that anything in your .gitignore is a good candidate for inclusion in your .dockerignore.
Dockerfile itself is also part of the build context