0
0
Dockerdevops~5 mins

RUN instruction for executing commands in Docker - Commands & Configuration

Choose your learning style9 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.