Runpod Pods use custom Docker images, so you can’t directly build Docker containers or use Docker Compose on a GPU Pod. However, you can use Bazel to build and push Docker images from inside a Pod, effectively creating a “Docker in Docker” workflow.
Requirements
Before starting, you’ll need:
Step 1: Deploy a Pod
- Navigate to Pods and select + Deploy.
- Choose GPU or CPU based on your needs.
- Select an instance type (for example, A40).
- (optional) Attach a for larger image builds.
- Select a (for example, Runpod Pytorch).
- Select Deploy On-Demand.
Wait for the Pod to start, then connect via the web terminal:
- Select Connect.
- Select Start Web Terminal, then Connect to Web Terminal.
Step 2: Install dependencies
Install the required tools:
-
Update packages and install sudo:
-
Install Docker:
-
Log in to Docker Hub:
-
Go to Docker Hub Security Settings and create an access token with Read/Write permissions.
-
Log in to Docker Hub, replacing
YOUR_USERNAME with your actual Docker Hub username:
When prompted for a password, paste your access token (not your Docker Hub password).
-
Install Bazel via Bazelisk:
-
Verify the installations:
You should see version numbers for both Docker and Bazel.
Step 3: Create the project files
Create a new directory for your Bazel project:
Create a .bazelversion file to pin to a stable Bazel version:
Create an empty MODULE.bazel file (required by Bazel 7+):
Create the WORKSPACE file that declares your dependencies:
Create the BUILD.bazel file that defines the image build:
Use sed to replace the username placeholder with your Docker Hub username in BUILD.bazel. Replace YOUR_ACTUAL_USERNAME with your actual Docker Hub username:
Verify the change was applied:
You should see your username in the output:
Step 5: Build and push the image
Run the Bazel command to build and push the Docker image:
The first build may take several minutes as Bazel downloads dependencies and the base image. Subsequent builds will be much faster due to caching.
Once complete, your image will be available in your Docker Hub repository at your_username/custom_image:latest.
Understanding the build files
.bazelversion
This file pins the Bazel version to ensure reproducible builds. Bazelisk reads this file and automatically downloads the specified version.
WORKSPACE
This file declares your project’s external dependencies:
http_archive: Downloads the rules_oci package from GitHub.
rules_oci_dependencies: Loads transitive dependencies.
oci_register_toolchains: Registers the OCI toolchain (crane) for building images.
oci_pull: Pulls a base Docker image that your custom image builds on top of.
BUILD.bazel
This file defines what to build:
oci_image: Creates a new container image by adding layers to a base image.
genrule: Creates a tar file containing custom content to add to the image.
oci_push: Pushes the built image to a container registry.
Next steps