0
0
Dockerdevops~20 mins

Docker layer caching in CI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker Layer Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use Docker layer caching in CI pipelines?

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?

AIt automatically pushes the image to the registry without building.
BIt deletes all previous images to free up disk space before building.
CIt speeds up builds by reusing unchanged layers, reducing build time and resource use.
DIt forces the build to always start from scratch to avoid stale layers.
Attempts:
2 left
💡 Hint

Think about what happens if you rebuild the same parts of the image repeatedly.

💻 Command Output
intermediate
1:00remaining
Output of Docker build with cache hit

What will be the output status of the following Docker build step if the layer is cached?

Docker
Step 3/5 : RUN apt-get update && apt-get install -y curl
---> Using cache
AThe step runs again and downloads packages.
BThe step is skipped and the cached layer is reused.
CThe build fails due to cache conflict.
DThe step is ignored and not included in the image.
Attempts:
2 left
💡 Hint

Look at the phrase '---> Using cache' in the output.

Configuration
advanced
2:30remaining
Configuring Docker layer caching in GitHub Actions

Which GitHub Actions step correctly enables Docker layer caching using the official cache action?

Docker
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-cache
AThe given configuration correctly caches Docker layers between builds.
BThe cache action is missing the 'restore-keys' field, so caching won't work.
CThe docker build command lacks '--cache-from' option, so no cache is used.
DThe cache path is incorrect; it should be '/var/lib/docker' to cache layers.
Attempts:
2 left
💡 Hint

Check if the cache action and docker build commands use matching cache paths.

Troubleshoot
advanced
2:00remaining
Why is Docker layer caching not working in CI?

You notice your CI builds always take a long time and never reuse cached Docker layers. Which reason below explains this behavior?

AThe Dockerfile has a 'COPY .' command before installing dependencies, invalidating cache early.
BThe CI runner has enough disk space and memory for caching.
CThe Docker build uses '--cache-from' with a valid cache source.
DThe base image is pulled from a public registry without changes.
Attempts:
2 left
💡 Hint

Think about how Docker cache invalidation works with file changes.

🔀 Workflow
expert
3:00remaining
Optimizing Docker layer caching in multi-stage CI builds

You have a multi-stage Dockerfile used in CI. Which practice best improves layer caching efficiency across stages?

AUse 'COPY .' as the first command in every stage to include all files early.
BCombine all commands into a single stage to avoid cache misses between stages.
CAlways run 'apt-get update' in every stage to ensure fresh packages.
DSeparate frequently changing files into later stages and keep stable dependencies in earlier stages.
Attempts:
2 left
💡 Hint

Think about which parts of the build change often and which stay stable.