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"
FROM alpine:latest RUN echo "Step 1" && echo "Step 2" && echo "Step 3"
Think about how the shell interprets the && operator inside a RUN command.
The && operator runs commands sequentially only if the previous command succeeds. Each echo prints its output on a new line, so the output will be three lines.
Which Dockerfile snippet correctly combines multiple commands into a single RUN instruction to minimize image layers?
Consider how combining commands with && affects image layers and command execution.
Using && chains commands so the next runs only if the previous succeeds, and combining them in one RUN creates a single image layer. Using multiple RUN instructions creates multiple layers. Using ; runs commands regardless of success, and & runs commands in background, which is not suitable here.
Given this Dockerfile snippet, why does the build fail?
RUN apt-get update && apt-get install -y curl || exit 1 && echo "Done"
RUN apt-get update && apt-get install -y curl || exit 1 && echo "Done"
Think about how shell operators && and || combine and their precedence.
In shell, && has higher precedence than ||. So the command is parsed as (apt-get update && apt-get install -y curl) || (exit 1 && echo "Done"). If 'apt-get install' fails, 'exit 1' runs but 'echo "Done"' also runs because of the &&. This causes unexpected behavior and build failure.
You want to install dependencies and clean cache in a Dockerfile. Which RUN command order optimizes Docker build caching?
Think about which commands should run first to maximize cache reuse.
Running apt-get update first updates package lists, then installing packages uses that updated list. Cleaning cache last reduces image size. This order allows Docker to cache layers effectively. Other orders break this logic or run commands unnecessarily.
Which RUN command best ensures that if any command fails, the Docker build stops immediately, and all commands run in a single layer?
Consider how to make the shell stop on any error inside a RUN command.
Using set -e causes the shell to exit immediately if any command fails. Chaining commands with && also stops on failure, but set -e is more reliable for complex commands. Using ; runs commands regardless of errors, and & runs commands in background, which is not desired.