Member-only story
Docker Tips: All About the Build Context
Understand what the build context is and how to optimize it

What Is the Build Context?
Let’s start with the command used to build a Docker image:
$ docker build [OPTIONS] PATH | URL | -
The build context is the set of files located at the specified PATH or URL. Those files are sent to the Docker daemon during the build so it can use them in the filesystem of the image.
Let’s illustrate this.
Using a path
Let’s suppose I’m in the folder /Users/luc/src/github.com/lucj/genx
containing the source code of the genx application (simple Go application that generates dummy data).
Usually, we use a command like the following one to build the image, the Dockerfile being at the root of the project’s folder:
$ docker image build -t genx:1.0 .
In that case, the build context is the content of the current folder (“.” specified as the last element of the command).
Using a URL
This same genx project is managed in GitLab, so it’s possible to build an image locally referencing the GitLab repository:
$ docker image build…