Build Docker Images on RunPod with Bazel
RunPod's GPU Pods use custom Docker images to run your code. This means you can't directly spin up your own Docker instance or build Docker containers on a GPU Pod. Tools like Docker Compose are also unavailable.
This limitation can be frustrating when you need to create custom Docker images for your RunPod templates.
Fortunately, many use cases can be addressed by creating a custom template with the desired Docker image.
In this tutorial, you'll learn how to use the Bazel build tool to build and push Docker images from inside a RunPod container.
By the end of this tutorial, you’ll be able to build custom Docker images on RunPod and push them to Docker Hub for use in your own templates.
Prerequisites
Before you begin this guide you'll need the following:
- A Docker Hub account and access token for authenticating the docker login command
- Enough volume for your image to be built
Create a Pod
- Navigate to Pods and select + Deploy.
- Choose between GPU and CPU.
- Customize your an instance by setting up the following:
- (optional) Specify a Network volume.
- Select an instance type. For example, A40.
- (optional) Provide a template. For example, RunPod Pytorch.
- (GPU only) Specify your compute count.
- Review your configuration and select Deploy On-Demand.
For more information, see Manage Pods.
Wait for the Pod to spin up then connect to your Pod through the Web Terminal:
- Select Connect.
- Choose Start Web Terminal and then Connect to Web Terminal.
- Enter your username and password.
Now you can clone the example GitHub repository
Clone the example GitHub repository
Clone the example code repository that demonstrates building Docker images with Bazel:
git clone https://github.com/therealadityashankar/build-docker-in-runpod.git && cd build-docker-in-runpod
Install dependencies
Install the required dependencies inside the Runpod container:
Update packages and install sudo:
apt update && apt install -y sudo
Install Docker using the convenience script:
curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh
Log in to Docker using an access token:
- Go to https://hub.docker.com/settings/security and click "New Access Token".
- Enter a description like "Runpod Token" and select "Read/Write" permissions.
- Click "Generate" and copy the token that appears.
- In the terminal, run:
docker login -u <your-username>
When prompted, paste in the access token you copied instead of your password.
Install Bazel via the Bazelisk version manager:
wget https://github.com/bazelbuild/bazelisk/releases/download/v1.20.0/bazelisk-linux-amd64
chmod +x bazelisk-linux-amd64
sudo cp ./bazelisk-linux-amd64 /usr/local/bin/bazel
Configure the Bazel Build
First, install nano if it’s not already installed and open the BUILD.bazel
file for editing:
sudo apt install nano
nano BUILD.bazel
Replace the {YOUR_USERNAME}
placeholder with your Docker Hub username in the BUILD.bazel
file:
[label BUILD.bazel]
oci_push(
name = "push_custom_image",
image = ":custom_image",
repository = "index.docker.io/{YOUR_USERNAME}/custom_image",
remote_tags = ["latest"]
)
Build and Push the Docker Image
Run the bazel command to build the Docker image and push it to your Docker Hub account:
bazel run //:push_custom_image
Once the command completes, go to https://hub.docker.com/ and log in. You should see a new repository called custom_image
containing the Docker image you just built.
You can now reference this custom image in your own Runpod templates.
Conclusion
In this tutorial, you learned how to use Bazel to build and push Docker images from inside RunPod containers. By following the steps outlined, you can now create and utilize custom Docker images for your RunPod templates. The techniques demonstrated can be further expanded to build more complex images, providing a flexible solution for your containerization needs on RunPod.