How to cache an already built docker image?

Hi all,

I’m using a 3-step sequential workflow: checkout -> build -> test

workflows:
  version: 2
  build_and_test:
    jobs:
      - checkout_code
      - build:
          requires:
            - checkout_code
      - test:
          requires:
            - build

Also, in my CI config, I’m using machine executor mode, since my project uses docker-compose.

However, I can’t find documentation on if/how I can cache my already-built Docker images from the build step to the test step. Since I don’t know how to do it, at the moment I’m just building in both of them, which I don’t like since we’re wasting resources.

This is how I’d like my configuration to be, more or less:

  build:
    machine: 
      enabled: true
      image: circleci/classic:latest
    
    working_directory: ~/project

    steps:
      - run:
          name: Build the Docker images
          command: docker-compose build
      - save_cache: #Here we save the just-built docker images

  test:
    machine: 
      enabled: true
      image: circleci/classic:latest
    
    working_directory: ~/project

    steps:

      - restore_cache: #Here we load the docker images that were built in the previous step

      - run:
          name: Put the backend up again
          command: docker-compose up -d

      - run:
          name: Run the test suite
          command: |
            docker-compose exec web bundle exec rspec

At the moment, however, I’m repeating the build process since nothing docker-related is saved between build and test.

Is it possible to cache the results of docker-compose build (or docker build, for that matter)?

Thanks!
– Félix

I made some notes on that in this blog post:

1 Like

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