mirror of
https://github.com/johndoe6345789/docker-swarm-termina.git
synced 2026-04-24 13:45:01 +00:00
This commit enhances the Docker diagnostics system with comprehensive Swarm-specific health checks to ensure the application is properly deployed in a Docker Swarm/CapRover environment. Changes: - Add check_swarm_status() function to verify Docker Swarm configuration - Checks if Docker is running in Swarm mode - Retrieves and logs Swarm node information (hostname, role, state) - Detects if container is running as a Swarm service task - Provides clear diagnostic messages for troubleshooting - Integrate Swarm checks into application startup (app.py) - Runs after Docker connection is verified - Logs success for production Swarm deployments - Warns (but doesn't fail) for local development environments - Add comprehensive test coverage (8 new tests) - Tests for active/inactive Swarm states - Tests for error handling and edge cases - Tests for node retrieval and hostname detection - Maintains 99% overall code coverage (128 tests passing) This ensures that Docker Swarm-related issues are caught early during deployment and provides clear diagnostic information for troubleshooting CapRover deployments with Docker socket mounting. https://claude.ai/code/session_01RRUv2BWJ76L24VyY6Fi2bh
Backend - Flask API
Python Flask backend for Docker container management.
Features
- RESTful API for container management
- Docker SDK integration
- Session-based authentication
- CORS enabled for frontend access
Setup
- Install dependencies:
pip install -r requirements.txt
- Configure environment (optional):
cp .env.example .env
# Edit .env with your settings
- Run the server:
python app.py
The server will start on http://localhost:5000
API Endpoints
Authentication
POST /api/auth/login- Login with username/passwordPOST /api/auth/logout- Logout current session
Containers
GET /api/containers- List all containers (requires auth)POST /api/containers/<id>/exec- Execute command in container (requires auth)
Health
GET /api/health- Health check
Docker
Build the Docker image:
docker build -t docker-swarm-backend .
Run the container:
docker run -p 5000:5000 -v /var/run/docker.sock:/var/run/docker.sock docker-swarm-backend
Debugging
The application includes comprehensive Docker connection diagnostics that run automatically on startup. Check the logs for:
- Docker environment variables (DOCKER_HOST, DOCKER_CERT_PATH, etc.)
- Docker socket existence and permissions
- Current user and group information
- Connection attempt results
Example output:
=== Docker Environment Diagnosis ===
DOCKER_HOST: unix:///var/run/docker.sock
✓ Docker socket exists at /var/run/docker.sock
Socket permissions: 0o140777
Readable: True
Writable: True
Current user: root (UID: 0, GID: 0)
✓ Successfully connected to Docker using Unix socket
✓ Docker connection verified on startup
If connection fails, the diagnostics will show detailed information about what's wrong.
CapRover Deployment
For deploying to CapRover (which uses Docker Swarm), see the detailed guide in CAPROVER_DEPLOYMENT.md.
Key points:
- Uses
captain-definitionfile withserviceUpdateOverrideto mount Docker socket - Runs as root to access Docker socket
- Includes enhanced debugging for troubleshooting connection issues
- Only supports 1 replica (Docker socket can't be shared)
Security
⚠️ This backend requires access to the Docker socket. Ensure proper security measures are in place in production environments.
Security Considerations:
- Container has root access to the host system via Docker socket
- Implement strong authentication (change default credentials)
- Restrict network access to the API
- Only use in trusted environments
- Monitor logs for suspicious activity
- Consider using a Docker socket proxy for additional security