Docker Cache not working
Asked by maiermic:

I'm using docker-compose to run some pipelines. Since the compose file contains multiple services with different images, the docker cache gets to big to be cached. Hence, I only like to cache the image of the service that is build. Everything else can be downloaded from a registry. My

```
image: atlassian/default-image:4.20230906

pipelines:
  custom:
    create-dockerfile-based-cache:
      - step:
          name: Create Dockerfile based cache of backend
          runs-on:
            - self.hosted
            - linux
          script:
            - docker image ls -a
            - docker-compose -f docker-compose.yml -f docker-compose.bitbucket-pipelines.yml build backend
            - docker image ls -a
            - >
              docker image inspect $(docker image ls -aq) --format {{.Size}}
              | awk '{totalSizeInBytes += $0} END {print totalSizeInBytes}'
          caches:
            - docker
          services:
            - docker

definitions:
  services:
    docker:
      image: docker:dind
      memory: 7168

options:
  docker: true #enabling docker daemon
  size: 2x
```

I run the pipeline twice on the same commit. I expect the cache to be created on the first run and to be used on the second run, such that the build does not have to be run again.

The first run works as expected. It shows no images before building, builds from scratch, shows the images after the build

```
+ docker image ls -a
REPOSITORY  TAG     IMAGE ID      CREATED        SIZE
backend     latest  d196ad1ca1c2  4 seconds ago  1.04GB
```

and uploads the cache.

The second run downloads the cache and shows the same image ID as built in the first run. Nevertheless, it builds from scratch. The cache seems to have no effect. Why?

The Dockerfile starts with

```
FROM --platform=linux/amd64 php:8.2-apache
RUN apt-get update \
 && apt-get install ...
```

Is php:8.2-apache not part of the cache? If it is, why is the RUN command run again in the second pipeline run? The command didn't change. It should also be part of the cache.


Answers:
author: Theodora Boudale
body: Hi [@maiermic](/t5/user/viewprofilepage/user-id/5282231),

Just a heads up, I moved your post to a new question since you are reporting an issue with a completely different setup.

I tried to reproduce this and I see that docker-compose uses BuildKit, which is the reason why the cache is not used. The predefined docker cache used for caching the layers produced during Docker Build operations does not cache layers produced when using BuildKit:

* <https://support.atlassian.com/bitbucket-cloud/docs/run-docker-commands-in-bitbucket-pipelines/#Docker-BuildKit-caching-limitations>

If you want to use the Docker cache with this pipeline, you can add the following command at the beginning of your yml file's script in order to disable BuildKit:

```
- export DOCKER_BUILDKIT=0
```

Kind regards,  
Theodora

comments: author: maiermic
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thanks, are there any plans to support caching with BuildKit in a similar fashion in the future?


author: Theodora Boudale
body: Hi [@maiermic](/t5/user/viewprofilepage/user-id/5282231),

This is not on the roadmap at the moment. We have a feature request in our issue tracker:

* <https://jira.atlassian.com/browse/BCLOUD-22592>

You can add your vote to it to express your interest (by selecting the **Vote for this issue** link) and you can also add yourself as a watcher (by selecting the **Start watching this issue** link) if you'd like to get notified via email on updates.

Implementation of new features is done as per our policy [here](https://confluence.atlassian.com/support/implementation-of-new-features-policy-201294576.html) and any updates will be posted in the feature request.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud