Project organization
Organize your project files in a clear directory structure:requirements.txt file should list all Python packages your handler needs:
requirements.txt
Basic Dockerfile structure
A basic Dockerfile for a Runpod Serverless worker follows this structure:Dockerfile
- Starts with a Python base image.
- Sets the working directory to the root.
- Copies and installs Python dependencies.
- Copies your handler code.
- Specifies the command to run when the container starts.
Choosing a base image
The base image you choose affects your image size, startup time, and available system dependencies. Common options include:Python slim images
Recommended for most use cases. These images are smaller and faster to download:Python full images
Include more system tools and libraries but are larger:CUDA images
Required if you need CUDA libraries for GPU-accelerated workloads:Custom base images
You can build on top of specialized images for specific frameworks:Including models and files
Baking models into the image
If you need to include model files or other assets in your image, use theCOPY instruction:
Dockerfile
Downloading models during build
You can download models during the Docker build process:Dockerfile
Environment variables
Set environment variables to configure your application without hardcoding values:Dockerfile
Optimizing image size
Smaller images download and start faster, reducing cold start times. Use these techniques to minimize image size:Use multi-stage builds
Multi-stage builds let you compile dependencies in one stage and copy only the necessary files to the final image:Dockerfile
Clean up build artifacts
Remove unnecessary files after installation:Dockerfile
Use .dockerignore
Create a.dockerignore file to exclude unnecessary files from the build context:
.dockerignore
Next steps
After creating your Dockerfile, you can:- Build and deploy your image from Docker Hub.
- Deploy directly from GitHub.
- Test your handler locally before building the image.