gws exec
Execute commands in a service container.
Usage
gws exec <service> -w [worktree] -p [project] [options]
gws exec <service> -- <command> [args...]
Output:
# Interactive shell
root@api-pod:/app#
# Command execution
Database migration completed successfully!
Note: This command can be run from anywhere using the
-p [project]option.
Arguments
<service>- Name of the service to execute command in. Required.[command...]- Command and arguments to execute (optional, defaults to/bin/sh)
Options
| Option | Description | Default |
|---|---|---|
-w, --worktree <name> | Worktree name | Main project deployment |
-p, --project <name> | Project name | Auto-detected from current directory |
-i, --stdin | Pass stdin to the container | Auto (true for shell) |
-t, --tty | Allocate a TTY | Auto (true for shell) |
-c, --container <name> | Container name (for multi-container pods) | First container |
--shell <path> | Shell to use for interactive mode | /bin/sh |
Examples:
# Open interactive shell (default worktree)
gws exec api
# Open shell in specific worktree
gws exec api -w feature-auth
# Run specific command
gws exec api -- npm run migrate
# Execute database query
gws exec database -- psql -U postgres -c "SELECT * FROM users;"
# Check container environment
gws exec api -- env
# Inspect files
gws exec api -- ls -la /app
# Use bash instead of sh
gws exec api --shell /bin/bash
# Target specific container in multi-container pod
gws exec api -c sidecar-container -- ls /data
Typical Use Cases
Debug application
# Open shell to inspect application state
gws exec api -w feature-auth
# Inside container:
cd /app
cat .env
ps aux
Run database migrations
# Execute migration script
gws exec api -w feature-auth -- npm run migrate:latest
Clear cache
# Clear Redis cache
gws exec redis -w feature-auth -- redis-cli FLUSHALL
Check logs in container
# View application logs
gws exec api -w feature-auth -- tail -f /var/log/app.log
Multirepo Behavior
When running gws exec in a multirepo setup, the command targets a specific service within the shared namespace.
Project Structure
myapp/ # Parent directory
├── .worktrees/ # Shared worktrees folder (at parent level)
│ └── feature-auth/ # One folder per worktree name
│ ├── api/ # Worktree for api repo
│ │ └── src/
│ └── app/ # Worktree for app repo
│ └── src/
├── api/ # Git repo 1
│ ├── .git/
│ └── src/
└── app/ # Git repo 2
├── .git/
└── src/
Executing in Main Workspace
cd myapp
gws exec api -- npm run migrate
What happens:
- Finds the
apiservice pod in namespacemyapp - Executes the migration command inside the api container
Executing in Worktree
cd myapp
gws exec api -w feature-auth -- npm run migrate
What happens:
- Finds the
apiservice pod in namespacemyapp-feature-auth - Executes the migration command inside the worktree's api container
Key Points:
- All services (api, app, database) are deployed to the same namespace
- You specify which service to execute commands in
- The worktree flag determines which namespace (deployment) to target
- Each service runs in its own pod but shares the namespace
Understanding exec
The exec command uses kubectl exec under the hood to access the container. It:
- Identifies the pod for the service
- Connects to the running container
- Executes the specified command
- Returns the output
Security Considerations
- Commands run with the same user as the container process
- File changes persist only until the pod restarts
- Be careful with destructive commands
- Use for debugging and diagnostics, not for production changes
See Also
- gws logs - View service logs
- gws status - Check service status
- gws expose - Expose service ports