How to See RUN Command Output in Docker BuildKit
Last Updated :
26 Aug, 2024
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
Start and enable docker daemon by using following commands
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
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
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
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 .
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 .
- 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.
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.
Similar Reads
How to Use AWS CLI in Docker Container ?
The AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. Integrating AWS CLI within a Docker container can significantly streamline workflows, especially for development and deployment processes that rely on cloud infrastruct
4 min read
How To Include Files Outside Of Docker's Build Context ?
In this guide, we'll walk through the steps to include files outside of Docker's build context on an AWS EC2 instance. This process allows you to access files located outside of the Docker build context within your Docker container. We'll demonstrate this with an Apache web server setup on an EC2 in
5 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 unif
6 min read
How To Fix "Bash: Docker: Command Not Found" In Linux
Docker has become an essential tool for developers and system administrators to manage and deploy applications efficiently. However, encountering the error message "Bash: Docker: Command Not Found" can be frustrating, especially when you're trying to work with Docker containers. Here, we'll explore
4 min read
How to Install Docker on Debian?
Docker Service Product is the essential tool used for the development purpose of any software where the said software needs to be passed through different development phases. The Installed Docker Service makes Operating System-Level Virtualization to create Docker Containers. Docker can easily be in
4 min read
Top 10 Docker Commands You Need to Know
Docker has changed the goalposts of application development and deployment by providing a standard environment to run the applications, which is independent of their place of deployment. Moving forward into 2024, as a developer and DevOps engineer, one has to fully understand and know how to use app
7 min read
How to Setup Jenkins in Docker Container?
Setting up of Jenkins in a docker container provides the facility of streamlining the CI/CD with scalability and consistency. It also helps for attaching the worker nodes as slaves and run the build jobs over their. In this article, we will guide you through the process of deploying jenkins in a Doc
6 min read
How to Link Multiple Container in Docker ?
Docker is a free software created by Docker Inc. and it enables clients to create free and confined environments where the users can deploy their applications. A container is a standard unit of software that packages up code and all its dependencies from one computing environment and runs it quickly
2 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, includin
5 min read
How To Comment In Dockerfile?
The purpose of comments in a Dockerfile is to both clarify and improve the readability of the instructions. It can also be used to stop execution when testing other code. The comments are meant to serve as a source of code line information. Comments are a frequent way for programmers to document the
3 min read