I wrote original content here: https://github.com/kimh/circleci-build-recipies/tree/better-docker-cache
The following circle.yml demonstrates how we can use Docker Hub for caching.
machine: services: - docker dependencies: pre: - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS override: - docker pull kimh/build-image:latest || true test: override: - docker build -t kimh/build-image:latest . - docker push kimh/build-image:latest
Here is Dockerfile.
FROM phusion/passenger-ruby21:latest RUN apt-get update RUN apt-get install sl WORKDIR /tmp ADD Gemfile /tmp/ ADD Gemfile.lock /tmp/ RUN bundle install
What we are doing in
circle.yml is very simple:
- Pull image from Docker Hub that was pushed from previous builds
- Build image from Dockerfile
- Push the image back to Docker Hub for the subsequent builds
In short, we are using Docker Hub as our image cache store.
In the first build,
docker build took about 3 mins and
docker push took 2 mins, but both commands finish immediately in the second build thanks to cache.