Skip to main content

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:
from metaparticle_pkg import Containerize
import time

package_repo = 'repo'
package_name = 'something'
sleep_time = 10


@Containerize(
    package={'name': package_name, 'repository': package_repo},
    runtime={'ports': [80, 8080]}
)
def container_with_port():
    print('hello container_with_port')

    for i in range(sleep_time):
        print('Sleeping ... {} sec'.format(i))
        time.sleep(1)


@Containerize(
    package={'name': package_name, 'repository': package_repo, 'publish': True}
)
def hihi():
    print('hello world!')

    for i in range(sleep_time):
        print('Sleeping ... {} sec'.format(i))
        time.sleep(1)


if __name__ == '__main__':
    hihi()
    container_with_port()

Step-by-Step Breakdown

1

Import the Containerize decorator

from metaparticle_pkg import Containerize
import time
Import the Containerize decorator from Buildr. This is the core component that will handle containerization.
2

Configure package settings

package_repo = 'repo'
package_name = 'something'
Set up your package configuration:
  • 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.
3

Containerize with port exposure

@Containerize(
    package={'name': package_name, 'repository': package_repo},
    runtime={'ports': [80, 8080]}
)
def container_with_port():
    print('hello container_with_port')
    for i in range(sleep_time):
        print('Sleeping ... {} sec'.format(i))
        time.sleep(1)
The first function demonstrates 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
Use port exposure when your application needs to accept incoming network connections, such as web servers or APIs.
4

Containerize with publishing

@Containerize(
    package={'name': package_name, 'repository': package_repo, 'publish': True}
)
def hihi():
    print('hello world!')
    for i in range(sleep_time):
        print('Sleeping ... {} sec'.format(i))
        time.sleep(1)
The second function demonstrates image publishing:
  • package.publish: Set to True to push the built image to the Docker registry
  • Without this, the image is only built locally
5

Run the application

if __name__ == '__main__':
    hihi()
    container_with_port()
Execute both containerized functions. Buildr will:
  1. Detect it’s not running in a container
  2. Generate a Dockerfile automatically
  3. Build the Docker image
  4. Run the container
  5. Stream logs back to your terminal

Configuration Options Explained

Package Options

OptionTypeDescription
namestringContainer image name
repositorystringDocker registry repository
publishbooleanWhether to push the image to the registry (default: false)

Runtime Options

OptionTypeDescription
portsarrayList of ports to expose from the container
executorstringContainer runtime executor (default: docker)
replicasnumberNumber of container instances to run

Running the Example

1

Save the code

Save the example code to a file named example.py
2

Create requirements.txt

metaparticle-pkg
3

Run the application

python example.py
Buildr will automatically:
  • Generate a Dockerfile
  • Build the container image
  • Run the containerized application
  • Display the output

What Happens Behind the Scenes

When you run this example:
  1. Detection: Buildr checks if it’s already running inside a container
  2. Dockerfile Generation: Creates a Python-based Dockerfile if needed
  3. Build: Builds the Docker image with tag repo/something:latest
  4. Publish (if enabled): Pushes the image to the registry
  5. Run: Starts the container with specified runtime options
  6. 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.

Build docs developers (and LLMs) love