Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 09:01:48 +08:00
commit 9111605393
9 changed files with 653 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
---
name: truenas-docker-ops
description: Interact with Docker containers running on a TrueNAS server via SSH. Use this skill when working with containers on the remote TrueNAS server, including querying databases, inspecting logs, executing commands, or managing container state. Particularly useful for complex operations requiring nested SSH and docker exec commands with special escape sequences.
---
# TrueNAS Docker Operations
## Overview
Enable seamless interaction with Docker containers running on a TrueNAS server accessed via SSH. The skill provides helper scripts for complex nested command execution and reference documentation for server layout and container discovery.
## When to Use This Skill
Use this skill when:
- Querying databases inside containers on the TrueNAS server
- Executing Python or SQL code in remote containers
- Inspecting container logs, status, or configurations
- Working with containers where nested SSH and docker exec commands require complex escape sequences
- Discovering container locations, volume mappings, or configurations on the server
## Server Access
Access the TrueNAS server using the pre-configured SSH alias:
```bash
ssh truenas
```
Authentication is already configured. For detailed server layout information including directory structure and container patterns, refer to `references/server_layout.md`.
## Core Operations
### Discover Running Containers
List all running containers:
```bash
ssh truenas docker ps
```
Filter for specific containers:
```bash
ssh truenas docker ps | grep <container-name>
```
### View Container Configuration
Check the docker-compose file for volume mappings, networks, and settings:
```bash
ssh truenas cat /mnt/performance/home/admin/Develop/nixonomicon/nas/stacks/<container-name>/docker-compose.yml
```
### Execute Python Code in Container
For complex Python execution requiring nested heredocs and escape sequences, use the provided helper script:
```bash
scripts/docker_exec_python.sh <container_name> '<python_code>'
```
**Example:**
```bash
scripts/docker_exec_python.sh anypod '
import sqlite3, json
conn = sqlite3.connect("/data/db/anypod.db")
conn.row_factory = sqlite3.Row
result = conn.execute("SELECT * FROM feed").fetchall()
print(json.dumps([dict(r) for r in result], indent=2))
conn.close()
'
```
The script handles complex quoting and escape sequences for:
1. SSH connection to remote server
2. Docker exec with stdin input
3. Python code containing quotes, newlines, and special characters
**Note**: When using dictionary access in f-strings (e.g., `f"{dict['key']}"`), extract to variables first to avoid quote escaping issues:
```python
# Instead of: print(f"{row['status']}")
# Use:
status = row["status"]
print(f"{status}")
```
### Execute SQLite Queries in Container
For direct SQLite queries, use the provided helper script:
```bash
scripts/docker_exec_sqlite.sh <container_name> <db_path> '<sql_query>'
```
**Example:**
```bash
scripts/docker_exec_sqlite.sh anypod /data/db/anypod.db '
SELECT feed_id, status, COUNT(*) as count
FROM download
GROUP BY feed_id, status;
'
```
### View Container Logs
```bash
ssh truenas docker logs <container-name>
```
Add `-f` to follow logs in real-time:
```bash
ssh truenas docker logs -f <container-name>
```
### Simple Command Execution
For straightforward commands without complex escaping needs:
```bash
ssh truenas docker exec -i <container-name> <command>
```
**Example:**
```bash
ssh truenas docker exec -i anypod ls -la /data
```
## Directory Structure Reference
Key locations on the TrueNAS server:
- **Compose files**: `/mnt/performance/home/admin/Develop/nixonomicon/nas/stacks/`
- **Configs**: `/mnt/performance/docker/`
- **Large data**: `/mnt/capacity/watch/`
For complete directory structure details and container patterns, see `references/server_layout.md`.
## Determining What to Run Where
**Use helper scripts when:**
- Executing multi-line Python code in containers
- Running SQL queries with complex syntax
- Dealing with nested heredocs or quote escaping
- The command would require trial-and-error to get the escaping right
**Use direct commands when:**
- Running simple single commands
- Viewing logs or status
- Listing files or inspecting basic container state
- The operation doesn't involve complex escape sequences
## Resources
### scripts/
**`docker_exec_python.sh`** - Execute Python code inside a Docker container on TrueNAS
- Handles complex escape sequences for nested SSH and docker exec with heredoc
- Usage: `scripts/docker_exec_python.sh <container_name> '<python_code>'`
- Avoids manual trial-and-error of nested quoting
**`docker_exec_sqlite.sh`** - Execute SQLite queries inside a Docker container on TrueNAS
- Handles complex escape sequences for SQL queries
- Usage: `scripts/docker_exec_sqlite.sh <container_name> <db_path> '<sql_query>'`
- Simplifies database inspection workflows
### references/
**`server_layout.md`** - Complete TrueNAS server structure documentation
- SSH access patterns
- Directory structure for compose files, configs, and data
- Container discovery techniques
- Common container patterns and examples
- Volume mapping conventions

View File

@@ -0,0 +1,89 @@
# TrueNAS Server Layout
## SSH Access
Access the TrueNAS server using the pre-configured SSH alias:
```bash
ssh truenas
```
Authentication is already configured on the host machine.
## Directory Structure
### Docker Compose Files
All container compose files are located at:
```
/mnt/performance/home/admin/Develop/nixonomicon/nas/stacks/
```
This is the **source of truth** for container configurations, volume mappings, and network settings.
### Configuration Files
Container-specific configurations are stored at:
```
/mnt/performance/docker/
```
Typically organized as `/mnt/performance/docker/<container-name>/`.
### Data Storage
Large data files (downloads, media, etc.) are stored at:
```
/mnt/capacity/watch/
```
## Discovering Container Information
### List Running Containers
```bash
ssh truenas docker ps
```
### Find Container Configuration
To understand a container's volume mappings, networks, and settings:
```bash
ssh truenas cat /mnt/performance/home/admin/Develop/nixonomicon/nas/stacks/<container-name>/docker-compose.yml
```
### Common Container Pattern
Most containers follow this structure:
- **Config**: `/mnt/performance/docker/<container-name>/`
- **Data**: Varies by container, check compose file for volume mappings
- **Large files**: Often mapped to `/mnt/capacity/watch/<container-name>/`
## Working with Containers
### Execute Commands in Container
```bash
ssh truenas docker exec -i <container-name> <command>
```
### View Container Logs
```bash
ssh truenas docker logs <container-name>
```
### Inspect Container Details
```bash
ssh truenas docker inspect <container-name>
```
## Examples
### Check if anypod container is running
```bash
ssh truenas docker ps | grep anypod
```
### View anypod compose configuration
```bash
ssh truenas cat /mnt/performance/home/admin/Develop/nixonomicon/nas/stacks/anypod/docker-compose.yml
```
### Access anypod database
```bash
# Database is typically at /data/db/anypod.db inside the container
ssh truenas docker exec -i anypod sqlite3 /data/db/anypod.db "SELECT * FROM feed;"
```

View File

@@ -0,0 +1,18 @@
#!/bin/bash
# Execute Python code inside a Docker container on TrueNAS server
# Usage: docker_exec_python.sh <container_name> <python_code>
set -euo pipefail
if [ $# -ne 2 ]; then
echo "Usage: $0 <container_name> <python_code>" >&2
echo "Example: $0 anypod 'import sys; print(sys.version)'" >&2
exit 1
fi
CONTAINER_NAME="$1"
PYTHON_CODE="$2"
# Execute Python code in container via SSH
# Pass Python code through stdin to avoid nested heredoc escaping issues
echo "$PYTHON_CODE" | ssh truenas docker exec -i "${CONTAINER_NAME}" /app/.venv/bin/python -

View File

@@ -0,0 +1,35 @@
#!/bin/bash
# Execute SQLite queries inside a Docker container on TrueNAS server
# Usage: docker_exec_sqlite.sh <container_name> <db_path> <sql_query>
set -euo pipefail
if [ $# -ne 3 ]; then
echo "Usage: $0 <container_name> <db_path> <sql_query>" >&2
echo "Example: $0 anypod /data/db/anypod.db 'SELECT * FROM feed;'" >&2
exit 1
fi
CONTAINER_NAME="$1"
DB_PATH="$2"
SQL_QUERY="$3"
# Execute SQLite query in container via Python (since sqlite3 CLI may not be available)
# This approach works universally since Python is always available in the containers
PYTHON_CODE=$(cat <<EOF
import sqlite3, json, sys
try:
conn = sqlite3.connect("${DB_PATH}")
conn.row_factory = sqlite3.Row
result = conn.execute("""${SQL_QUERY}""").fetchall()
if result:
for row in result:
print(json.dumps(dict(row)))
conn.close()
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
EOF
)
echo "$PYTHON_CODE" | ssh truenas docker exec -i "${CONTAINER_NAME}" /app/.venv/bin/python -