In the previous blog post I described how to define GitHub Actions pipeline to benefit from caching Rust dependencies and container images’ layers. But the final result may also depend on your Dockerfile. Namely,
Docker Buildx Action
supports
BuildKit
and
buildx
and in order to benefit from this, your Dockerfile
has to explicitly cache layers. Actually, this is quite easy to achieve, let’s see the example:
|
|
The crucial part here is the annotation # syntax=docker/dockerfile:1.4
, this will turn on the advanced features. ARG TARGETPLATFORM
is optional and will be used just if you have matrix build with different platforms. If you build for single platform, you can remove this line and also ,id=${TARGETPLATFORM}
from the remaining lines of the Dockerfile
. The next important point is --mount=type=cache,target=/usr/local/cargo/registry
. This defines which path should be actually cached as container image layer. Replace <your-crate-name>
placeholder with your real crate name and you are ready to go. If you expose your service on a different port than 3000, please modify the final line EXPOSE 3000
to the correct port, or if there are no exposed ports, just delete the line.
If you haven’t yet achieved the speedup of GitHub Actions pipelines explained in my previous post, this modification will further accelerate your build. It is important to mention that these improvements won’t affect just GitHub Actions builds, but any builds where BuildKit
is enabled, including your local builds. In order to activate this on your local machine, just run this command in terminal:
|
|
And if you don’t like the new output during the build, you may revert to previous behavior:
|
|
To preserve the settings, add these commands to your .bashrc
or .zshrc
file.
In version 1.4 of the Dockerfile syntax, there are even more available options. To find out more information, click here .