Utilities for running Docker images with Python
Project description
ambergris
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ambergris-0.1.0.1.tar.gz.
File metadata
- Download URL: ambergris-0.1.0.1.tar.gz
- Upload date:
- Size: 3.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b69955b7a5ac34cbdd9dfa35420dda5f5e7ea470381b17f81ea6914c6483066
|
|
| MD5 |
8d389f61a54e3a31143ef8f6ae7a09f9
|
|
| BLAKE2b-256 |
8211f7c2e6281305ecff0ca658742e0702a464848935fab5fd2a9d8dfb516b74
|
File details
Details for the file ambergris-0.1.0.1-py3-none-any.whl.
File metadata
- Download URL: ambergris-0.1.0.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b91f62e612b38c876f7d6cd7c7843b8984a2019d660668d2a3d511696efa5f4b
|
|
| MD5 |
9234adaca46c46026cb60a59dd431def
|
|
| BLAKE2b-256 |
103bc9053f0f30bf7ac15510451db6de1f5d6500703121c2cd817e1aefe8692f
|