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.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f97c4e610d2ddba6551ba7ca4aaf0eb1315e5570bfdd03187777b2979ec885d4 |
|
MD5 | 7f90b0745a1f5739a502395090bf16a5 |
|
BLAKE2b-256 | f81169f8b782af4ed9f1642e8c22edb6db359e220e78b711ab648c705eb30a5d |