Dockerfile: Output of RUN instruction into a Variable
Last Updated :
23 Jul, 2025
Docker is an open-source platform that helps a developer to build, test, and deploy applications. It updates and containerizes applications which makes it easier to design an application. With the help of Docker, we can separate the application's infrastructure and the application which allows us easy distribution. Using Docker the developers are able to create a standardized component which includes the operating system libraries that help to run the applications.
What is a Dockerfile?
A Dockerfile is a text file instruction that are used to generate a Docker Image. It could be referred to as a recipe that specifies all the necessary instructions for building a Docker Image.
What is a Docker Image?
Docker Image is a pre-packaged bundle of everything that is needed to run an application. It contains the application's code, libraries, and dependencies required inside the container.
What is a Container?
A container is a virtual box like a shipping container containing everything needed to run a software application. It makes the process of development, testing, and deployment smoother.
Step-by-Step Guide to Capture the Output of RUN Instruction
Methods to capture the output of a RUN instruction
- Redirecting output to a file
- Command substitution
- ENV Instruction
- A Shell script
- RUN with `-o` option (Docker 20.10 and later)
- RUN with `>>` operator (Docker 20.10 and later)
Step 1: Open VS Code Editor and make sure you have the Docker Extension installed in VS code
Step 2: Open the Bash terminal and write the following commands
mkdir docker-project
cd docker-project
Step 3: To create a Dockerfile write the touch command
touch Dockerfile
Step 4: Write the FROM instruction in the Dockerfile to specify the base image
FROM alpine:3.18
Step 5: Write the RUN instruction in the Dockerfile to execute a process or a message during build process
RUN apk add curl
Step 6: Add the WORKDIR instruction to set the path on which you need to work
- If the Working Directory is not present this command will create it for you.
WORKDIR /downloads
Step 7: Create a user with the RUN command
RUN adduser -h /home/cloudchamp -D cloudchamp
Storing the output in a variable within the Dockerfile
Step : Creating a file
RUN touch example.txt
Step 9: Write the RUN instruction to get the output into a variable
- The command "ls -1" will list the file in the "/downloads" path and the downloads directory then captures values into "files" which then echoes or prints the value of "files". You can change this step and use another method instead of using the command substitution,I am listing examples to store output using different methods below.
RUN FILES=$(ls -1 /downloads) && echo "FILES=$FILES" >> env.txt
Step 10: Writing a "cat" command to read and write outputs
RUN cat env.txt
Step 11: Add the RUN command to print the value passed in "FILES"
RUN echo $FILES
Step 12: Write the USER instruction in the Dockerfile to set the username or user ID
USER cloudchamp:cloudchamp
Step 10: Save the Dockerfile
Step 11: Build the Docker Image using the following command
docker build -t my-image .
Retrieving The Output of Run Instruction From a Variable
Step 1: After building the Docker Image check the image if the image is present in the repository
docker images myimage
Step 2: Now, to check the output from the RUN instructions
docker run -it myimage sh -c 'ls -l /downloads'
Practical Examples and Best Practices
Example 1: Capturing command output in a file (Method 1)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example1.txt
RUN ls -1 /downloads > files.txt
RUN cat files.txt
USER cloudchamp:cloudchamp
Example 2: Capturing command output using command substitution (Method 2)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN output=$(ls -1 /downloads) && echo "OUTPUT=$output"
USER cloudchamp:cloudchamp
Example 3: Using the captured output using file and ENV (Method 3)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN ls -1 /downloads > temp.txt
ENV FILES=$(<temp.txt)
RUN echo "FILES=$FILES"
USER cloudchamp:cloudchamp
Example 4: Using the captured output using shell script (Method 4)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN echo "ls -1 /downloads > files.txt" > script.sh
RUN chmod +x script.sh
RUN ./script.sh
RUN cat files.txt
USER cloudchamp:cloudchamp
Example 4: Using the captured output using shell script (Method 4)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN -o files.txt ls -1 /downloads
RUN cat files.txt
USER cloudchamp:cloudchamp
Example 5: Using the captured output using RUN with -o option (Method 5)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN -o files.txt ls -1 /downloads
RUN cat files.txt
USER cloudchamp:cloudchamp
Example 6: Using the captured output using RUN with >> operator (Method 6)
FROM alpine:3.18
RUN apk add curl
WORKDIR /downloads
RUN adduser -h /home/cloudchamp -D cloudchamp
RUN touch example.txt
RUN ls -1 /downloads >> files.txt
RUN cat files.txt
USER cloudchamp:cloudchamp
Best Practices: Tips for efficiently using variables with RUN instructions in Dockerfile
- Use meaningful variable names: This makes the code easy to understand and readable.
- Avoid using RUN instructions excessively: It creates new layers which results in increase in the size of image and slow down the build process.
- Use ENV instructions instead of RUN: The ENV instruction doesn't create new layers like the RUN instructions and hence, increases the build process speed.
- Keep your Dockerfile organized: This will make it easier to update and maintain your Dockerfile.
- Test your Dockerfile: Ensure that the Docker Desktop is running and it builds and produces the output correctly.
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle.The goals of DevOps are:Faster and continuous software releases.Reduces manual errors through a
7 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps