MBalk
RRunPod
•Created by MBalk on 5/9/2024 in #⚡|serverless
Docker build inside serverless
Yes, I want to clone a github repo, build the image, then push the image to ghcr. Because I cannot build certain images on my Macbook or Github Actions
14 replies
RRunPod
•Created by MBalk on 5/9/2024 in #⚡|serverless
Docker build inside serverless
is it not possible with something like this? https://stackoverflow.com/a/68975166
14 replies
RRunPod
•Created by MBalk on 5/9/2024 in #⚡|serverless
Docker build inside serverless
import runpod
import os
import git
import docker
def clone_repo(repo_url, repo_name):
# Clone the GitHub repository
git.Repo.clone_from(repo_url, repo_name)
def build_docker_image(dockerfile_path, image_name, image_tag):
# Initialize Docker client
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
# Build the Docker image
image, build_logs = client.images.build(
path=os.path.dirname(dockerfile_path),
dockerfile=os.path.basename(dockerfile_path),
tag=f"{image_name}:{image_tag}",
)
# Print build logs
for log in build_logs:
print(log)
def push_docker_image(username, image_name, image_tag, github_token):
# Initialize Docker client
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
# Authenticate with GitHub Docker Registry using access token
client.login(username=username, password=github_token, registry="ghcr.io")
# Push the Docker image to GitHub Docker Registry
client.images.push(repository=f"ghcr.io/{image_name}", tag=image_tag)
def handler(job):
job_input = job["input"]
# GitHub access token
github_token = os.environ["GITHUB_TOKEN"]
# GitHub repository information
username = job_input["username"]
repo_name = job_input["repo_name"]
repo_url = f"https://{github_token}:[email protected]/{username}/{repo_name}.git"
# Docker image information
dockerfile_path = f"./{repo_name}/Dockerfile"
image_name = f"{username}/{repo_name}"
image_tag = job_input.get("image_tag", "latest")
# Clone the GitHub repository
clone_repo(repo_url, repo_name)
# Build the Docker image
build_docker_image(dockerfile_path, image_name, image_tag)
# Push the Docker image to GitHub Docker Registry
result = push_docker_image(username, image_name, image_tag, github_token)
return result
runpod.serverless.start({"handler": handler})
import runpod
import os
import git
import docker
def clone_repo(repo_url, repo_name):
# Clone the GitHub repository
git.Repo.clone_from(repo_url, repo_name)
def build_docker_image(dockerfile_path, image_name, image_tag):
# Initialize Docker client
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
# Build the Docker image
image, build_logs = client.images.build(
path=os.path.dirname(dockerfile_path),
dockerfile=os.path.basename(dockerfile_path),
tag=f"{image_name}:{image_tag}",
)
# Print build logs
for log in build_logs:
print(log)
def push_docker_image(username, image_name, image_tag, github_token):
# Initialize Docker client
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
# Authenticate with GitHub Docker Registry using access token
client.login(username=username, password=github_token, registry="ghcr.io")
# Push the Docker image to GitHub Docker Registry
client.images.push(repository=f"ghcr.io/{image_name}", tag=image_tag)
def handler(job):
job_input = job["input"]
# GitHub access token
github_token = os.environ["GITHUB_TOKEN"]
# GitHub repository information
username = job_input["username"]
repo_name = job_input["repo_name"]
repo_url = f"https://{github_token}:[email protected]/{username}/{repo_name}.git"
# Docker image information
dockerfile_path = f"./{repo_name}/Dockerfile"
image_name = f"{username}/{repo_name}"
image_tag = job_input.get("image_tag", "latest")
# Clone the GitHub repository
clone_repo(repo_url, repo_name)
# Build the Docker image
build_docker_image(dockerfile_path, image_name, image_tag)
# Push the Docker image to GitHub Docker Registry
result = push_docker_image(username, image_name, image_tag, github_token)
return result
runpod.serverless.start({"handler": handler})
14 replies