Imagine you run a CI pipeline that builds a Docker image every time you push code. Why is using Docker layer caching helpful in this scenario?
Think about what happens if you rebuild the same parts of the image repeatedly.
Docker layer caching saves time by reusing layers that did not change, so the build only processes new or changed parts.
What will be the output status of the following Docker build step if the layer is cached?
Step 3/5 : RUN apt-get update && apt-get install -y curl ---> Using cache
Look at the phrase '---> Using cache' in the output.
'---> Using cache' means Docker found a matching layer and reused it without rerunning the command.
Which GitHub Actions step correctly enables Docker layer caching using the official cache action?
name: Build and cache Docker image
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build image
run: |
docker build --cache-from=type=local,src=/tmp/.buildx-cache \
--cache-to=type=local,dest=/tmp/.buildx-cache-new .
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cacheCheck if the cache action and docker build commands use matching cache paths.
This setup uses a local directory to store cache and passes it to docker build with proper options, enabling layer caching.
You notice your CI builds always take a long time and never reuse cached Docker layers. Which reason below explains this behavior?
Think about how Docker cache invalidation works with file changes.
Placing 'COPY .' before installing dependencies causes cache invalidation if any source file changes, forcing rebuild of all later steps.
You have a multi-stage Dockerfile used in CI. Which practice best improves layer caching efficiency across stages?
Think about which parts of the build change often and which stay stable.
Keeping stable dependencies in early stages allows Docker to cache them once, while changing files copied later avoid invalidating earlier cached layers.