Bird
Raised Fist0
Dockerdevops~20 mins

RUN instruction for executing commands in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RUN Instruction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Dockerfile RUN command?
Consider this Dockerfile snippet:
FROM alpine
RUN echo Hello World

What will be the output when building this Docker image?
Docker
FROM alpine
RUN echo Hello World
AThe build logs will show 'Hello World' printed during the RUN step.
BThe container will start and print 'Hello World' automatically.
CThe RUN command will be ignored and no output will appear.
DThe image will fail to build due to syntax error in RUN command.
Attempts:
2 left
💡 Hint
RUN commands execute during image build and their output appears in build logs.
🧠 Conceptual
intermediate
1:30remaining
What does the RUN instruction do in a Dockerfile?
Choose the best description of the RUN instruction in a Dockerfile.
AIt executes commands during the image build and creates a new image layer.
BIt runs commands every time the container starts.
CIt copies files from the host to the container.
DIt sets environment variables for the container.
Attempts:
2 left
💡 Hint
RUN commands happen once during build, not at container start.
❓ Troubleshoot
advanced
2:30remaining
Why does this RUN command fail in Dockerfile?
Given this Dockerfile snippet:
FROM ubuntu
RUN apt-get update && apt-get install -y python3 pip

What is the cause of the failure?
Docker
FROM ubuntu
RUN apt-get update && apt-get install -y python3 pip
AThe apt-get update command requires sudo inside Dockerfile.
BThe RUN command syntax is invalid; '&&' cannot be used.
CThe Ubuntu base image does not support apt-get.
DThe package name 'pip' is incorrect; it should be 'python3-pip'.
Attempts:
2 left
💡 Hint
Check package names carefully when installing with apt-get.
🔀 Workflow
advanced
2:00remaining
How to combine multiple commands in a single RUN instruction?
You want to update package lists and install curl in one RUN instruction. Which option correctly does this?
ARUN apt-get update || apt-get install -y curl
BRUN apt-get update & apt-get install -y curl
CRUN apt-get update && apt-get install -y curl
DRUN apt-get update; apt-get install -y curl
Attempts:
2 left
💡 Hint
Use command chaining to run commands sequentially only if the first succeeds.
✅ Best Practice
expert
3:00remaining
What is the best practice to reduce Docker image layers when using RUN?
You want to install packages and clean cache in one RUN instruction to keep image size small. Which option follows best practice?
A
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*
BRUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
C
RUN apt-get update && apt-get install -y curl
RUN 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
Combine commands with && to create a single layer and clean cache in the same RUN.

Practice

(1/5)
1. What does the RUN instruction do in a Dockerfile?
easy
A. Executes commands during the image build process
B. Starts a container from an image
C. Copies files from host to container
D. Defines environment variables for the container

Solution

  1. Step 1: Understand Dockerfile instructions

    The RUN instruction is used to execute commands while building the Docker image.
  2. Step 2: Differentiate from other instructions

    Other instructions like COPY or ENV serve different purposes, not command execution.
  3. Final Answer:

    Executes commands during the image build process -> Option A
  4. Quick Check:

    RUN = execute commands during build [OK]
Hint: RUN runs commands while building the image [OK]
Common Mistakes:
  • Confusing RUN with CMD or ENTRYPOINT
  • Thinking RUN starts containers
  • Mixing RUN with COPY or ENV
2. Which of the following is the correct syntax to install curl using RUN in a Debian-based Docker image?
easy
A. RUN apt-get install curl
B. RUN apt-get update && apt-get install -y curl
C. RUN install curl
D. RUN curl install

Solution

  1. Step 1: Understand package installation in Debian

    Before installing packages, you must update the package list using apt-get update.
  2. Step 2: Use correct flags and chaining

    The -y flag auto-confirms installation, and chaining commands with && ensures update runs before install.
  3. Final Answer:

    RUN apt-get update && apt-get install -y curl -> Option B
  4. Quick Check:

    Update then install with -y flag [OK]
Hint: Always update before install and use -y flag [OK]
Common Mistakes:
  • Skipping apt-get update
  • Not using -y to auto-confirm
  • Wrong command order or syntax
3. Given this Dockerfile snippet:
FROM ubuntu:latest
RUN echo "Hello" && echo "World"

What will be the output when building this Docker image?
medium
A. Hello World printed during build
B. No output shown during build
C. Error because of missing semicolon
D. Only 'Hello' printed, 'World' ignored

Solution

  1. Step 1: Analyze the RUN command chaining

    The command uses && to run two echo commands sequentially during build.
  2. Step 2: Understand Docker build output

    During build, the output of RUN commands is shown in the terminal, so both 'Hello' and 'World' will print.
  3. Final Answer:

    Hello World printed during build -> Option A
  4. Quick Check:

    RUN shows command output during build [OK]
Hint: RUN output appears during build, && runs commands sequentially [OK]
Common Mistakes:
  • Thinking RUN output is hidden
  • Confusing && with ;
  • Assuming only first command runs
4. Identify the error in this Dockerfile RUN instruction:
RUN apt-get update & apt-get install -y git
medium
A. RUN cannot have multiple commands
B. Missing -y flag for apt-get install
C. apt-get update should be last command
D. Using single & instead of && causes syntax error

Solution

  1. Step 1: Check command chaining syntax

    Single & runs command in background and is invalid in Docker RUN; && is needed to chain commands properly.
  2. Step 2: Confirm correct chaining for sequential execution

    Using && ensures the second command runs only if the first succeeds.
  3. Final Answer:

    Using single & instead of && causes syntax error -> Option D
  4. Quick Check:

    Use && to chain commands in RUN [OK]
Hint: Use && not & to chain commands in RUN [OK]
Common Mistakes:
  • Using & instead of &&
  • Ignoring command chaining rules
  • Assuming RUN runs commands in parallel
5. You want to reduce Docker image layers by combining multiple commands in a single RUN instruction. Which is the best way to do this?
hard
A. Use multiple RUN instructions, one per command
B. Separate commands with semicolons in multiple RUN lines
C. Chain commands with && inside one RUN instruction
D. Use CMD instead of RUN for multiple commands

Solution

  1. Step 1: Understand Docker image layers

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

    Chaining commands with && inside one RUN instruction runs them sequentially in a single layer.
  3. Final Answer:

    Chain commands with && inside one RUN instruction -> Option C
  4. Quick Check:

    Combine commands with && to reduce layers [OK]
Hint: Chain commands with && in one RUN to reduce layers [OK]
Common Mistakes:
  • Using multiple RUNs increases layers
  • Using CMD instead of RUN for build steps
  • Separating commands with semicolons without chaining
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