Use Docker Compose Integration
This guide shows how to use Angreal’s Docker Compose integration to manage multi-container applications.
Quick Reference Card
Section titled “Quick Reference Card”Essential Commands
Section titled “Essential Commands”from angreal.integrations.docker import compose
# Initializestack = compose("docker-compose.yml") # Basicstack = compose("docker-compose.yml", "project-name") # With project name
# Lifecyclestack.up(detach=True) # Start servicesstack.down() # Stop and removestack.restart() # Restart allstack.stop() # Stop (keep containers)stack.start() # Start stopped containers
# Monitoringstack.ps() # List containersstack.logs(tail="50") # View logsstack.config() # Validate config
# Operationsstack.build() # Build imagesstack.pull() # Pull imagesstack.exec("web", ["bash"]) # Run commandCommon Patterns
Section titled “Common Patterns”# Development workflowstack.down(volumes=True) # Clean slatestack.up(detach=True, build=True) # Rebuild & startstack.logs(services=["web"], follow=True) # Watch logs
# Service managementstack.up(services=["db"]) # Start specificstack.restart(services=["web", "worker"]) # Restart multiplestack.exec("db", ["psql", "-U", "postgres"]) # Database access
# Debuggingresult = stack.up()if not result.success: print(result.stderr) # Check errorsKey Options
Section titled “Key Options”| Method | Key Parameters | Purpose |
|---|---|---|
up() |
detach, build, services |
Start containers |
down() |
volumes, remove_orphans |
Clean up |
logs() |
follow, tail, services |
View output |
exec() |
user, env, workdir |
Run commands |
build() |
no_cache, pull |
Build images |
Prerequisites
Section titled “Prerequisites”- Docker and Docker Compose installed
- A
docker-compose.ymlfile in your project - Angreal with Docker integration
Quick Start
Section titled “Quick Start”from angreal.integrations.docker import compose
# Start servicesstack = compose("docker-compose.yml")result = stack.up(detach=True)
if result.success: print("Services started")else: print(f"Failed: {result.stderr}")Common Operations
Section titled “Common Operations”Start Services
Section titled “Start Services”# Basic startstack.up(detach=True)
# Build and startstack.up(detach=True, build=True)
# Start specific servicesstack.up(detach=True, services=["web", "db"])Monitor Services
Section titled “Monitor Services”# Check statusresult = stack.ps()print(result.stdout)
# View logsstack.logs(tail="100")stack.logs(services=["web"], follow=True)Execute Commands
Section titled “Execute Commands”# Run commands in containersstack.exec("web", ["bash"])stack.exec("web", ["python", "manage.py", "migrate"])stack.exec("db", ["psql", "-U", "postgres"], user="postgres")Manage Lifecycle
Section titled “Manage Lifecycle”# Restart servicesstack.restart()stack.restart(services=["web"])
# Stop without removingstack.stop()
# Start stopped containersstack.start()Build and Update
Section titled “Build and Update”# Build imagesstack.build()stack.build(no_cache=True, services=["web"])
# Pull latest imagesstack.pull()Clean Up
Section titled “Clean Up”# Stop and remove containersstack.down()
# Also remove volumesstack.down(volumes=True)
# Remove orphaned containersstack.down(remove_orphans=True)Working with Environments
Section titled “Working with Environments”Use different compose files for different environments:
# Developmentdev = compose("docker-compose.dev.yml", project_name="myapp-dev")dev.up(detach=True)
# Productionprod = compose("docker-compose.prod.yml", project_name="myapp-prod")prod.up(detach=True)Create Angreal Tasks
Section titled “Create Angreal Tasks”Basic Task
Section titled “Basic Task”# .angreal/docker_tasks.pyimport angrealfrom angreal.integrations.docker import compose
@angreal.command(name="dev-up", about="Start development stack")@angreal.argument(name="build", long="build", is_flag=True, help="Rebuild images")def start_dev(build=False): """Start development services""" stack = compose("docker-compose.dev.yml")
result = stack.up(detach=True, build=build) if result.success: print("✓ Services started") print(stack.ps().stdout) else: print(f"✗ Failed: {result.stderr}") return 1Database Reset Task
Section titled “Database Reset Task”@angreal.command(name="db-reset", about="Reset database")def reset_database(): """Reset database to clean state""" stack = compose("docker-compose.yml")
# Stop and remove volumes stack.down(volumes=True)
# Start fresh database result = stack.up(detach=True, services=["db"]) if not result.success: return 1
# Run migrations import time time.sleep(5) # Wait for DB
result = stack.exec("web", ["python", "manage.py", "migrate"]) return 0 if result.success else 1Test Runner
Section titled “Test Runner”@angreal.command(name="test", about="Run tests in Docker")def run_tests(): """Run integration tests""" stack = compose("docker-compose.test.yml")
try: # Start test environment if not stack.up(detach=True, build=True).success: return 1
# Run tests result = stack.exec("test", ["pytest", "-v"]) print(result.stdout) return 0 if result.success else 1
finally: # Clean up stack.down(volumes=True)Error Handling
Section titled “Error Handling”All operations return a ComposeResult with:
success: Boolean indicating successexit_code: Process exit codestdout: Command outputstderr: Error messages
result = stack.up(detach=True)if not result.success: print(f"Error: {result.stderr}") print(f"Exit code: {result.exit_code}")Best Practices
Section titled “Best Practices”- Check availability: Use
DockerCompose.is_available()before operations - Use project names: Isolate environments with unique project names
- Check results: Always verify
result.success - Clean up: Use
down()to remove resources when done - Handle errors: Check
stderrfor error details
Quick Reference
Section titled “Quick Reference”from angreal.integrations.docker import compose
stack = compose("docker-compose.yml", project_name="myapp")
# Lifecyclestack.up(detach=True, build=True) # Start servicesstack.down(volumes=True) # Stop and clean upstack.restart(services=["web"]) # Restart services
# Monitoringstack.ps(all=True) # List containersstack.logs(follow=True, tail="100") # View logs
# Operationsstack.build(no_cache=True) # Build imagesstack.exec("web", ["bash"]) # Run commandsstack.pull() # Update imagesSee the API Reference for complete details.