0
0
Dockerdevops~20 mins

Combining RUN commands in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RUN Command Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of combined RUN commands in Dockerfile

What will be the output of the following Dockerfile snippet when building the image?

FROM alpine:latest
RUN echo "Step 1" && echo "Step 2" && echo "Step 3"
Docker
FROM alpine:latest
RUN echo "Step 1" && echo "Step 2" && echo "Step 3"
A
Step 1
Step 2 && Step 3
BStep 1 Step 2 Step 3
CStep 1 && Step 2 && Step 3
D
Step 1
Step 2
Step 3
Attempts:
2 left
💡 Hint

Think about how the shell interprets the && operator inside a RUN command.

Configuration
intermediate
1:30remaining
Combining RUN commands to reduce image layers

Which Dockerfile snippet correctly combines multiple commands into a single RUN instruction to minimize image layers?

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/*
DRUN apt-get update; apt-get install -y curl; rm -rf /var/lib/apt/lists/*
Attempts:
2 left
💡 Hint

Consider how combining commands with && affects image layers and command execution.

Troubleshoot
advanced
2:00remaining
Why does this combined RUN command fail?

Given this Dockerfile snippet, why does the build fail?

RUN apt-get update && apt-get install -y curl || exit 1 && echo "Done"
Docker
RUN apt-get update && apt-get install -y curl || exit 1 && echo "Done"
AThe 'exit 1' is ignored because '&&' has higher precedence than '||', causing unexpected command flow.
BThe 'echo "Done"' runs even if 'apt-get install' fails, causing build failure.
CThe 'apt-get install' command syntax is invalid, causing the failure.
DThe 'apt-get update' command is missing a required flag, causing failure.
Attempts:
2 left
💡 Hint

Think about how shell operators && and || combine and their precedence.

🔀 Workflow
advanced
2:00remaining
Optimizing Dockerfile RUN commands for caching

You want to install dependencies and clean cache in a Dockerfile. Which RUN command order optimizes Docker build caching?

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

Think about which commands should run first to maximize cache reuse.

Best Practice
expert
2:30remaining
Best practice for combining RUN commands to handle errors

Which RUN command best ensures that if any command fails, the Docker build stops immediately, and all commands run in a single layer?

ARUN apt-get update && apt-get install -y curl & rm -rf /var/lib/apt/lists/*
BRUN set -e && apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
CRUN apt-get update || exit 1 && apt-get install -y curl || exit 1 && rm -rf /var/lib/apt/lists/* || exit 1
DRUN apt-get update; apt-get install -y curl; rm -rf /var/lib/apt/lists/*
Attempts:
2 left
💡 Hint

Consider how to make the shell stop on any error inside a RUN command.