Skip to main content

Build Docker Images on TIR Nodes Using a Dockerfile

Introduction

BuildKit is a toolkit for converting source code to build artifacts in an efficient, expressive and repeatable manner.

In TIR, users can leverage BuildKit to create custom images from Dockerfiles and push them directly to their own container registries. This tutorial will guide you through building and pushing images using buildctl (the BuildKit command-line interface) on TIR Nodes.

Note

BuildKit CLI is not installed in TIR Nodes by default. To install it, follow the steps outlined below.

Installation

Step 1 : To download the BuildKit CLI (version 0.9.0) on your system, you can use the following command.

wget https://github.com/moby/buildkit/releases/download/v0.9.0/buildkit-v0.9.0.linux-amd64.tar.gz

Step 2 : Once downloaded, you can proceed with extracting and setting up BuildKit on your TIR Node. To extract the contents of the compressed tarball file run the command.

tar xvf buildkit-v0.9.0.linux-amd64.tar.gz

Step 3 : To ensure that buildctl (the BuildKit command-line interface) is accessible from any directory, you need to add its location to your system's PATH environment variable. Here's how you can do it

PATH_TO_BUILDKIT_FOLDER="<PATH_TO_BUILDKIT_FOLDER>"
export PATH=$PATH:$PATH_TO_BUILDKIT_FOLDER/bin
source /root/.bashrc

Usage

Build an Image with Buildkit

Step 1 : Now that buildctl is installed on your Node, you can begin building images. However, before you can do that, you need to create a Dockerfile. For this tutorial, we will use a sample Dockerfile. To download it, execute the following command:

wget --no-check-certificate https://raw.githubusercontent.com/courselabs/kubernetes/main/labs/docker/simple/Dockerfile

Step 2 : Once the Dockerfile is downloaded, you will have the necessary file to proceed with building your image. To initiate the image build process, run the following command:

# You can specify the relative as well as the absolute path to the dockerfile directory.
# For example: If you have the Dockerfile in the same directory where you are executing the buildkit command simply put PATH_TO_YOUR_DOCKERFILE_DIRECTORY="."
PATH_TO_YOUR_DOCKERFILE_DIRECTORY="<PATH_TO_YOUR_DOCKERFILE_DIRECTORY>"
IMAGE_NAME="<IMAGE_NAME>"
buildctl --addr tcp://buildkit.internal:1234 build --frontend=dockerfile.v0 --local context=. --local dockerfile=$PATH_TO_YOUR_DOCKERFILE_DIRECTORY --output type=image,name=$IMAGE_NAME
Note
  • By default, the build results and intermediate cache are stored internally within BuildKit. To access the built image outside of BuildKit, you must push it to your designated container registry.
  • If your registry is private, you will need to provide the authentication details on the TIR Node to gain access. This ensures that you can securely authenticate and push images to your private registry.

Pushing an Image using BuildKit

To push the image to a private repository, you first need to add your credentials to the config.json file located in the .docker folder. This step is essential for authenticating your access to the private repository. Follow these steps to update your credentials:

Note

In this tutorial, we will demonstrate how to push the image to E2E's Container Registry. If you prefer to push the image to Docker Hub instead, you will need to include the following URL in the authentication dictionary: https://index.docker.io/v1/, along with your credentials.

mkdir $HOME/.docker
cd $HOME/.docker
touch config.json
echo -n username:password | base64
  • Replace base64_username_and_password with the ouput of the code snippet above and add it in config.json.
{"auths":
{"registry.e2enetworks.net":
{"auth":<base64_username_and_password>}
}
}
  • Now that you have saved your registry credentials, you are ready to push your image to the private repository.
Note
  • To push your image to your container registry, simply add the push=true flag to the buildctl build command. This will build your image and automatically push it to the specified container registry.
  • Ensure that you modify the image name to suit your specific requirements before proceeding.
# You can specify the relative as well as the absolute path to the dockerfile directory.
# For example: If you have the Dockerfile in the same directory where you are executing the buildkit command simply put PATH_TO_YOUR_DOCKERFILE_DIRECTORY="."
PATH_TO_YOUR_DOCKERFILE_DIRECTORY="<PATH_TO_YOUR_DOCKERFILE_DIRECTORY>"
REPOSITORY="<REGISTRY_NAMESPACE>"
IMAGE_NAME="<IMAGE_NAME_WITH_TAG>"
buildctl --addr tcp://buildkit.internal:1234 build --frontend=dockerfile.v0 --local context=. --local dockerfile=$PATH_TO_YOUR_DOCKERFILE_DIRECTORY --output type=image,name=registry.e2enetworks.net/$REPOSITORY/$IMAGE_NAME,push=true