Skip to content

Latest commit

 

History

History
344 lines (263 loc) · 7.91 KB

File metadata and controls

344 lines (263 loc) · 7.91 KB

Containnap - Usage Examples

Installation & Setup

1. Install Dependencies

cd Containnap
pip install -r requirements.txt

2. Run the Tool

python main.py

Scenario 1: Local Docker - Stop Container After Delay

Situation: You have a development container running locally and want to stop it after 45 minutes.

Steps:

1. Run: python main.py
2. Select: 1 (Local)
3. Automatically connects to Docker daemon
4. View running containers
5. Select your container
6. Enter: 45 (minutes)
7. Start scheduler (option 3)

Result: Container will stop after 45 minutes, even if you close the application.


Scenario 2: Remote Docker - Schedule Container Start/Stop

Situation: You need to start a production database container at 2:00 PM and stop it after 8 hours (480 minutes).

Steps:

1. Run: python main.py
2. Select: 2 (Remote)
3. Enter host: 192.168.1.100
4. Enter port: 22
5. Enter username: ubuntu
6. Enter password: ****
7. View all containers
8. Select database container
9. Enter start time: 14:00
10. Enter duration: 480 (8 hours)
11. Start scheduler

Result:

  • Container starts at exactly 14:00
  • Runs for 8 hours
  • Automatically stops at 22:00 (10 PM)

Scenario 3: Multiple Containers with Different Schedules

Situation: You want to schedule multiple containers with different start/stop times.

Script:

python main.py

In Application:

1. Connect (local or remote)
2. Schedule Container A:
   - Stop after 30 minutes
3. View menu → Back to scheduling
4. Schedule Container B:
   - Start at 09:00, stop after 120 minutes (2 hours)
5. Schedule Container C:
   - Stop after 60 minutes
6. View Scheduled Tasks (shows all 3)
7. Start Scheduler

Result: All three containers execute on their own schedule.


Scenario 4: Monitoring Scheduled Tasks

Situation: You've scheduled several tasks and want to see what's coming up.

Steps:

1. Run application
2. Connect to Docker
3. Select: Menu Option 2 (View Scheduled Tasks)

Output Example:

==============================================================
  📋 Scheduled Tasks
==============================================================

╒═════╤══════════════════╤════════╤═══════╤══════════╕
│   # │ Container        │ Action │ Time  │ Duration │
╞═════╪══════════════════╪════════╪═══════╪══════════╡
│   1 │ web-server       │ Stop   │ 16:30 │ —        │
│   2 │ database         │ Start  │ 09:00 │ 120m     │
│   3 │ cache-redis      │ Stop   │ 14:45 │ —        │
│   4 │ database         │ Stop   │ 11:00 │ —        │
╘═════╧══════════════════╧════════╧═══════╧══════════╛

Scenario 5: Handling SSH Connectivity Issues

Situation: You encounter an SSH authentication error.

Common Issues & Solutions:

Issue: "Failed to connect to remote host"

Troubleshooting:
1. Verify host is reachable: ping 192.168.1.100
2. Test SSH manually: ssh ubuntu@192.168.1.100
3. Check credentials are correct
4. Verify port (default 22)

Issue: "Permission denied"

Troubleshooting:
1. User might not have sudo access
2. Try: ssh ubuntu@192.168.1.100
3. Then: sudo docker ps
4. If that fails, user needs sudo privileges

Issue: Can't see Docker daemon

Troubleshooting:
1. Docker must be installed on remote server
2. User must be in docker group or have sudo access
3. Test: sudo docker ps

Scenario 6: Using with Docker Compose Services

Situation: You have a docker-compose setup and want to manage one service container.

Example:

Docker Compose Setup:
- Service: myapp_web_1
- Service: myapp_db_1
- Service: myapp_cache_1

In Containnap:

1. Connect (local)
2. View all containers
3. Select: myapp_db_1
4. Schedule to stop after 60 minutes
5. Start scheduler

Note: This stops only the database container,
      not the entire compose stack

Scenario 7: Development Workflow

Morning Setup:

python main.py
# Connect local
# Schedule web-server to stop after 8 hours (end of workday)
# Schedule test-db to stop after 2 hours (morning testing)
# Start scheduler

During Work:

  • Continue using other applications
  • Containers stop automatically as scheduled
  • Scheduler runs in background

End of Day:

  • Containers already stopped (saved resources)
  • No manual cleanup needed

Scenario 8: Automated Testing Environment

Use Case: Run tests in containers that auto-clean after testing.

python main.py

# Setup:
1. Connect to local Docker
2. Schedule test-container-1: stop after 30 minutes
3. Schedule test-container-2: stop after 30 minutes
4. Schedule test-container-3: stop after 30 minutes
5. Start scheduler

# Result: All test containers stop after 30 min
#         regardless of test results

Advanced Usage: Programmatic Access

If you want to use Containnap modules in your own Python code:

from containnap.docker_manager import LocalDockerManager
from containnap.scheduler import ContainerScheduler

# Direct docker access
docker_mgr = LocalDockerManager()
containers = docker_mgr.get_all_containers()

for container in containers:
    print(f"{container['name']}: {container['status']}")

# With scheduling
scheduler = ContainerScheduler(docker_mgr)
scheduler.schedule_stop_after_delay('abc123', 'my-container', 60)
scheduler.start_scheduler()

Tips & Tricks

Tip 1: Timeouts for Testing

If testing locally, use small numbers:

  • Stop after 1 minute (instead of hours)
  • Start at current time + 1 minute

Tip 2: Container Names vs IDs

  • Use container names in output (clearer)
  • Internally uses short IDs (more reliable)

Tip 3: SSH Connection Issues

  • Always test SSH manually first
  • Ensure user has sudo access
  • Use key-based auth in production (future enhancement)

Tip 4: Multiple Schedules

Don't worry about scheduling the same container multiple times:

  • Can schedule to start at 9:00 and stop at 11:00
  • Can schedule to stop at 14:00
  • Scheduler will execute both

Tip 5: View Logs

On remote servers, check Docker logs:

ssh user@host
sudo docker logs <container-id>

Batch Operations Example

Stop All Dev Containers at End of Day:

python main.py

# Connect → View containers
# Select: dev-web → Stop after 480 minutes (8 hours)
# Back to menu
# Select: dev-api → Stop after 480 minutes
# Back to menu
# Select: dev-db → Stop after 480 minutes
# Start scheduler

All three stop at same time without intervention

Troubleshooting Scenarios

"Container not found" Error

Possible causes:
1. Container was deleted after selection
2. Using remote: container deleted on remote host
3. Container name changed

Solution: Refresh container list and re-select

"Scheduler running but task didn't execute"

Possible causes:
1. Wrong time format (must be HH:MM)
2. System clock not synchronized
3. Scheduler thread crashed

Solution:
1. Check system time
2. Restart scheduler
3. Check Python console for errors

SSH Works but Docker Commands Fail

Possible causes:
1. Docker not installed on remote
2. User doesn't have Docker access
3. Docker daemon not running

Solution:
SSH into host and verify:
- docker ps works
- user can run docker commands

Performance Notes

  • Local Docker: Very fast, uses native socket
  • Remote Docker: Slightly slower due to SSH
  • Scheduler: Minimal CPU usage (checks every 60 seconds)
  • Container Listing: Instant, cached locally
  • Multiple Containers: No performance degradation

For more information, see README.md and DEVELOPER.md