Stack
Profiles and environments
Set Compose profiles and interpolation files once on the stack; they apply to all its Compose operations. For example, select the environment before creating the stack:
import os
from orche import Stack
environment = os.environ.get("ORCHE_ENV", "dev")
if environment not in {"dev", "prod"}:
raise ValueError(f"Unknown environment: {environment}")
stack = Stack(
path=".",
profiles=["debug"] if environment == "dev" else [],
env_files=[".env", f".env.{environment}"],
)
Run with ORCHE_ENV=prod orche up. Relative env file paths are resolved against
Stack.path; explicit files must exist when the stack is created. Compose reads
the files in order, with later files overriding earlier ones. Shell variables
take precedence for Compose interpolation. When no env files are supplied,
Compose's normal default .env behavior applies. These are Compose CLI
interpolation files, distinct from a service's env_file setting. See the
Docker Compose interpolation documentation.
Main orchestrator for Docker Compose stacks.
Source code in orche/stack.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
__init__(name=None, path='.', compose_files=None, host=None, profiles=None, env_files=None)
Initialize a Docker Compose stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Optional project name (defaults to directory name) |
None
|
path
|
str | Path
|
Project root path (defaults to current directory) |
'.'
|
compose_files
|
list[str] | list[Path] | None
|
List of docker-compose file paths (relative to project path or abs). Files are merged in order (later files override earlier ones). Defaults to ["docker-compose.yml"] if None. Cannot be an empty list. |
None
|
host
|
str | None
|
Host name or IP used to build the URLs in
|
None
|
profiles
|
list[str] | None
|
Compose profiles to enable for every operation. |
None
|
env_files
|
list[str | Path] | None
|
Ordered Compose interpolation env files, relative to the project path or absolute. Later files override earlier ones. |
None
|
Raises:
| Type | Description |
|---|---|
ConfigError
|
If compose_files is empty or a specified file is missing |
Source code in orche/stack.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
build(services=None)
Build services in the stack.
If 'services' is not provided, uses the active services from CLI args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
list[str] | None
|
Optional list of specific services to build |
None
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining |
Source code in orche/stack.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
client()
Get the underlying DockerComposeWrapper instance.
Source code in orche/stack.py
499 500 501 | |
down(volumes=False)
Stop and remove the whole stack, including its networks.
Like docker compose down, this never targets specific services. Use
:meth:rm for that.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volumes
|
bool
|
Whether to remove named volumes |
False
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If services were selected on the command line |
Source code in orche/stack.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
on(services)
Return True if at least one of the specified services is active.
This method uses OR logic across the provided service names and checks
membership against the current execution context (self._active_services).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
str | list[str]
|
A service name or list of service names to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any of the specified services are active, False otherwise. |
Source code in orche/stack.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
parse_args(command, tokens)
Split command-line tokens into service names and handler options.
Options are the parameters of the command handler, so the handler's signature decides which tokens are option values and which are services.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command name the tokens belong to. |
required |
tokens
|
list[str]
|
Everything that followed the command name. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[str], dict[str, Any]]
|
The service names and the resolved option values, defaults included. |
Raises:
| Type | Description |
|---|---|
CommandError
|
If the command is unknown or the options are invalid. |
Source code in orche/stack.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
print_services_summary()
Print a Rich panel listing running services and their local URLs.
Source code in orche/stack.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
rm(services=None, volumes=False)
Stop and remove service containers, keeping networks and volumes.
If 'services' is not provided, uses the active services from CLI args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
list[str] | None
|
Optional list of specific services to remove |
None
|
volumes
|
bool
|
Whether to remove anonymous volumes of the containers |
False
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining |
Source code in orche/stack.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
run(command, services=None, options=None)
Execute a command with before/after hooks.
Execution order
- Before-hooks run sequentially. First failure raises
HookErrorand aborts (remaining hooks and handler are skipped). - Main handler runs only if all before-hooks succeeded.
Failures raise
CommandError;KeyboardInterruptpropagates. - After-hooks run sequentially. First failure raises
HookErrorand aborts (remaining after-hooks are skipped).
Hooks receive only the options they declare as parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command name to execute. |
required |
services
|
list[str] | None
|
List of service names. |
None
|
options
|
dict[str, Any] | None
|
Option values for the handler, keyed by parameter name. Omitted options take the handler's defaults. |
None
|
Raises:
| Type | Description |
|---|---|
CommandError
|
If the command is not registered, the options are invalid, or the handler fails. |
HookError
|
If a before- or after-hook fails. |
Source code in orche/stack.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
stop(services=None)
Stop services without removing them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
list[str] | None
|
Optional list of specific services to stop |
None
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining |
Source code in orche/stack.py
390 391 392 393 394 395 396 397 398 399 400 401 | |
up(services=None, wait=True)
Start services in the stack.
If 'services' is not provided, uses the active services from CLI args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
list[str] | None
|
Optional list of specific services to start |
None
|
wait
|
bool
|
If True, wait for services to be running |
True
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining |
Source code in orche/stack.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
wait_for_service(service, timeout=60, interval=1)
Wait for every container of a service to be running and healthy.
The service must define a healthcheck. Missing, starting and unhealthy containers are polled until the timeout; exited or dead containers and running containers without a healthcheck fail immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
str
|
Compose service name. |
required |
timeout
|
float
|
Maximum polling time in seconds (positive and finite). |
60
|
interval
|
float
|
Delay between polls in seconds (positive and finite). |
1
|
Returns:
| Type | Description |
|---|---|
Stack
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If arguments are invalid. |
DockerComposeError
|
If inspection fails, a container cannot become healthy, or the timeout expires. |
Source code in orche/stack.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |