A one-line edit to a Phoenix template shouldn't send CI back through mix deps.compile. Our Docker-in-Docker (DinD) jobs start with a fresh Docker daemon, so we keep the build cache in GitLab's Container Registry. The Dockerfile here is for Elixir/Phoenix. The GitLab job works the same way for any image you build with Buildx in DinD.
Dockerfile: copy dependencies first
In the builder stage, once Elixir, Hex, and Rebar are in place, keep COPY lib below mix deps.compile:
WORKDIR /app
ENV MIX_ENV=prod
COPY mix.exs mix.lock ./
RUN mix deps.get --only prod
COPY config/config.exs config/prod.exs config/
RUN mix deps.compile
COPY lib lib
COPY priv priv
COPY assets assets
RUN mix compile && mix assets.deploy
COPY config/runtime.exs config/
RUN mix release
An edit under lib/ still rebuilds the app, but it doesn't invalidate the dependency layer. The shipped image contains the release, not the layers that built it. That's why we use Buildx's registry cache with mode=max rather than relying on the release image or GitLab's cache: archives.
.gitlab-ci.yml: give the cache its own tag
Start with the docker:dind service, registry login, and docker-container builder from GitLab's Docker layer caching guide. Push the cache to :buildcache and the release to its own tag:
docker_image:
script:
- |
cache="$CI_REGISTRY_IMAGE:buildcache"
docker buildx build --progress=plain \
--cache-from "type=registry,ref=$cache" \
--cache-to "type=registry,ref=$cache,mode=max" \
--tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --push .
If :buildcache doesn't exist yet, the first build creates it. After a source-only edit, check for CACHED on mix deps.get and mix deps.compile.
Skip cache exports on source-only pushes
We got the dependency cache hits we wanted, but Buildx was still exporting a cache on every build. For a source-only push, we can leave the cache tag alone and still push the new release image. Here's the replacement script. It needs git in the job image.
docker_image:
script:
- |
cache="$CI_REGISTRY_IMAGE:buildcache"
before="${CI_COMMIT_BEFORE_SHA:-}"
set --
if [ "${CI_PIPELINE_SOURCE:-}" = push ] &&
[ -n "$before" ] &&
git merge-base --is-ancestor "$before" "$CI_COMMIT_SHA" 2>/dev/null &&
git diff --quiet "$before" "$CI_COMMIT_SHA" -- \
Dockerfile .dockerignore .gitlab-ci.yml mix.exs mix.lock config/ &&
docker buildx imagetools inspect --raw "$cache" >/dev/null 2>&1; then
echo 'Importing the existing cache without exporting it'
else
set -- --cache-to "type=registry,ref=$cache,mode=max"
fi
docker buildx build --progress=plain \
--cache-from "type=registry,ref=$cache" "$@" \
--tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --push .
Use the whole push for the diff: the last commit might only change a comment even though an earlier commit changed mix.lock. Adjust the path list to match your build inputs. If a shallow checkout leaves you without the push base, or the registry can't confirm the cache tag exists, export the cache. In our source-only run, omitting --cache-to left the Mix and apt steps cached, and the release image still went to the registry.