Skip to main content
In this lesson, we’ll explore what a build context is and how it influences the Docker build process. Understanding build contexts helps you optimize build times and reduce image size by sending only the necessary files to the Docker daemon.

What Is a Build Context?

The build context is the set of files and folders the Docker CLI packages and sends to the Docker daemon when running docker build. By default, Docker uses the current directory (.) as the build context.
This command:
  1. Archives everything under ..
  2. Sends it to the Docker daemon.
  3. Unpacks it into a temporary directory (e.g., /var/lib/docker/tmp/...).
  4. Executes the instructions in your Dockerfile.
If you omit the -t (tag) flag, Docker builds the image and assigns the latest tag by default:

Example Dockerfile for a Flask App

Specifying a Different Build Context

You can point Docker to any local directory containing your Dockerfile:
Docker will look for /opt/my-custom-app/Dockerfile and include all files under /opt/my-custom-app in the context.

Common Context Sources

Managing Context Size with .dockerignore

Sending large or unnecessary files (logs, build artifacts) can slow down builds, especially when the daemon is remote. To prevent this, create a .dockerignore file in your context root:
Docker will exclude these paths when packaging the build context.
Be careful: missing important source files in .dockerignore can lead to build failures or incomplete images.

Remote Docker Daemon Output

When using a remote Docker daemon, you’ll see output similar to:
This confirms the context has been sent over the network before the build steps execute.

Building from a Git Repository

Docker can directly use Git URLs as the build context:
By default, Docker looks for Dockerfile at the root of the checked‐out code. Use -f to point to a different file:

Summary

  • The build context defines what files are sent to the Docker daemon.
  • Use .dockerignore to exclude unnecessary files and speed up builds.
  • You can build from local paths or Git repositories.
  • The -f flag lets you specify a non-default Dockerfile.

Watch Video