liveBook · Manning

Build context

As explained in Learn Docker in a Month of Lunches, Second Edition

A build context is the directory and its contents supplied to Docker as input for building a container image.

A build context is the directory supplied to Docker for an image build. In `docker image build --tag web-ping .`, the final `.` selects the current directory, where the Dockerfile and related application files are located. If the build cannot find them, check your working directory and make sure the final argument is present. section 3.3

Listing 11.4 Docker Compose build settings and reusable arguments
x-args: &args
  args:
    BUILD_NUMBER: ${BUILD_NUMBER:-0}
    BUILD_TAG: ${BUILD_TAG:-local}

services:
  numbers-api:
    build:
      context: numbers
      dockerfile: numbers-api/Dockerfile.v4
      <<: *args

  numbers-web:
    build:
      context: numbers
      dockerfile: numbers-web/Dockerfile.v4
      <<: *args

Docker Compose can set the context to `numbers` while selecting Dockerfiles in its `numbers-api` and `numbers-web` subdirectories. This keeps the source directory and Dockerfile path explicit for each service. section 11.3

Docker sends the build context to the engine with the Dockerfile, so unnecessary files can slow a build. A precise `COPY ./app ./app` avoids putting unrelated files in the image. For a complex directory layout, `.dockerignore` can exclude files from the context instead. Deleting unwanted files in a later image layer does not recover the space they already occupy. section 17.1

Try a smaller context

Build the example image, add the supplied `.dockerignore`, then rebuild with a new tag and compare the context sent. The ignored files are no longer sent to the engine. section 17.1

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Defines the CLI directory argument and explains context transfer and exclusions, without detailing BuildKit internals.
Examples
Contrasts a Compose context with nested Dockerfiles and image builds using precise COPY or .dockerignore.
What it assumes
Assumes basic terminal navigation and a Dockerfile, while defining the build command’s final directory argument.
What you can do after
Operate/configure it by selecting a CLI or Compose context and excluding unnecessary files with .dockerignore.
How it connects
Connects build context to Dockerfile paths, Docker Compose, COPY instructions, and .dockerignore.

Also explained in

Compare how 2 books explain build context →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.