GitLab CI/CD with Docker 24.x—best practices for caching layers?
We're running GitLab CI on Hetzner with Docker 24.0.6, and our pipeline is taking 18 minutes per build. Most of the time is spent rebuilding Docker layers that haven't changed.
Currently using:
docker build -t myapp:latest .
docker push myapp:latest
I've looked at BuildKit and layer caching, but I'm unsure about the best approach for GitLab CI specifically. Should I be using:
DOCKER_BUILDKIT=1with inline cache?- A private registry for intermediate stages?
- Something else entirely?
Anyone running similar setups? What's your go-to strategy?
Edited at 25 Mar 2026, 21:51
BuildKit with inline cache is solid, but here's the real win: use type=registry cache backend in your .gitlab-ci.yml. Push intermediate layers to your registry and GitLab CI will reuse them automatically. Set DOCKER_BUILDKIT=1 and add --cache-from type=registry,ref=myapp:buildcache to your build command. Way faster than inline and doesn't bloat your main image. We cut our build times from 20min to 5min with this approach on Hetzner.
18 min is rough. tbh, skip inline cache for now—it's flaky in CI. Instead, set up a local registry mirror on your Hetzner box using Docker's --cache-from flag pointing to your last build image. Also make sure you're using .dockerignore aggressively—a lot of time gets wasted on unnecessary COPY layers. You can also split your Dockerfile into a separate base layer that rarely changes and push that independently.
Thanks both! Yeah, the type=registry cache backend sounds promising—I'll try that first since we're already pushing to GitLab's registry anyway. @yaml_warrior, good point about inline cache being flaky; I was worried about that. Will test the registry approach before spinning up a local mirror.