title: How do i cache in gitlab ci while building docker images with docker:dind
body: <p>I have a <code>gitlab-ci.yml</code> like this:</p>
  <pre class="lang-yaml prettyprint-override"><code>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: [&quot;&quot;]
    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
  </code></pre>
  <p>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 <code>amazon-linux-extras install docker</code> 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 <code>docker:dind</code> help ?</p>