Bird
Raised Fist0
Dockerdevops~20 mins

Image layers concept in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Layers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Docker image layers

Which statement best describes Docker image layers?

ALayers are stacked read-only filesystems that build the final image.
BLayers are temporary and deleted after container creation.
CEach layer is a full copy of the entire image filesystem.
DLayers are only used for container runtime, not image building.
Attempts:
2 left
💡 Hint

Think about how Docker reuses parts of images to save space.

💻 Command Output
intermediate
2:00remaining
Inspecting image layers with Docker CLI

What is the output of the command docker history alpine?

Docker
docker history alpine
AA list showing image layers with size, creation time, and commands used.
BA list of running containers based on alpine image.
CThe full Dockerfile used to build alpine image.
DAn error stating 'alpine' image not found.
Attempts:
2 left
💡 Hint

Try to recall what docker history shows about images.

🔀 Workflow
advanced
2:30remaining
Optimizing Dockerfile to reduce image layers

You want to reduce the number of layers in your Docker image. Which Dockerfile snippet achieves this best?

Docker
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
ARUN apt-get update; apt-get install -y curl; rm -rf /var/lib/apt/lists/*
B
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*
CRUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
D
RUN apt-get update && apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*
Attempts:
2 left
💡 Hint

Think about how Docker creates a new layer for each RUN instruction.

❓ Troubleshoot
advanced
2:30remaining
Diagnosing unexpected large Docker image size

Your Docker image is unexpectedly large. Which cause is most likely related to image layers?

AYou used COPY instead of ADD for files.
BYou used ENV variables in the Dockerfile.
CYou did not specify a base image in the Dockerfile.
DYou used multiple RUN commands that add temporary files without cleanup.
Attempts:
2 left
💡 Hint

Think about how layers keep all changes, even deleted files if not cleaned properly.

✅ Best Practice
expert
3:00remaining
Choosing the best practice for layer caching in Docker builds

Which Dockerfile practice best leverages layer caching to speed up rebuilds?

APlace frequently changing commands at the top of the Dockerfile.
BOrder commands from least to most frequently changing to maximize cache reuse.
CCombine all commands into one RUN instruction regardless of change frequency.
DAvoid using COPY or ADD commands to prevent cache invalidation.
Attempts:
2 left
💡 Hint

Think about how Docker caches layers and when cache is invalidated.

Practice

(1/5)
1. What does each layer in a Docker image represent?
easy
A. A user account inside the container
B. A separate container running inside Docker
C. A network connection between containers
D. A change made by a single Dockerfile command

Solution

  1. Step 1: Understand Dockerfile commands

    Each command in a Dockerfile creates a change in the image.
  2. Step 2: Connect changes to layers

    Docker saves each change as a separate layer to reuse if unchanged.
  3. Final Answer:

    A change made by a single Dockerfile command -> Option D
  4. Quick Check:

    Layer = Dockerfile command change [OK]
Hint: Think of layers as snapshots after each Dockerfile step [OK]
Common Mistakes:
  • Confusing layers with running containers
  • Thinking layers are user accounts
  • Believing layers are network connections
2. Which Dockerfile command creates a new image layer?
easy
A. START
B. ENVIRONMENT
C. RUN
D. COPYFILE

Solution

  1. Step 1: Identify valid Dockerfile commands

    RUN is a valid Dockerfile command to execute commands in the image.
  2. Step 2: Check other options

    ENVIRONMENT, COPYFILE, and START are not valid Dockerfile commands.
  3. Final Answer:

    RUN -> Option C
  4. Quick Check:

    RUN creates layers [OK]
Hint: Only standard Dockerfile commands create layers [OK]
Common Mistakes:
  • Using incorrect or non-existent Dockerfile commands
  • Confusing ENVIRONMENT with ENV
  • Thinking COPYFILE is a Docker command
3. Given this Dockerfile snippet:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y curl
COPY . /app

How many layers will this image have (excluding the base image)?
medium
A. 3
B. 2
C. 1
D. 4

Solution

  1. Step 1: Count commands creating layers

    RUN apt-get update, RUN apt-get install -y curl, and COPY . /app each create a layer.
  2. Step 2: Exclude base image layer

    FROM ubuntu is the base image, not counted here.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 commands create layers [OK]
Hint: Count RUN, COPY, ADD commands for layers [OK]
Common Mistakes:
  • Counting FROM as a new layer
  • Ignoring COPY command creates a layer
  • Miscounting number of RUN commands
4. You notice your Docker image size is very large. Which Dockerfile practice can help reduce image layers and size?
medium
A. Combine multiple RUN commands into one using &&
B. Add a new RUN command for each small step
C. Use multiple COPY commands for each file
D. Use FROM multiple times in one Dockerfile

Solution

  1. Step 1: Understand layer creation

    Each RUN command creates a new layer, increasing image size.
  2. Step 2: Combine commands to reduce layers

    Using && to combine commands in one RUN reduces layers and image size.
  3. Final Answer:

    Combine multiple RUN commands into one using && -> Option A
  4. Quick Check:

    Fewer RUN commands = fewer layers [OK]
Hint: Merge RUN commands with && to shrink layers [OK]
Common Mistakes:
  • Adding many small RUN commands
  • Using multiple COPY commands unnecessarily
  • Misusing multiple FROM commands in one Dockerfile
5. You want to optimize a Docker image build where a large file changes frequently. Which approach best minimizes rebuild time and image size?
hard
A. Place COPY of the large file at the start of the Dockerfile
B. Place COPY of the large file at the end of the Dockerfile
C. Combine COPY of the large file with RUN commands in one layer
D. Avoid using COPY and download the file inside RUN

Solution

  1. Step 1: Understand layer caching

    Docker caches layers; if a layer changes, later layers rebuild.
  2. Step 2: Place frequently changing files last

    Putting COPY of large changing files last limits rebuild to that layer only.
  3. Final Answer:

    Place COPY of the large file at the end of the Dockerfile -> Option B
  4. Quick Check:

    Last COPY limits rebuild scope [OK]
Hint: Put changing files COPY last to speed rebuilds [OK]
Common Mistakes:
  • Copying large files early causing full rebuilds
  • Combining COPY with RUN causing bigger layers
  • Downloading files inside RUN increasing image size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard