Use `docker compose` from within Python. This is a branch forked from https://github.com/schollm/docker-composer to support Docker Compose V2.
Project description
Docker Composer
A library to interact with docker compose
(Compose V2) from a python Program.
All commands and parameters are exposed as python classes and attributes
to allow for full auto-completion of its parameters with IDEs
that support it.
This project is based off the open source project: https://github.com/schollm/docker-composer The main goal for this branch-off effort is to make it work for the latest Docker Compose V2.
Install
pip install docker-composer-v2
Usage
The main class is docker_composer.DockerCompose
. Its parameters are
all options from docker compose
.
Each docker compose
subcommand has a corresponding function, e.g.
DockerCompose.run
or DockerCompose.scale
. Their arguments again mirror
the options of the corresponding command.
The resulting object has a call
function.
It takes arbitrary strings as input, as well as all keyword arguments from
subprocess.run
, and returns a subprocess.CompletedProcess
import os
from docker_composer import DockerCompose
# set log level and verbose
os.environ["DOCKER_COMPOSE_LOG_LEVEL"] = "info"
# Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
base = DockerCompose(file="docker-compose.yml")
base.run(detach=True, workdir="/tmp").call("app")
base.run(workdir="/tmp").call("app", "/bin/bash", "-l")
# /tmp $ ls /.dockerenv
# /.dockerenv
# /tmp $ exit
process = base.ps(all=True).call(capture_output=True)
print(str(process.stdout, encoding="utf-8"))
# Name Command State Ports
# -------------------------------------------------------------------
# myapp_app_70fd8b786b76 myapp --start-server Exit 0
# myapp_app_6ac3db4e1b55 myapp --client Exit 0
Develop
Coding Standards
Type | Package | Comment |
---|---|---|
Linter | black |
Also for auto-formatted modules |
Logging | loguru |
|
Packaging | poetry |
|
Tests | pytest |
|
Typing | mypy |
Type all methods |
Imports | isort |
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
Built Distribution
Hashes for docker_composer_v2-0.8.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 945128fb5bf1e3dbf96920f4de3bbb02558d3ac9ccb26100a61ad5600e7ae031 |
|
MD5 | f73abbb1b354dacf29dc4a288bd4c48c |
|
BLAKE2b-256 | dcee3484f8e9e412ac0c61af2e0f44be1e8428a1b62ac6bb9139ebbc8e3e1ec2 |