Overview
This example demonstrates the basics of containerizing a Python application using Buildr. You’ll learn how to use the@Containerize decorator to automatically build and run your code in containers.
Complete Example
Here’s a complete example showing two different containerized functions:Step-by-Step Breakdown
Import the Containerize decorator
Containerize decorator from Buildr. This is the core component that will handle containerization.Configure package settings
- repository: The Docker registry repository where your image will be stored
- name: The name of your container image
In production, use a real Docker registry like
docker.io/yourusername or your private registry URL.Containerize with port exposure
- runtime.ports: Array of ports to expose from the container
[80, 8080] - When the container runs, these ports will be accessible from outside
Containerize with publishing
- package.publish: Set to
Trueto push the built image to the Docker registry - Without this, the image is only built locally
Configuration Options Explained
Package Options
| Option | Type | Description |
|---|---|---|
name | string | Container image name |
repository | string | Docker registry repository |
publish | boolean | Whether to push the image to the registry (default: false) |
Runtime Options
| Option | Type | Description |
|---|---|---|
ports | array | List of ports to expose from the container |
executor | string | Container runtime executor (default: docker) |
replicas | number | Number of container instances to run |
Running the Example
What Happens Behind the Scenes
When you run this example:- Detection: Buildr checks if it’s already running inside a container
- Dockerfile Generation: Creates a Python-based Dockerfile if needed
- Build: Builds the Docker image with tag
repo/something:latest - Publish (if enabled): Pushes the image to the registry
- Run: Starts the container with specified runtime options
- Logs: Streams container output to your terminal
The first function call (
hihi()) will publish the image because publish: True is set. The second call (container_with_port()) will only run locally.