Yes, you can—but it requires setting up external caching mechanisms rather than just relying on Docker’s layer cache. Here are two common approaches:
1. Use an HTTP(S) Caching Proxy
Overview:
Set up a caching proxy (such as Squid, Varnish, or even specialized tools like apt-cacher-ng) on your network or build host. Then, configure your Docker build so that all outgoing HTTP/HTTPS requests (from curl, some git operations, etc.) are routed through this proxy. This way:
- The proxy will cache the responses, so if the same request is made again (even if a previous build step has changed and forces re-running later steps), the data will be served from the cache.
- You can inspect the logs on your proxy to see exactly what was downloaded.
How to Implement:
-
Set Up the Proxy:
For example, to set up Squid, install it on your host system and configure it to cache the kinds of traffic you expect (HTTP/HTTPS, maybe even Git if you use HTTPS clones).
-
Configure the Build Environment:
In your Dockerfile (or via build arguments), set environment variables so that your build tools use the proxy. For example:
ENV HTTP_PROXY="http://your-proxy-address:3128"
ENV HTTPS_PROXY="http://your-proxy-address:3128"
This ensures that tools like curl or git clone https://… use the proxy.
-
Use HTTPS Where Possible:
Some tools (like Git) support different protocols. To take full advantage of HTTP caching, use HTTPS URLs where possible so that the proxy can intercept and cache the responses.
2. Use BuildKit’s Cache Mounts
Overview:
If you’re using Docker BuildKit (which is enabled by default in recent Docker versions), you have the option to use cache mounts with the --mount=type=cache directive in your RUN instructions. This helps you persist directories between build steps.
How It Helps:
Even if you change an early layer that invalidates Docker’s layer cache, the cached data in the mounted directory remains, so subsequent build steps don’t have to re-fetch everything.
Example:
# syntax=docker/dockerfile:1.3
FROM ubuntu:20.04
# Install curl and build dependencies
RUN apt-get update && apt-get install -y curl git
# Use a cache mount for downloaded files
RUN --mount=type=cache,target=/var/cache/mydownloads \
curl -L -o /var/cache/mydownloads/somefile.tar.gz https://example.com/somefile.tar.gz
# Later steps can re-use the /var/cache/mydownloads directory
RUN ls -l /var/cache/mydownloads
In this example, even if you change an earlier part of the Dockerfile that forces the subsequent RUN commands to re-execute, the /var/cache/mydownloads content is still cached for the build. You might need to adjust this approach depending on your tools (e.g., for Git caches or other kinds of downloads).
Combining Approaches
You might even find it useful to combine both methods:
- Proxy Caching: For all HTTP/HTTPS downloads.
- BuildKit Cache Mounts: For non-HTTP cache directories (such as Git clone caches if you can force it to use a local cache directory, or for reusing extraction artifacts, etc.).
This combination gives you:
- An external proxy log (so you know exactly what external artifacts and URLs were used) and cached responses.
- A persistent internal cache within the Docker build that isn’t invalidated by changes in earlier steps.
Final Thoughts
Keep in mind:
- Non-HTTP(s) Protocols: Tools like Fossil or Git configured to use non-HTTP protocols may not benefit from an HTTP caching proxy. In those cases, see if they can be configured to use HTTPS or if there are similar caching mechanisms available.
- Deterministic Builds: For fully reproducible builds, it’s also good practice to record checksums or versions of external resources. The caching proxy helps you replay builds faster but isn’t a substitute for version pinning.
- Cache Invalidation: If a resource on the web changes, the proxy (or your cache mount) may still serve the old version. Ensure your caching strategy (e.g., cache expiration rules in Squid) is set according to your use case.
By using these techniques you can achieve both a performance boost via caching and better transparency into what exactly is being pulled in during your Docker build process.