Is it possible to cache images for docker-compose in CircleCI?

I’m aware how to cache dependencies in CircleCI:

- restore_cache:
    keys:
    - my-project-v1-{{ checksum "project.clj" }}
    # fallback to using the latest cache if no exact match is found
    - my-project-v1
- run: lein with-profile test deps
- save_cache:
    paths:
      - ~/.m2
    key: my-project-v1-{{ checksum "project.clj" }}

I’m also aware how to use docker-compose:

- run:
    name: Install Docker Compose
    command: |
      curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
      chmod +x ~/docker-compose
      sudo mv ~/docker-compose /usr/local/bin/docker-compose
- run: docker-compose up -d

However, every time the job runs docker-compose up -d, it downloads the images, specified in the docker-compose.yml file. Is there a way to make CircleCI download them once and then use them (until docker-compose.yml is modeified)?

This isn’t possible in any meaningful way with one exception.

If you are building an image as a part of docker-compose up, you can use Docker Layer Caching to speed up the build portion.

If you’re not building an image, but instead spinning up a stack with docker-compose this feature will not be useful.

Even if you could cache docker images, the performance gains would be minimal in my opinion. The cache is stored on S3 and needs to be downloaded for each build. If you cache a bunch of docker images the cache size would be quite large and would likely not be faster than pulling in images the normal way.

In practice, the best way to speed this up is to make the images that you are using as small as possible. Alpine based images are a good option for this (with some caveats).

1 Like

Hey @levlaz,

Thank you for your response. What you explained about the caching does make sense. I’m not building any images as part of brining a docker compose stack up, but using services such as mysql, kafka, zookeepr, etc., so I guess the Docker Leyer Caching feature won’t be useful to me.

Cheers,
Sasho

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.