Create a flexible Serverless worker that supports a Pod-first development workflow.
handler.py
) that adapts its behavior based on a user-specified environment variable.start.sh
) to manage different operational modes.handler.py
: Your Python script with the RunPod handler logic.start.sh
: A shell script that will be the entrypoint for your Docker container.Dockerfile
: Instructions to build your Docker image.requirements.txt
: A file to list Python dependencies.handler.py
fileMODE_TO_RUN
to determine whether to run in Pod or Serverless mode.
Add the following code to handler.py
:
MODE_TO_RUN = os.getenv("MODE_TO_RUN", "pod")
: Reads the mode from an environment variable, defaulting to pod
.async def handler(event)
: Your core logic.if __name__ == '__main__':
: This block controls what happens when the script is executed directly.
serverless
” mode, it starts the RunPod Serverless worker.pod
mode, it runs a sample test call to your handler
function, allowing for quick iteration.start.sh
scriptMODE_TO_RUN
environment variable and configures the container accordingly.
Add the following code to start.sh
:
case $MODE_TO_RUN in ... esac
: This structure directs the startup based on the mode.serverless
mode: Executes handler.py
, which then starts the RunPod Serverless worker. exec
replaces the shell process with the Python process.pod
mode: Starts up the JupyterLab server for Pod development, then runs sleep infinity
to keep the container alive so you can connect to it (e.g., via SSH or docker exec
). You would then manually run python /app/handler.py
inside the Pod to test your handler logic.Dockerfile
Dockerfile
:
FROM runpod/pytorch:2.0.1-py3.10-cuda11.8.0-devel-ubuntu22.04
: Starts with a RunPod base image that comes with nginx, runpodctl, and other helpful base packages.ARG WORKSPACE_DIR=/workspace
and ENV WORKSPACE_DIR=${WORKSPACE_DIR}
: Allows the workspace directory to be set at build time.WORKDIR $WORKSPACE_DIR
: Sets the working directory to the value of WORKSPACE_DIR
.COPY requirements.txt ./requirements.txt
and RUN pip install ...
: Installs Python dependencies.COPY . .
: Copies all application files into the workspace directory.ENV MODE_TO_RUN="pod"
: Sets the default operational mode to “pod”. This can be overridden at runtime.CMD ["$WORKSPACE_DIR/start.sh"]
: Specifies start.sh
as the command to run when the container starts.Build your Docker image
[YOUR_USERNAME]
with your Docker Hub username and choosing a suitable image name:--platform linux/amd64
flag is important for compatibility with RunPod’s infrastructure.Push the image to your container registry
docker login
first.[YOUR_USERNAME]/dual-mode-worker:latest
.MODE_TO_RUN
and the value to pod
./app
and run your handler directly:
handler.py
, giving you immediate feedback. You can edit handler.py
within the Pod and re-run it for rapid iteration.
handler.py
logic tested in Pod mode, you’re ready to deploy your dual-mode worker to a Serverless endpoint.
docker.io/[YOUR_USERNAME]/dual-mode-worker:latest
.MODE_TO_RUN
to serverless
.start.sh
will now direct it to run in Serverless mode, starting the runpod.serverless.start
worker.
Develop using Pod mode
MODE_TO_RUN
is set to pod
(or rely on the Dockerfile default)./app
directory.pip install [PACKAGE_NAME]
) or system dependencies (apt-get install [PACKAGE_NAME]
).handler.py
script. Test your changes frequently by running python handler.py
directly in the Pod’s terminal. This will execute the test harness you defined in the elif MODE_TO_RUN == "pod":
block, giving you immediate feedback.Update your Docker image
requirements.txt
file.RUN apt-get update && apt-get install -y [PACKAGE_NAME]
) to your Dockerfile
.handler.py
is saved.Deploy and test in Serverless mode
MODE_TO_RUN
environment variable for the endpoint is set to serverless
.