Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i cache in gitlab ci while building docker images with docker:dind

I have a gitlab-ci.yml like this:

build and push docker image:
  stage: publish  
  variables:
    DOCKER_REGISTRY: amazon-registry
    AWS_DEFAULT_REGION: ap-south-1
    APP_NAME: sample-app
    DOCKER_HOST: tcp://docker:2375
  image: 
    name: amazon/aws-cli
    entrypoint: [""]
  services:
    - docker:dind 
  before_script:
    - amazon-linux-extras install docker
  script:
    - docker build -t $DOCKER_REGISTRY/$APP_NAME:master .
    - aws ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_REGISTRY
    - docker push $DOCKER_REGISTRY/$APP_NAME:master

This step takes 19=8 minutes to complete since the docker image steps are not cached. I want to be able to cache the before_script amazon-linux-extras install docker as well as the docker image I'm building. We are running on our own gitlab runners. I've searched for answers but found 4 years old solutions. Is there a way to figure this out ? Also, will switching away from docker:dind help ?

like image 444
ssbb191 Avatar asked Dec 31 '20 07:12

ssbb191


People also ask

Are Docker images cached?

Docker uses a layer cache to optimize and speed up the process of building Docker images. Docker Layer Caching mainly works on the RUN , COPY and ADD commands, which will be explained in more detail next.

What is Docker build cache?

Docker's build-cache is a handy feature. It speeds up Docker builds due to reusing previously created layers. You can use the --no-cache option to disable caching or use a custom Docker build argument to enforce rebuilding from a certain step.

What is cache in GitLab CI?

all tiers. A cache is one or more files a job downloads and saves. Subsequent jobs that use the same cache don't have to download the files again, so they execute more quickly.


1 Answers

One thing I have tried is to use cache layer in docker build.
You could pull exist image from your registry, and then build with --cache-from parameter.

The job shell would be like this:

  variables:
    IMAGE_TAG: $DOCKER_REGISTRY/$APP_NAME:master
  script:
    - aws ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_REGISTRY
    - docker pull $IMAGE_TAG || true
    - docker build --cache-from $IMAGE_TAG -t $IMAGE_TAG .
    - docker push $IMAGE_TAG

This method is mentioned in gitlab-ci official document as well.

like image 157
Carl Tsai Avatar answered Sep 29 '22 06:09

Carl Tsai