Open In App

How to See RUN Command Output in Docker BuildKit

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

Docker build is one of the most crucial parts of the operation of containerizing applications. Docker's traditional build system has served developers well, but with projects growing in complexity, the need for better builds that are more efficient and flexible was well apparent. Enter BuildKit, the next-generation build architecture for Docker, aimed at producing a faster, more powerful, and more secure build process.

But these powerful optimizations also change how the build behaves—especially with the output from commands, like those run with the RUN instruction in a Dockerfile. Unlike the classic Docker build, which streams command output straight to the terminal, BuildKit, being optimized to its nature, hides most of that output by default. Although this enhances performance and minimizes the noise when the builds are running, it is annoying during debugging or when you need to be able to follow a particular step within your Dockerfile.

In this article, we go through how to make your Docker build process with BuildKit such that the output of RUN commands becomes visible. We will look at what configurations need to be done, provide a step-by-step guide, and give you practical examples to help you take full control of your Docker builds using BuildKit.

Primary Terminologies

  • Docker: An open-source platform for automating the process of deploying applications within lightweight, portable containers. A container packages an application with all its dependencies so that it runs consistently in different environments—from development to production.
  • BuildKit: BuildKit is an advanced subsystem for Docker build, it amplifies the traditional Docker build process to its maximum, with parallel execution at its core, it allows import and export of build cache for better handling of secrets during the build, increasing speed, efficiency, and security.
  • RUN Command: The RUN command is used to execute commands in a Dockerfile within the container being built. Typically this is where you would install software packages, compile code, or set up the environment in which an application runs.
  • Dockerfile: A Dockerfile is a text document that contains a script of instructions for creating a Docker image. Each Dockerfile instruction represents a single layer in the image; together, these layers describe the final container.
  • Environment Variable: A variable holding some dynamic value that may change the behavior of running processes on a computer, in Docker environment variables can pass configuration information into containers or control certain aspects of the Docker build process, such as enabling BuildKit.

Step-by-Step Process to View RUN Command Output with BuildKit

Step 1: Install Docker

Install docker in our local instance by using the following commands

sudo yum -y install docker
Screenshot-2024-08-22-100816

Start and enable docker daemon by using following commands

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Screenshot-2024-08-22-100911

Step 2: Enable BuildKit

Temporarily Enable BuildKit:

To enable BuildKit for a single build session, set the environment variable DOCKER_BUILDKIT=1 in your terminal session:

export DOCKER_BUILDKIT=1

We can check BuildKit Status by using following command

echo $DOCKER_BUILDKIT
Screenshot-2024-08-22-100722

Step 3: Create Dockerfile

Here is the example of dockerfile

# Use an official Python runtime as a parent image

FROM python:3.8-slim

# Set the working directory inside the container

WORKDIR /app

# Copy the requirements file into the container

COPY requirements.txt /app/

# Install dependencies from the requirements file

RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app

COPY . /app

# Run a command to display contents

RUN ls -la

Screenshot-2024-08-22-101350

Step 4: Build the Docker Image with BuildKit Enabled

When building the Docker image, you need to ensure that the output of RUN commands is visible.

Standard Build Command:

  • By default, running the build command might not show all the output due to BuildKit optimizations:
docker build -t my-image .
Screenshot-2024-08-22-101605

Force Output Display:

  • To ensure you can see the output of each RUN command, add the --progress=plain flag:
docker build --progress=plain -t my-image .
Screenshot-2024-08
  • As the build process runs, you will see the output of each RUN command in your terminal. This will help you monitor the progress and diagnose any issues that arise during the build.
  • This indicates that the ls -la command ran successfully and lists the contents of the /app directory.
RUN ls- la

Conclusion

Working with Docker in combination with BuildKit has a lot of benefits that result in faster builds, better-cached builds, and builds managed more securely for build processing. Some of the optimization characteristics which make BuildKit powerful have a tendency to hide the output of critical commands, such as the ones run with the RUN instruction inside a Dockerfile, but for developers who depend on this output for debugging or monitoring purposes, it is important to learn how to change the build process to keep this in view.

Whether you're debugging an elaborate build process or simply want more visibility into what's happening in your Docker builds, knowing how to control RUN command output with BuildKit is an important skill. With this knowledge, you can confidently move forward and optimize your containerization workflows to best serve your development needs efficiently and effectively.


Next Article
Article Tags :

Similar Reads