Sub topic 14
Git URL, tarball, stdin and named contexts
This chapter assumes you know that the PATH argument of docker build selects the build context and that COPY resolves paths against it. PATH is usually a local directory, but the context can come from other places too.
Other places a context can come from
docker build accepts a directory, a URL, or - for stdin. Buildx adds named contexts on top of these.
| Source | Example argument | Typical use |
|---|---|---|
| Git repository | `https://github.com/acme/app.git#main:app` | Build a branch or tag without cloning it yourself |
| Remote tarball | `https://example.com/context.tar.gz` | Build from an archive published by another job |
| Tar archive on stdin | `- < context.tar.gz` | Build a context that a script generated |
| Dockerfile on stdin | `-f- .` | Use a generated Dockerfile with a local context |
| Named context | `--build-context lib=../lib` | Pull extra directories or images into one build |
Git URL contexts
The builder fetches the repository and uses its files as the context. A fragment after # selects the ref and an optional subdirectory, in the form #<ref>:<dir>.
# Default branch, repository root as the context
docker build -t app https://github.com/acme/app.git
# Branch "release" and subdirectory "services/api" as the context
docker build -t api "https://github.com/acme/app.git#release:services/api"
# A tag
docker build -t app "https://github.com/acme/app.git#v1.4.0"Quote the URL so your shell does not treat # as a comment. A .dockerignore committed in the repository still applies. Only committed files exist in this context, so uncommitted changes and untracked files on your machine are never included. For a private repository, pass credentials with BuildKit's SSH forwarding (--ssh default) or a secret. Do not put tokens in the URL, because they appear in shell history and build logs.
Tarball and stdin contexts
If the argument is a URL that is not a Git repository, the builder downloads it and unpacks it as a tar archive. You can also stream an archive in yourself:
tar -czf context.tar.gz -C ./app .
docker build -t app - < context.tar.gzThe Dockerfile must be at the root of the archive, or you must name it with -f.
Streaming only a Dockerfile is a different case:
docker build -t hello - <<'EOF'
FROM busybox
CMD ["echo", "hello"]
EOFThis build has no context at all, so any COPY of a local file fails. To read the Dockerfile from stdin and keep a real context, use -f- and give a PATH:
docker build -t app -f- . <<'EOF'
FROM node:20-slim
WORKDIR /app
COPY . .
CMD ["node", "server.js"]
EOFNamed contexts
A normal build has one context. Named contexts let a Dockerfile refer to additional sources by name. They need BuildKit through docker buildx build, which is also what docker build uses in current Docker releases.
docker buildx build -t api \
--build-context shared=../shared \
--build-context tools=https://github.com/acme/tools.git#main \
--build-context alpine=docker-image://alpine:3.20 \
.FROM alpine
COPY --from=shared /config ./config
COPY --from=tools /bin/lint /usr/local/bin/lintHere FROM alpine uses the pinned image instead of pulling alpine from the registry, because the name matches. COPY --from=shared reads from the named directory, which sits outside the main context. This is a cleaner alternative to widening the context to a repository root just to reach a sibling folder.
Exercise
A teammate runs docker build -t app - < Dockerfile and gets an error on COPY package.json ./. Why does it fail, and what is the smallest change that fixes it?
Show answer
Passing - with only a Dockerfile on stdin means the build has no context, so there are no files for COPY to read. Keep the Dockerfile on stdin but supply a context directory by running docker build -t app -f- . < Dockerfile. Alternatively, run docker build -t app . and let the builder read the Dockerfile from the context.
Exercise
You need COPY to read files from ../shared, which is outside your context. Name one option other than moving the context to the parent directory.
Show answer
Pass --build-context shared=../shared to docker buildx build, then use COPY --from=shared /path ./path in the Dockerfile.