The Wayback Machine - https://web.archive.org/web/20240913185417/https://www.geeksforgeeks.org/docker-buildkit-run-command-output/
Open In App

How to See RUN Command Output in Docker BuildKit

Last Updated : 26 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

BuildKit with Docker – FAQs

How can I view the output of RUN commands when using BuildKit?

You can view the output of RUN commands by building the Docker image with the –progress=plain flag.

docker build --progress=plain -t my-image .

Can I disable BuildKit if I prefer the old build system?

Yes, you can disable BuildKit by setting the environment variable DOCKER_BUILDKIT=0 before running your build command.

export DOCKER_BUILDKIT=0
docker build -t my-image .

Why isn’t the output of the RUN command visible when using BuildKit by default?

BuildKit keeps the Docker build process optimized by hiding intermediate outputs to improve performance and prevent clutter. This can make builds faster and more efficient but can also make it harder to see what happens at each step.

Can I permanently enable BuildKit for all Docker builds?

Yes, you can enable BuildKit by default by adding the environment variable DOCKER_BUILDKIT=1 to your shell configuration file, such as .bashrc or .zshrc. This will automatically enable BuildKit for every Docker build.

What are the benefits of using BuildKit despite the hidden outputs?

BuildKit offers several real benefits—parallel build execution, more extensive caching mechanisms, and secure handling of secrets. These features improve performance and reliability, especially in complex projects. By using the right flags, you can balance the benefits of BuildKit with the need for visible command outputs.



Previous Article
Next Article

Similar Reads

Docker Tips : about /var/run/docker.sock
Docker has thus gained lots of usage today in software development. It allows developers to create, deploy, and run their applications in isolated environments. Such isolation will guarantee consistency between development, testing, and production environments and, finally, app security and scalability. A central part of the functionalities of Dock
8 min read
Docker Start vs Docker Run
Docker is a platform as a service(PaaS) that allows engineers to use OS-level virtualization to automate the deployment of applications inside lightweight and portable packages called containers. The containers have a very big advantage as they allow apps to run on different platforms effortlessly, and reduce the old headache of "it runs on my mach
7 min read
How To See Docker Image Contents?
In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process. Contents of a Docker image are important for troubleshooting, optimizing, and ensuring security in your containerized applications. This ar
3 min read
Run PostgreSQL on Docker and Setting Up pgAdmin
PostgreSQL, an effective tool, is a free­-to-use relational database management system. Docker can quickly construct and orche­strate its instances without bothering about the complexity of setup or depe­ndencies. This step-by-step simple guide will show you how to get Postgre­SQL on Docker, and then, use the pgAdmin extension to look at the databa
7 min read
How To Run A Cron Job Inside A Docker Container ?
In the containerization world, management of resources and scaling the applications are necessary to make our services effective and user-friendly for the users. In this, the cron job helps in a significant way with facilitating features such as isolation, portability, scalability, and resource management. It will be a convenient and efficient solu
5 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can also save that environment for future use also as
7 min read
Docker Compose Tool To Run aMulti Container Applications
The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml file with a single command, you create and start all
8 min read
How to start or run docker daemon
Docker has had a significant impact on the development, shipping, and deployment of applications. By using containerization, Docker allows a developer to package an application along with all the dependencies into standard units termed containers. This way, it makes sure the application behaves uniformly across any environment, starting from develo
6 min read
Docker Run with Environment Variables
Containerization has significantly advanced software development by packaging applications and their dependencies together and containerizing them to ensure consistency across multiple environments. Environment variables are one of the most important features of containerization as they make it possible to manage configuration and behavior. Imagine
7 min read
How to Run a Python Script using Docker?
Docker helps you to run your python application very smoothly in different environments with out worrying about underlying platforms. Once you build an image using dockerfile you can run that image where ever you want to run. Docker image will help you to package all the dependencies required for the application which will used for further while ru
6 min read
How to Use Docker For Fault Tolerance with Docker Replicas?
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine. Docker ReplicasDocker replicas refer to running multiple instances of a doc
4 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact and portable solution. Deploying containerized app
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
Docker Compose vs Docker Swarm
Docker is one of the most widely used container-based software on the market. Docker is an open-source platform for creating, deploying, and managing containerized applications. Docker allows us to simply bundle our apps into containers and can be deployable on any platform that supports docker software acting as a platform independent. What Is Doc
6 min read
Docker CLI vs Docker Desktop
Docker is an open-source platform. It is used to containerize applications. This is done by packaging applications along with their dependencies into containers that can be easily deployed. Users interact with Docker primarily using either the Docker CLI or the Docker Desktop Application. In this article, we'll be looking into each one in detail an
5 min read
Docker: How To Use Bash With An Alpine Based Docker Image?
Docker is a tool that is used to encapsulate the application with all its dependencies, called Docker containers. On the other hand, Alpine Linux is a lightweight and minimal Linux distribution. Here in this guide, I will first discuss what Docker is. Then I will discuss what Alpine is. After this, I will walk you through the different steps to ins
5 min read
Docker - Deploying WebApps on Docker
Docker applies abstraction at the software layer of the operating system to create an instant and portable environment that includes everything you need: a Linux kernel, libraries, system tools like grep or sshfs, plus applications (web servers/services) and all dependencies needed by those applications (e.g., database engines). When deployed, thes
9 min read
How To Use Docker Security Tools To Secure Docker Container Images
Docker is a tool that is used to package the application and its dependencies into compact units called Docker containers. In this guide, I will first discuss what a docker is. Then I will discuss a security tool called Trivy, which is used to scan and detect Docker image vulnerabilities. After this, I will walk you through the different steps to d
11 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it public it gives users more flexibility in creating e
8 min read
Docker - Using Public Repositories To Host Docker Images
Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing container images. Just like GitHub allows the hosting
10 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its dependencies, making it highly scalable and maintainable.
12 min read
Docker Logs Inside the Docker Container
While running containers, it is important to know about various information like running processes generated by the container. Docker logs allow us to get this information regarding the containers. While it's pretty straighforward to check logs directly from Docker CLI, here we will see how to check logs from within a container. What is Docker?Dock
5 min read
How To Use Docker Desktop To Deploy Docker Containerized Applications
Docker Desktop is an user-friendly tool that simplifies the deployment of Docker containerized applications on your local machine. It provides a graphical interface for managing containers, images, and volumes, making it accessible for developers and DevOps professionals. In this guide, we will walk you through the essential steps of using Docker D
9 min read
Dockerfile: Output of RUN instruction into a Variable
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 a
6 min read
How to Run curl Command From Within a Kubernetes Pod?
Ever needed to fetch data or test connectivity from within a Kubernetes pod? The curl command becomes your trusty companion in these situations. This guide explores different approaches to execute curl commands directly from within your Kubernetes pod, empowering you to diagnose issues and interact with external services effectively. What are Kuber
3 min read
What Is Docker rm Command ?
"docker rm" is used to remove one or more Docker containers that are running on the Docker host. A Docker container uses up system resources like CPU, memory, and disk space when it is running. To free up such resources, you may choose to uninstall a container after using it for a while. docker rm [OPTIONS] CONTAINER [CONTAINER...] OPTIONS: Various
3 min read
What is Docker PS Command ?
he "docker ps" command is used to list all the running containers in the docker host. With the help of some filters, you can get the output of all the containers in the docker with are running and that are stopped. it shows the list of active containers that includes details about each one, including its name, ID, creation image, program being run
5 min read
How do I Download Docker Images Without Using the Pull Command?
Nowadays, effective packaging and deployment are closely associated with Docker in the world of containerized apps. A common Docker task is pulling images from registries, which can be completed with the all-purpose docker pull command. However, you do not have to use this command-line interface to retrieve Docker images; there are other alternativ
5 min read
How to Install ""ifconfig"" Command in My Ubuntu Docker Image?
Docker images are templates that include all the code, libraries, dependencies, and runtime required to run an application. They guarantee consistency and portability across several systems, which makes them indispensable in containerized contexts. Docker images make it simple to scale, deploy, and manage applications, which promotes quicker develo
6 min read
How to Fix the "Docker Compose Command Not Found" Error
To determine and operate multi-container Docker applications, you'll need Docker Compose. The "Docker Compose command not found" error is a frequent problem that many engineers encounter, nevertheless. This problem usually arises from an incorrect setup or non-existence of the Docker Compose binary in the PATH of the system. We'll look at how to re
5 min read
Article Tags :