Open In App

Dockerfile: Output of RUN instruction into a Variable

Last Updated : 22 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. Redirecting output to a file
  2. Command substitution
  3. ENV Instruction
  4. A Shell script
  5. RUN with `-o` option (Docker 20.10 and later)
  6. 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

dop2

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
Docker project

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

dockerfile

Step 11: Build the Docker Image using the following command

docker build -t my-image .
docker build


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
docker image

Step 2: Now, to check the output from the RUN instructions

docker run -it myimage sh -c 'ls -l /downloads'
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
dop8
dop7

Best Practices: Tips for efficiently using variables with RUN instructions in Dockerfile

  1. Use meaningful variable names: This makes the code easy to understand and readable.
  2. Avoid using RUN instructions excessively: It creates new layers which results in increase in the size of image and slow down the build process.
  3. 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.
  4. Keep your Dockerfile organized: This will make it easier to update and maintain your Dockerfile.
  5. Test your Dockerfile: Ensure that the Docker Desktop is running and it builds and produces the output correctly.

Next Article
Article Tags :

Similar Reads