in
Execute commands inside Docker containers or Kubernetes pods.
Synopsisβ
xec in [options] <target> [command...]
Descriptionβ
The in
command executes commands, scripts, or tasks inside Docker containers and Kubernetes pods. It provides a unified interface for container execution across different container runtimes.
Argumentsβ
<target>
- Target container or pod (supports patterns)[command...]
- Command to execute (optional, defaults to interactive shell)
Target Patternsβ
containers.name
- Specific Docker containerpods.name
- Specific Kubernetes podcontainers.*
- All Docker containerspods.*
- All Kubernetes pods{containers.web,containers.api}
- Multiple specific targets
Optionsβ
General Optionsβ
-p, --profile <profile>
- Configuration profile to use-i, --interactive
- Interactive mode (attach to container)--task <task>
- Execute a configured task in the target--repl
- Start a REPL session with $target available-t, --timeout <duration>
- Command timeout (e.g., 30s, 5m)-e, --env <key=value>
- Environment variables (can be used multiple times)-d, --cwd <path>
- Working directory in container-u, --user <user>
- User to run command as--parallel
- Execute on multiple targets in parallel-v, --verbose
- Enable verbose output-q, --quiet
- Suppress output--dry-run
- Preview execution without running
Examplesβ
Basic Command Executionβ
Execute simple commands in containers:
# Execute in Docker container
xec in containers.app "ls -la"
# Execute in Kubernetes pod
xec in pods.webapp "date"
# Check running processes
xec in containers.db "ps aux"
# Run package manager commands
xec in containers.node "npm test"
Interactive Shellβ
Start interactive shell sessions:
# Interactive shell in container (default)
xec in containers.app
# Interactive shell with specific user
xec in -u root containers.web
# Interactive shell with custom working directory
xec in -d /app containers.api
Script Executionβ
Run scripts with container context:
# Execute TypeScript script
xec in containers.app ./scripts/deploy.ts
# Execute JavaScript file
xec in pods.worker ./migrate.js
# Script with $target context available
xec in containers.test ./test-runner.js
Task Executionβ
Run configured tasks in containers:
# Run test task
xec in containers.app --task test
# Run build task
xec in pods.builder --task build
# Run custom task
xec in containers.* --task migrate --parallel
REPL Modeβ
Start REPL with container context:
# Start REPL with $target available
xec in containers.app --repl
# REPL for debugging
xec in pods.api --repl
# Access container filesystem in REPL
# > await $target`ls -la`
# > const config = await $target`cat /app/config.json`
Multiple Target Executionβ
Execute on multiple targets:
# Run on all containers
xec in containers.* "date"
# Parallel execution
xec in containers.* "npm test" --parallel
# Specific containers
xec in {containers.web,containers.api} "health-check"
Environment Variablesβ
Set environment variables for execution:
# Single environment variable
xec in containers.app -e NODE_ENV=production "npm start"
# Multiple environment variables
xec in pods.worker -e DB_HOST=localhost -e DB_PORT=5432 "./run.sh"
# Override container defaults
xec in containers.test -e DEBUG=true "pytest"
Working Directoryβ
Set working directory for command:
# Change to specific directory
xec in containers.app -d /app/src "npm test"
# Run from project root
xec in pods.builder -d /workspace "make build"
# Temporary directory
xec in containers.temp -d /tmp "./cleanup.sh"
Timeout Controlβ
Set execution timeouts:
# 30 second timeout
xec in containers.app -t 30s "npm test"
# 5 minute timeout
xec in pods.migration -t 5m "./migrate.js"
# 1 hour timeout for long tasks
xec in containers.backup -t 1h "./backup.sh"
User Contextβ
Run commands as specific user:
# Run as root
xec in -u root containers.app "apt-get update"
# Run as application user
xec in -u app containers.web "npm install"
# Run as numeric UID
xec in -u 1000 pods.worker "./script.sh"
Target Typesβ
Docker Containersβ
Execute in Docker containers:
# Named container
xec in containers.myapp "ls"
# Container by ID
xec in containers.abc123 "date"
# All running containers
xec in containers.* "health-check" --parallel
Kubernetes Podsβ
Execute in Kubernetes pods:
# Pod in default namespace
xec in pods.webapp "kubectl version"
# Pod with specific container
xec in pods.multi-container "date"
# (uses container from config)
# All pods matching pattern
xec in pods.worker-* "./process.sh" --parallel
Advanced Usageβ
Parallel Executionβ
Run commands on multiple targets simultaneously:
# Test all services
xec in containers.* --task test --parallel
# Deploy to multiple pods
xec in pods.app-* "./deploy.sh" --parallel
# Collect logs from all containers
xec in containers.* "tail -n 100 /var/log/app.log" --parallel
Command Chainingβ
Chain multiple commands:
# Multiple commands
xec in containers.app "cd /app && npm test && npm build"
# Pipe commands
xec in pods.processor "cat data.json | jq '.items[]' | wc -l"
# Conditional execution
xec in containers.db "pg_isready && psql -c 'SELECT 1'"
Script Contextβ
Scripts have access to container context:
// deploy.js
const result = await $target`npm run build`;
console.log(result.stdout);
await $target`cp -r dist/* /var/www/`;
Interactive Testingβ
Use interactive mode for debugging:
# Debug container issues
xec in -i containers.broken
# Inspect pod filesystem
xec in -i pods.debug
# Test connectivity
xec in -i containers.network
Configurationβ
Configure defaults in .xec/config.yaml
:
targets:
containers:
app:
type: docker
container: my-app
workdir: /app
user: node
shell: /bin/bash
pods:
worker:
type: k8s
namespace: production
pod: worker-deployment-abc123
container: worker # For multi-container pods
commands:
in:
timeout: "1m"
parallel: false
env:
- NODE_ENV=production
Docker-Specific Featuresβ
Docker Exec Optionsβ
The command maps to docker exec
with options:
# Basic execution
xec in containers.app "ls"
# β docker exec app ls
# With user and workdir
xec in -u root -d /tmp containers.app "pwd"
# β docker exec -u root -w /tmp app pwd
# Interactive
xec in -i containers.app
# β docker exec -it app /bin/sh
Container Statesβ
- Container must be running
- Use container name or ID
- Supports pattern matching on names
Kubernetes-Specific Featuresβ
Kubectl Exec Optionsβ
The command maps to kubectl exec
with options:
# Basic execution
xec in pods.webapp "ls"
# β kubectl exec webapp -- ls
# With namespace (from config)
xec in pods.webapp "date"
# β kubectl exec -n production webapp -- date
# Specific container in pod
xec in pods.multi "ps"
# β kubectl exec multi -c app -- ps
Pod Selectionβ
- Pod must be in Running state
- Namespace from configuration
- Container selection for multi-container pods
Error Handlingβ
The command handles common errors:
- Container/pod not found
- Container not running
- Command not found in container
- Permission denied
- Timeout exceeded
Use -v, --verbose
for detailed error information.
Performance Considerationsβ
- Commands execute directly in containers (no SSH overhead)
- Parallel execution for multiple targets
- Stream processing for large outputs
- Efficient for batch operations
Security Notesβ
- Respects container user permissions
- Environment variables not logged
- Sensitive commands should use
--quiet
- Interactive mode for secure input
Troubleshootingβ
Container Not Foundβ
# List available containers
docker ps
# Check configuration
xec inspect containers
Pod Not Readyβ
# Check pod status
kubectl get pods
# Wait for pod ready
kubectl wait --for=condition=ready pod/webapp
Permission Deniedβ
# Run as root
xec in -u root containers.app "chmod +x script.sh"
# Check container user
xec in containers.app "whoami"
Related Commandsβ
Exit Codesβ
0
- Success1
- General error2
- Invalid arguments3
- Target not found4
- Command execution failed124
- Timeout exceeded