docker-reuse is a tool for building and publishing Docker images. It has two
related and complementary purposes:
-
Make the image content-addressable by tagging it with a fingerprint computed from the sources referenced in the Dockerfile.
As a result, the image tag never changes unless the sources have changed, and so Kubernetes (or another orchestration system) won't have to restart the containers that use that image.
-
Save time and resources by bypassing the lengthy build and push operations if not a single source has changed.
This performance improvement is less significant for the local Docker builds, which are relatively fast if the sources did not change. However, for the environments where Docker layer caching is not available (like Google Cloud Build), knowing when it is safe to skip the entire build can be a huge time-saver.
Here's how docker-reuse works:
- It computes a 160-bit fingerprint from the Dockerfile sources.
- It attempts to find a previously built image in the registry using the fingerprint as a tag.
- If no such image exists, the tool builds it and pushes it to the registry.
- In either case,
docker-reuseupdates all references to the image in a user-provided template file(s) to contain this exact image tag.
docker-reuse [OPTIONS] PATH IMAGE [ARG...]
Positional arguments are:
-
PATHDocker build context directory
-
IMAGEName of the image to find or build
-
[ARG...]Optional build arguments (Format:
NAME[=value]). If the value is not provided, it is taken from the environment variable having the same name as the build argument.
Options:
-
-f, --dockerfile=FILEPathname of the Dockerfile (Default is
PATH/Dockerfile) -
-p, --placeholder=PLACEHOLDERPlaceholder for the new image name in the file specified by
--templateor--update-in-place(by default, the image name itself, including the tag). -
--template FILETemplate file to use for the next
--write-tooperation. -
--write-to FILEFile to overwrite with the template file's content where the placeholder is replaced with the new image tag. Requires
--templateto be specified earlier in the command line. -
-u, --update-in-place=FILEFile to read and update in place with the new image tag (equivalent to
--templateand--write-topointing to the same file). Can be specified multiple times. -
-q, --quietSuppress build output
-
-t, --tag=TAGAdditional tag to use for the image (by default, only the 160-bit fingerprint computed from the image sources is used). Can be specified multiple times.
-
-m, --mode=MODEFingerprinting mode — one of the following:
commit— use the commit hash as the fingerprintsha1— compute the SHA1 hash of the source files and use the combined hash as the fingerprintauto— use the commit hash if available, otherwise fall back tosha1
-
--platform PLATFORMTarget platform for the image (e.g.,
linux/amd64). -
--check-local-cacheIf the target image already exists in the local cache, assume it also exists in the registry and skip pushing it to the registry. This is an optimization shortcut for faster local builds and should be used with caution.
docker-reuse \
-f ./docker/myapp/Dockerfile \
--update-in-place ./kubernetes/myapp/deployment.yaml \
-t v1.0.0 \
-m sha1 \
./src/myapp \
mydockerhubid/myapp
When used as a community Cloud Build
builder,
docker-reuse replaces the docker builder steps as well as the images
field in cloudbuild.yaml.
Here's an example of a trivial but complete cloudbuild.yaml:
steps:
- id: build-and-push
name: gcr.io/$PROJECT_ID/docker-reuse
args: [
"-f",
"docker/hello-world/Dockerfile",
"--update-in-place",
"kubernetes/hello-world/deployment.yaml", # the file to update
"-p",
"IMAGE_PLACEHOLDER", # the string to replace in deployment.yaml
".",
"gcr.io/$PROJECT_ID/hello-world", # the image to build
"GREETING=Hello, World!", # build-arg value is provided
"PORT", # build-arg value is taken from the environment
]
env:
- "PORT=8080"
timeout: 900s
- id: deploy
waitFor: ["build-and-push"]
name: gcr.io/cloud-builders/kubectl
args: ["apply", "-k", "kubernetes/hello-world"]
env:
- "CLOUDSDK_COMPUTE_ZONE=${_CLUSTER_ZONE}"
- "CLOUDSDK_CONTAINER_CLUSTER=${_CLUSTER_NAME}"
substitutions:
_CLUSTER_ZONE: us-east4-b
_CLUSTER_NAME: cluster-1
Additional information and working examples can be found on the community builder page.