What you’ll learn
In this tutorial you’ll learn how to:- Set up your development environment.
- Create a handler file.
- Test your handler locally.
- Build a Docker image for deployment.
- Deploy and test your worker on the Runpod console.
Requirements
- You’ve created a Runpod account.
- You’ve installed Python 3.x and Docker on your local machine and configured them for your command line.
Step 1: Set up your project directory
Use the command line to set up your project directory and a virtual environment to manage dependencies.1
Create a directory for your project and navigate to it
2
Create a virtual environment
3
Activate the Python virtual environment
- macOS/Linux
- Windows
4
Install the Runpod SDK
5
Create the necessary project files
rp_handler.py: Your handler function, which will contain your custom logic for processing requests to your Serverless endpoint.test_input.json: A test input file for local testing of your handler function.Dockerfile: Instructions for building your handler function into a Docker image for deployment to Runpod.
Step 2: Create a handler file
Using a text editor of your choice, add the following code to yourrp_handler.py file:
prompt string contained in the input object.
You can replace the time.sleep(seconds) call with your own custom Python code for generating images, text, or running any machine learning workload.
Step 3: Create a test input file
Add the following lines to yourtest_input.json file:
Step 4: Test your handler locally
Use the command line to test your handler locally:Step 5: Create a Dockerfile
Add the following instructions to yourDockerfile:
Step 6: Build and push your Docker image
Before you can deploy your worker on Runpod Serverless, you need to push it to Docker Hub:1
Build your Docker image
Use the command line to build your Docker image, specifying the platform for Runpod deployment, replacing
DOCKER_USERNAME with your Docker Hub username:2
Push the image to your container registry
Step 7: Deploy your worker using the Runpod console
To deploy your worker to a Serverless endpoint:- Go to the Serverless section of the Runpod console.
- Click New Endpoint.
- Click Import from Docker Registry
- In the Container Image field, enter your Docker image URL:
docker.io/DOCKER_USERNAME/serverless-worker-tutorial:latest. - Click Next to proceed to endpoint configuration.
- Configure your endpoint settings:
- (Optional) Enter a custom name for your endpoint, or use the randomly generated name.
- Make sure the Endpoint Type is set to Queue.
- Under GPU Configuration, check the box for 16 GB GPUs.
- Leave the rest of the settings at their defaults.
- Click Deploy Endpoint.
Step 8: Test your worker
To test your worker, click the Requests tab in the endpoint detail page:
Next steps
Now that you’ve learned the basics, you’re ready to:- Create more advanced handler functions.
- Send endpoint requests using cURL and the Serverless SDK.
- Learn how to use endpoint operations like
/runand/status. - Manage your Serverless endpoints using the Runpod console.
- Configure your endpoints for optimal performance and cost.
- Learn more about local testing.