Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rough guide on how to cache a docker image #6288

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/pipelines/caching/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,52 @@ steps:
> [!NOTE]
> In this example, the key is a fixed value (the OS name) and because caches are immutable, once a cache with this key is created for a particular scope (branch), the cache cannot be updated. This means subsequent builds for the same branch will not be able to update the cache even if the cache's contents have changed. This problem will be addressed in an upcoming feature: [10842: Enable fallback keys in Pipeline Caching](https://github.com/microsoft/azure-pipelines-tasks/issues/10842).

## Docker

You can use the caching task in combination with `docker image save` and `docker load` to speed up your docker builds.

### Example

```yaml
- task: CacheBeta@0
inputs:
key: docker-image | path/to/dockerfile/**
path: ".dockercache"
restoreKeys: docker-image
cacheHitVar: DOCKER_CACHE_HIT
displayName: Cache docker layers
```

The following can be combined with the following script.

```bash
#! /bin/bash

set -e
set -x

# Create .dockercache directory
mkdir -p ./.dockercache/

# Import containerimage from cache to speed up build if there was a version in cache
if [ -f ".dockercache/containerimage.tar" ];
then
echo "-------> Restoring docker image"
time docker load -i .dockercache/containerimage.tar
fi

echo "-------> Building containerimage"
# Build the docker image using the `docker load`'d image for cache
time docker build --cache-from containerimage:latest -t containerimage -f ./Dockerfile ./.containerimage

# If the current cached image is out of date save containerimage so it can be cached
if [ $DOCKER_CACHE_HIT != "true" ];
then
echo "-------> Saving docker image"
time docker image save -o ./.dockercache/containerimage.tar containerimage
fi
```

## Maven

Maven has a local repository where it stores downloads and built artifacts. To enable, set the `maven.repo.local` option to a path under `$(Pipeline.Workspace)` and cache this folder.
Expand Down