1

I'll make the docker build using for cache the previous image. my process is :

  • pull the old image (docker pull <my_image>)
  • build image (docker build -t ) locally, the build use the layer just pull but, the runner make all step of the docker file from scratch

So, I want to known how I can use the cache for build a new image with gitlab runner

Anyi Chen
  • 11
  • 2

1 Answers1

0

As per suggested on documentation:

image: docker:19.03.12

services:
  - docker:19.03.12-dind

variables:
  # Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  script:
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker push $CI_REGISTRY_IMAGE:latest

Save this as your gitlab-ci.yml and it'll automatically build your Dockerfile using the registry image as a cache.

Gruber
  • 124
  • 3