Skip to content

Commit 3ab3e87

Browse files
committed
fix: improve docker build process and error handling
- Replace r8.im base image with standard Python 3.10 image - Update runpod package to version 1.7.13 - Add proper error handling for Cog server startup - Improve documentation and testing setup - Clean up Dockerfile and build process
1 parent ea3bae3 commit 3ab3e87

5 files changed

Lines changed: 99 additions & 17 deletions

File tree

CHANGES.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Changes Made
2+
3+
## Dockerfile
4+
- Replaced r8.im base image with standard Python 3.10 image
5+
- Updated runpod package to version 1.7.13
6+
- Added explicit COPY for test_input.json
7+
- Simplified build process by removing build arguments
8+
- Added proper cleanup after apt-get operations
9+
10+
## README.md
11+
- Updated documentation to reflect new build process
12+
- Removed build arguments section
13+
- Added section about customizing for specific models
14+
- Added testing information
15+
16+
## src/handler.py
17+
- Enhanced error handling for Cog server startup
18+
- Added alternative method to start Cog server if primary method fails
19+
- Improved logging
20+
- Added input validation
21+
22+
## Added Files
23+
- Ensured test_input.json is properly copied into the Docker image for RunPod SDK automated testing

Dockerfile

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
ARG COG_REPO
2-
ARG COG_MODEL
3-
ARG COG_VERSION
4-
5-
FROM r8.im/${COG_REPO}/${COG_MODEL}@sha256:${COG_VERSION}
1+
# Use a standard Python image as base
2+
FROM python:3.10-slim
63

74
ENV RUNPOD_REQUEST_TIMEOUT=600
85

9-
# Install necessary packages and Python 3.10
6+
# Install necessary packages
107
RUN apt-get update && apt-get upgrade -y && \
11-
apt-get install -y --no-install-recommends software-properties-common curl git openssh-server && \
12-
add-apt-repository ppa:deadsnakes/ppa -y && \
13-
apt-get update && apt-get install -y --no-install-recommends python3.10 python3.10-dev python3.10-distutils && \
14-
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 &&\
15-
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
16-
python3 get-pip.py
8+
apt-get install -y --no-install-recommends \
9+
software-properties-common \
10+
curl \
11+
git \
12+
openssh-server \
13+
build-essential \
14+
&& apt-get clean \
15+
&& rm -rf /var/lib/apt/lists/*
1716

1817
# Create a virtual environment
1918
RUN python3 -m venv /opt/venv
2019

21-
# Install runpod within the virtual environment
22-
RUN /opt/venv/bin/pip install runpod
20+
# Install Cog and RunPod
21+
RUN /opt/venv/bin/pip install --upgrade pip && \
22+
/opt/venv/bin/pip install cog runpod==1.7.13
23+
24+
# Copy the test input file
25+
COPY test_input.json /
26+
27+
# Add the handler script
28+
COPY src/handler.py /rp_handler.py
2329

24-
ADD src/handler.py /rp_handler.py
30+
# Set the working directory
31+
WORKDIR /
2532

33+
# Start the handler
2634
CMD ["/opt/venv/bin/python3", "-u", "/rp_handler.py"]

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,43 @@ Easily convert cog based images to a runpod serverless worker.
88

99
## Getting Started
1010

11+
This repository provides a template for creating RunPod serverless workers from Cog-based models.
12+
13+
### Building the Docker Image
14+
1115
```bash
1216
git clone https://github.com/runpod-workers/cog-worker.git
1317

1418
cd cog-worker/
1519

16-
docker build --tag user/repo:tag --build-arg COG_REPO=user --build-arg COG_MODEL=model_name --build-arg COG_VERSION=model_version .
20+
# Build the Docker image
21+
docker build --tag user/repo:tag .
1722

1823
docker push user/repo:tag
1924
```
25+
26+
### Customizing for Your Model
27+
28+
To use this worker with your specific Cog model:
29+
30+
1. Install your model's dependencies in the Dockerfile
31+
2. Create a `predict.py` file with your model's prediction logic
32+
3. Update the handler.py file if needed to work with your specific model
33+
34+
## RunPod Deployment
35+
36+
After pushing your Docker image, you can deploy it as a RunPod Serverless endpoint through the RunPod platform.
37+
38+
## Testing
39+
40+
The repository includes a test_input.json file that can be used to test your worker:
41+
42+
```json
43+
{
44+
"input": {
45+
"prompt": "A beautiful landscape with mountains and a lake"
46+
}
47+
}
48+
```
49+
50+
You can customize this file to match your model's expected input format.

src/handler.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import subprocess
33
import os
4+
import sys
45

56
import runpod
67
import requests
@@ -17,7 +18,17 @@
1718

1819
# ----------------------------- Start API Service ---------------------------- #
1920
# Call "python -m cog.server.http" in a subprocess to start the API service.
20-
subprocess.Popen(["python", "-m", "cog.server.http"])
21+
try:
22+
subprocess.Popen([sys.executable, "-m", "cog.server.http"])
23+
print("Started Cog server")
24+
except Exception as e:
25+
print(f"Error starting Cog server: {e}")
26+
# Try alternative method if the first one fails
27+
try:
28+
subprocess.Popen(["cog", "server"])
29+
print("Started Cog server using alternative method")
30+
except Exception as e2:
31+
print(f"Error starting Cog server with alternative method: {e2}")
2132

2233

2334
# ---------------------------------------------------------------------------- #
@@ -27,6 +38,7 @@ def wait_for_service(url):
2738
'''
2839
Check if the service is ready to receive requests.
2940
'''
41+
print(f"Waiting for service at {url}...")
3042
while True:
3143
try:
3244
health = requests.get(url, timeout=120)
@@ -61,6 +73,9 @@ def handler(event):
6173
'''
6274
This is the handler function that will be called by the serverless.
6375
'''
76+
# Check if input is provided
77+
if not event or "input" not in event:
78+
return {"error": "No input provided"}
6479

6580
json = run_inference({"input": event["input"]})
6681

test_input.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"input": {
3+
"prompt": "A beautiful landscape with mountains and a lake"
4+
}
5+
}

0 commit comments

Comments
 (0)