This guide explains how to package your Python policy server into a Docker container, so that it can be run and tested consistently by instructors or TAs.
Before you build a Docker image, make sure your policy server can:
- start successfully, and
- respond to at least one inference request.
From the repo root:
cd grasp-cube-sample
python serve_policy.py \
--host 0.0.0.0 --port 8000 \
--policy.checkpoint rl/tdmpc2/step_00256000.pt \
--policy.device autoNotes:
- If you want a fast CPU-only smoke test, add
--policy.no-mpc(or reduce--policy.num-samples). - Your server should always listen on
0.0.0.0(same requirement as Docker).
In another terminal:
cd grasp-cube-sample
python - <<'PY'
import numpy as np
from env_client.websocket_client_policy import WebsocketClientPolicy
client = WebsocketClientPolicy("127.0.0.1", 8000)
client.reset()
obs = {
"images": {
"front": (np.random.rand(480, 640, 3) * 255).astype(np.uint8),
"left_wrist": (np.random.rand(480, 640, 3) * 255).astype(np.uint8),
"right_wrist": (np.random.rand(480, 640, 3) * 255).astype(np.uint8),
},
"states": {
"left_arm": np.random.randn(6).astype(np.float32),
"right_arm": np.random.randn(6).astype(np.float32),
},
}
resp = client.infer(obs)
print("action shape:", np.asarray(resp["action"]).shape)
print("server_timing:", resp.get("server_timing"))
PYYour project may look like:
student_repo/
├── serve_policy.py
└── ...other files
Create a file named Dockerfile in the root of your repo:
# Use a proper image
FROM ...
# Set working directory
WORKDIR /app
# Copy the needed files
COPY ...
# Install system dependencies if needed
RUN apt-get update && apt-get install -y \
git \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies
RUN ...
# Default command to run your policy server
CMD ["python", "serve_policy.py"]Make sure your server listens on
0.0.0.0, not127.0.0.1.
Run the following in your project directory:
docker build -t <your-policy-server> .The instructor or TA can run the container and map any host port to the container port:
docker run --rm --gpus all -p 8000:8000 -v $(pwd)/models:/models <your-policy-server> [args for serve_policy.py]- Replace
8000with any desired port. - The server inside the container must listen on
0.0.0.0. - run with GPU support (requires NVIDIA Container Toolkit)
- Provide a Dockerfile in your repository, or a pre-built Docker image file (
.tar). - Make sure your server (
serve_policy.py) listens on 0.0.0.0 so it can accept connections from outside the container. - The image should be runnable by the TA without additional setup.
- Export image as
.tar:
docker save -o your-policy-server.tar your-policy-server- TA can import and run:
docker load -i your-policy-server.tar
docker run ...