Bird
Raised Fist0
Dockerdevops~5 mins

RUN instruction for executing commands in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When building a Docker image, you often need to install software or prepare files. The RUN instruction lets you execute commands inside the image during build time to set it up exactly how you want.
When you want to install software packages inside your Docker image.
When you need to create or modify files during the image build.
When you want to update the system or clean up temporary files to keep the image small.
When you want to run scripts that prepare your app environment before the container runs.
When you want to set environment variables or permissions as part of the image setup.
Config File - Dockerfile
Dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
RUN echo "Hello from Dockerfile RUN" > /hello.txt

FROM ubuntu:22.04 sets the base image.

RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* updates package lists, installs curl, and cleans up package cache to reduce image size.

RUN echo "Hello from Dockerfile RUN" > /hello.txt creates a file with a message.

Commands
This command builds a Docker image named 'example-run-image' using the Dockerfile in the current directory. It runs all RUN instructions to prepare the image.
Terminal
docker build -t example-run-image .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM ubuntu:22.04 ---> 1d622ef86b13 Step 2/3 : RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* ---> Running in 3a2f4c5b7e8a Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB] ... Removing intermediate container 3a2f4c5b7e8a ---> 5f4d3a2b1c4e Step 3/3 : RUN echo "Hello from Dockerfile RUN" > /hello.txt ---> Running in 7b8c9d0e1f2a Removing intermediate container 7b8c9d0e1f2a ---> 8a9b7c6d5e4f Successfully built 8a9b7c6d5e4f Successfully tagged example-run-image:latest
→
-t - Assigns a name and optionally a tag to the image.
This runs a container from the image and shows the content of the file created by the RUN instruction to verify it worked.
Terminal
docker run --rm example-run-image cat /hello.txt
Expected OutputExpected
Hello from Dockerfile RUN
→
--rm - Automatically removes the container after it exits to keep the system clean.
Key Concept

RUN executes commands during image build to prepare the container environment before it runs.

Common Mistakes
Running commands that require user interaction inside RUN instructions.
Docker build cannot handle interactive prompts, so the build will hang or fail.
Use command options to disable prompts or provide all inputs non-interactively.
Not cleaning up package caches after installing software.
This makes the image larger than necessary, wasting space and bandwidth.
Remove temporary files and caches in the same RUN instruction to keep images small.
Using multiple RUN instructions for related commands without combining them.
Each RUN creates a new image layer, increasing image size and build time.
Combine related commands with && in a single RUN instruction.
Summary
RUN lets you execute commands inside the image while building it.
Use RUN to install software, create files, and prepare the environment.
Combine commands in one RUN to keep images small and efficient.

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