Overview
TheDockerRunner class provides a simple interface for running containerized applications using Docker. This runner is ideal for local development and testing, allowing you to run your Buildr applications in isolated Docker containers with port mapping and logging capabilities.
When to Use DockerRunner
UseDockerRunner when you:
- Want to run applications locally on your development machine
- Need quick iteration and testing without cloud deployment
- Require direct access to container logs
- Want to expose container ports to your local machine
Class Reference
DockerRunner
Initializes a new Docker runner instance.docker_client- Docker API client instance (initialized lazily)container- Reference to the running container
Methods
run()
Launches a Docker container with the specified image and configuration.The Docker image to run (e.g.,
"myapp:latest" or "python:3.9")Name to assign to the container for identification
Configuration object with the following properties:
List of port numbers to expose and map. Ports are automatically bound from container to host with the same port number.Example:
[8080, 3000] will map container ports 8080 and 3000 to the same ports on the host.- Creates a Docker API client if not already initialized
- Configures port bindings when ports are specified
- Creates and starts the container
- Stores container reference for subsequent operations
- Logs container creation details
logs()
Streams and displays container logs in real-time.- Creates a Docker API client if not already initialized
- Streams logs from the container in real-time
- Follows log output continuously (similar to
docker logs -f) - Logs each line to the application logger
The
logs() method will block while streaming logs. It continues following the log output until the container stops or the process is interrupted.cancel()
Stops and removes the running container.Name of the container to cancel (provided for interface consistency, but the stored container reference is used)
- Creates a Docker API client if not already initialized
- Kills the running container immediately
- Removes the container and its resources
- Cleans up the container from the Docker host
Configuration Examples
Basic Container Without Ports
Web Application with Multiple Ports
Complete Workflow Example
Implementation Details
Docker API Client:- Uses the
docker-pylibrary (APIClient) - Automatically detects Docker API version with
version='auto' - Lazily initialized on first method call
- Ports are mapped 1:1 from container to host
- All ports use TCP protocol
- Port bindings use Docker’s default host configuration
- Container is created with specified configuration
- Container is started immediately after creation
- Container reference is stored for log streaming and cancellation
- Container is killed and removed on cancellation
See Also
- MetaparticleRunner - For cloud deployments with Kubernetes
- RuntimeOptions - Configure the executor and runtime settings