> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/rtk-ai/rtk/llms.txt
> Use this file to discover all available pages before exploring further.

# Container Commands

> Compact Docker and Kubernetes operations with 80% token savings

## Overview

RTK provides token-optimized alternatives for Docker and Kubernetes commands. Achieve **80%+ token savings** through:

* **Compact lists**: Tabular format with essential info only
* **Aggregation**: Show counts and summaries
* **Smart truncation**: Limit output to most relevant items
* **Deduplication**: Collapse repeated log lines

## Docker Commands

### rtk docker ps

Compact container list with essential details.

#### Examples

```bash Standard docker ps theme={null}
$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                    NAMES
abc123456789   nginx:latest             "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8080->80/tcp    web-server
def987654321   postgres:14-alpine       "docker-entrypoint.s…"   3 hours ago     Up 3 hours     0.0.0.0:5432->5432/tcp  db
ghi555666777   redis:7-alpine           "docker-entrypoint.s…"   1 day ago       Up 1 day       0.0.0.0:6379->6379/tcp  cache
...
# 20+ lines, ~900 tokens
```

```bash RTK docker ps theme={null}
$ rtk docker ps
🐳 3 containers:
  abc123456789 web-server (nginx:latest) [8080:80]
  def987654321 db (postgres:14-alpine) [5432:5432]
  ghi555666777 cache (redis:7-alpine) [6379:6379]
# 4 lines, ~180 tokens (80% savings)
```

#### Features

* **Container ID**: Truncated to 12 chars
* **Name**: Container name
* **Image**: Short image name (strip registry prefix)
* **Ports**: Compact port mapping
* **Truncation**: Max 15 containers shown

#### Implementation

From `src/container.rs:docker_ps()`:

```rust theme={null}
// Uses docker --format for structured output
docker ps --format "{{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Image}}\t{{.Ports}}"

// Compacts port mappings
"0.0.0.0:8080->80/tcp, 0.0.0.0:8443->443/tcp" → "8080:80,8443:443"
```

### rtk docker images

Compact image list with size aggregation.

#### Examples

```bash Standard docker images theme={null}
$ docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
nginx                    latest    abc123456789   2 weeks ago     142MB
postgres                 14-alpine def987654321   3 weeks ago     231MB
redis                    7-alpine  ghi555666777   1 month ago     32MB
node                     18-alpine jkl888999000   2 months ago    174MB
...
# 20+ lines, ~800 tokens
```

```bash RTK docker images theme={null}
$ rtk docker images
🐳 4 images (579MB)
  nginx:latest [142MB]
  postgres:14-alpine [231MB]
  redis:7-alpine [32MB]
  node:18-alpine [174MB]
# 5 lines, ~150 tokens (81% savings)
```

#### Features

* **Total size**: Aggregated across all images
* **Short names**: Repository:tag format
* **Size units**: Automatic MB/GB conversion
* **Truncation**: Max 15 images shown

### rtk docker logs

Deduplicated container logs.

#### Examples

```bash Standard docker logs theme={null}
$ docker logs web-server --tail 50
2024-01-23 10:00:01 GET /api/users 200
2024-01-23 10:00:02 GET /api/users 200
2024-01-23 10:00:03 GET /api/users 200
2024-01-23 10:00:04 GET /api/posts 200
2024-01-23 10:00:05 GET /api/users 200
...
# 50 lines, ~1500 tokens
```

```bash RTK docker logs theme={null}
$ rtk docker logs web-server --tail 50
2024-01-23 10:00:01 GET /api/users 200 (x12)
2024-01-23 10:00:04 GET /api/posts 200
2024-01-23 10:00:15 GET /api/comments 404
...
# 15 lines, ~450 tokens (70% savings)
```

#### Features

* **Deduplication**: Collapse repeated lines with count
* **Timestamp preservation**: Keep first occurrence timestamp
* **Error highlighting**: Preserve all error lines
* **Tail support**: Works with `--tail` flag

## Kubernetes Commands

### rtk kubectl pods

Compact pod list with namespace support.

#### Examples

```bash Standard kubectl get pods theme={null}
$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
web-deployment-abc123-xyz456        1/1     Running   0          2d
api-deployment-def789-uvw123        1/1     Running   1          5d
worker-deployment-ghi456-rst789     2/2     Running   0          1d
db-statefulset-0                    1/1     Running   0          10d
...
# 20+ lines, ~900 tokens
```

```bash RTK kubectl pods theme={null}
$ rtk kubectl pods
☸️ 4 pods:
  web-deployment-abc123-xyz456 (1/1) Running
  api-deployment-def789-uvw123 (1/1) Running (1 restart)
  worker-deployment-ghi456-rst789 (2/2) Running
  db-statefulset-0 (1/1) Running
# 5 lines, ~180 tokens (80% savings)
```

#### Features

* **Ready status**: Container ready count
* **Restart count**: Show if > 0
* **Status icons**: ✓ (Running), ✗ (Failed), ⏸ (Pending)
* **Namespace**: Optional `-n` flag support

### rtk kubectl services

Compact service list.

#### Examples

```bash Standard kubectl get svc theme={null}
$ kubectl get svc
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
web-service    ClusterIP   10.96.0.1       <none>        80/TCP           10d
api-service    NodePort    10.96.0.2       <none>        8080:30080/TCP   5d
db-service     ClusterIP   10.96.0.3       <none>        5432/TCP         20d
...
# 15+ lines, ~700 tokens
```

```bash RTK kubectl services theme={null}
$ rtk kubectl services
☸️ 3 services:
  web-service (ClusterIP) 10.96.0.1:80
  api-service (NodePort) 10.96.0.2:8080 → 30080
  db-service (ClusterIP) 10.96.0.3:5432
# 4 lines, ~140 tokens (80% savings)
```

#### Features

* **Service type**: ClusterIP, NodePort, LoadBalancer
* **Cluster IP**: Internal IP
* **Port mapping**: Internal → external (for NodePort)
* **Truncation**: Max 15 services

### rtk kubectl logs

Deduplicated pod logs.

#### Examples

```bash Standard kubectl logs theme={null}
$ kubectl logs web-deployment-abc123-xyz456 --tail=50
2024-01-23 10:00:01 INFO: Server started on port 8080
2024-01-23 10:00:02 INFO: Request received GET /health
2024-01-23 10:00:03 INFO: Request received GET /health
2024-01-23 10:00:04 INFO: Request received GET /health
2024-01-23 10:00:05 INFO: Request received GET /api/users
...
# 50 lines, ~1800 tokens
```

```bash RTK kubectl logs theme={null}
$ rtk kubectl logs web-deployment-abc123-xyz456 --tail=50
2024-01-23 10:00:01 INFO: Server started on port 8080
2024-01-23 10:00:02 INFO: Request received GET /health (x15)
2024-01-23 10:00:05 INFO: Request received GET /api/users
...
# 12 lines, ~400 tokens (78% savings)
```

#### Features

* **Deduplication**: Collapse repeated log lines
* **Multi-container**: Works with `-c` container flag
* **Follow mode**: Compatible with `-f` flag
* **Namespace**: Works with `-n` flag

## Common Patterns

### Container Lists

All list commands follow this pattern:

1. Execute with `--format` for structured output
2. Parse fields (ID, name, image, status, ports)
3. Compact long strings (truncate IDs, strip registry)
4. Group similar items
5. Show count + top N items

### Log Deduplication

All log commands use this algorithm:

```rust theme={null}
// From src/log_cmd.rs
for line in logs {
    if line == previous_line {
        count += 1
    } else {
        if count > 1 {
            output.push(format!("{} (x{})", previous_line, count))
        } else {
            output.push(previous_line)
        }
        count = 1
        previous_line = line
    }
}
```

### Exit Code Preservation

All container commands preserve exit codes:

```bash theme={null}
rtk docker ps || echo "Docker daemon not running"
rtk kubectl get pods -n production || echo "Namespace not found"
```

## Implementation Reference

Key functions in `src/container.rs`:

* `docker_ps()`: Parses `docker ps --format` output
* `docker_images()`: Aggregates image sizes
* `docker_logs()`: Deduplicates log lines
* `kubectl_pods()`: Parses `kubectl get pods -o json`
* `kubectl_services()`: Parses service JSON
* `kubectl_logs()`: Deduplicates pod logs

### Port Compaction

```rust theme={null}
// From src/container.rs:compact_ports()
"0.0.0.0:8080->80/tcp, 0.0.0.0:8443->443/tcp"
↓
"8080:80,8443:443"
```

Strips:

* `0.0.0.0:` prefix
* `/tcp` suffix
* Whitespace

### Image Name Shortening

```rust theme={null}
// From src/container.rs:docker_images()
"registry.example.com/org/nginx:latest"
↓
"nginx:latest"
```

## Token Savings Summary

| Command | Standard Tokens | RTK Tokens | Savings |
| - | - | - | - |
| `docker ps` (10 containers) | 900 | 180 | **-80%** |
| `docker images` (15 images) | 800 | 150 | **-81%** |
| `docker logs --tail 50` | 1,500 | 450 | **-70%** |
| `kubectl get pods` (20 pods) | 900 | 180 | **-80%** |
| `kubectl logs --tail 50` | 1,800 | 400 | **-78%** |

## Next Steps

> **[Testing](/commands/testing.md)**
>
> Test runner optimizations

> **[JavaScript](/commands/javascript.md)**
>
> Modern JS/TS stack tools


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.