Skip to main content

Utilities for running Docker images with Python

Project description

ambergris

CI Status PyPI Version Python Versions Code style: black Checked with mypy License: MIT

ambergris is a lightweight Python utility that simplifies the process of running commands in Docker containers.

Let's say you have a Docker image containing a simple Python tool that reads in .xlsx files, and stores the processed results as a CSV. A typical example of how ambergris can help here is as follows. The example covers path translation, creating bind-type mounts, and executing a command in a container:

import ambergris
import logging

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

@ambergris.make_io_relative_to_container
def my_bash_command(in_data, out_file, mounts):
    return f"/bin/bash -c 'python3 my_tool.py --in {in_data} --out {out_file}.csv'"
    
if __name__ == "__main__":
    # makes mount mappings: (host-path -> container-path)
    mounts = ambergris.make_bindmounts(
        ("/home/you/inputs/", "/container/in/"),
        ("/home/you/outputs/", "/container/out/")
    )
    cmd = my_bash_command("/home/you/inputs/data.xlsx", out_file="/home/you/outputs/data.csv", mounts=mounts)
    output, exit_code = ambergris.run_command("my-image:0.1", cmd, mounts)
    with open("/home/you/outputs/data.csv", "r") as f:
        print(f.readlines())

ambergris automatically closes and cleans up containers when your image has stopped running. It also supports running from images that are stored remotely in a Docker registry, locally in the Docker daemon, or locally from a tar(.gz) archived image. For example:

import ambergris

ambergris.run_command(image="/home/you/my_python_image.tar.gz", cmd=["python3", "my_program.py", "-h"])

Installation

pip install ambergris

Quick Start

1. Logging Setup

Ambergris uses the standard logging module. To see container output and lifecycle warnings, set your log level to INFO:

import logging
import ambergris

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

2. Running Tasks

Use run_container for "one-and-done" scripts. This handles the full lifecycle: loading the image, running the command, and cleaning up the container.

# Create mounts (Host Path, Container Path)
mounts = ambergris.make_bindmounts(
    ("~/input_data", "/in"),
    ("./results", "/out")
)

# Run a command (supports strings or lists)
command = ["/bin/sh", "-c", "ls -l /in"]

output, exit_code = ambergris.run_container("alpine:latest", command, mounts=mounts)

print(f"Exited with code: {exit_code}")
for line in output:
    print(line)

3. Persistent Sessions

Use open_container to run multiple commands within the same environment without the overhead of restarting the container.

with ambergris.open_container("alpine:latest", mounts) as container:
    # Execute multiple commands in the same instance
    ambergris.exec_command(container, "touch /tmp/session.log")
    ambergris.exec_command(container, 'sh -c "echo 'hello' > /tmp/session.log"')
    
    results = ambergris.exec_command(container, "cat /tmp/session.log")
    print(results)  # ['hello']

# Container is automatically stopped and removed after the 'with' block

4. Working with Local Tar Archives

If you have an image saved as a tarball (raw .tar or gzipped .tar.gz), simply pass the path instead of an image name. ambergris will automatically load it into the Docker daemon.

output, exit_code = ambergris.run_container("./my_custom_image.tar.gz", command=["whoami"])

5. Automatic Path Mapping (Decorator)

The @make_io_relative_to_container decorator is useful when your Python functions take host Path objects but need to reference where those files live inside the container.

from pathlib import Path

@ag.make_io_relative_to_container
def process_data(input_file: Path, mounts: list):
    print(f"Path inside container: {input_file}")

Development

If you are contributing to ambergris, install the dev dependencies:

pip install -e ".[dev]"
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ambergris-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ambergris-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file ambergris-0.1.0.tar.gz.

File metadata

  • Download URL: ambergris-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ambergris-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0d55033e6064907001c027d1820f691b06950c524a8d516de4e0fdbc09805b9
MD5 fc0ec96fd3f79cd1089cef3902b74b7a
BLAKE2b-256 b24c0c92762a0163e36f838e8479e34c309ff94d754b937023be011d2b8aef4e

See more details on using hashes here.

File details

Details for the file ambergris-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ambergris-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ambergris-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d72e457b4c822f46e444c8377704eed055aae7c256677cea568b18338dd0ec8
MD5 927ecbda792330aa06eb163d5c247d70
BLAKE2b-256 b34425a13f57a417a9722f8b550029978c85c6833974f718334035b100772b3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page