A quick example of how to build docker image faster using Gitlab CI with DOCKER_BUILDKIT.

With a Dockerfile like this

FROM php:7.4.3-apache-buster

...

You can build your Docker image using Buildkit and Buildkit inline cache.

You can add base image as --cache-from argument to increase cache layers.

image: docker:stable

services:
  - name: docker:19.03.5-dind
    command: ["--mtu=1500"]

stages:
  - build_docker_image
  - publish_docker_image

variables:
  DOCKER_HOST: tcp://localhost:2375
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""

cache:
  key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"

build_docker_image:
  stage: build_docker_image
  tags:
    - docker
    - dind
  only:
    - master
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - DOCKER_BUILDKIT=1 docker build --pull --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from ${CI_REGISTRY_IMAGE}:latest --cache-from php:7.4.3-apache-buster -t ${CI_REGISTRY_IMAGE}:latest .
    - docker push ${CI_REGISTRY_IMAGE}:latest

Performance are increased by 20%.