I’m building a Go app and I have a multistage Dockerfile that makes use of BuildKit statements like --mount=type=cache
For example : RUN --mount=type=cache,id=gomod,target=/go/pkg/mod go mod download
This works well between Docker build steps, but when I try to use this image to run go test as a freestyle step command, the cache directories are missing (and my test command can’t even download private dependencies again unless I set up git authentication in this step as well)
Is there any way to mount these Docker cache directories as a volume in the freestyle step ?
For now I’ve had to remove the --mount=type=cache statements completely.
I’m going to answer my own question : unfortunately that doesn’t seem possible :
--mount=type=cache does not keep files in image by design
from :
It’s a shame because it means I have to choose between :
faster local builds with --mount=type=cache enabled but it means I can’t easily use this image to run tests with docker run, it has to happen inside the Dockerfile.
being able to reuse the same Dockerfile locally and in a Codefresh testing step.
First of all I would suggest removing the mount directives. I think reusing the same Dockerfile in CI and in local development is really important
Workarounds
If your tests are simple unit tests then just run them in the Dockerfile itself (instead of a freestyle step).
Alternatively run the tests first in a freestyle step, make sure that everything is cached in /codefresh/volume by changing the gopath as explained here Pipeline caching · Codefresh | Docs Then once the Dockerfile is built, make sure that your dockeringore file does NOT prevent the cache folder to be sent as part of the docker context. So once the Dockerfile starts building all extra libraries will be there.
Frankly unless your application is really really large,it might be simpler to just let dependencies be downloaded twice (once in freestyle and once in Dockerfile).
How much time does the build spend now on dependencies?